I have a table/collection in PowerApps where the strings are formatted like: WW031-231, DT031-222, etc. I would like to convert these by inserting a hyphen after the first two characters, to produce WW-031-231, DT-031-222, etc.
What is the simplest way to do this? Some combination of replace, substitute, split, LEN?
Thanks.
Hi @Runner55552
Thanks - that's correct - AddColumns doesn't add columns to an existing collection - it's a function that returns a table with the additional columns you specify. Therefore, you would need to Collect to a new collection, or you can do as I did in the screenshot and set the Items property of a gallery, datatable or control directly to the call to AddColumns.
Thanks! Just accepted yours also. I did not know you could accept more than one solution. Also, I just tried your solution in my app and it worked well. Since some of my codes have a different format (132-2344 or 132-234), without letters at the front, I needed to wrap an If statement around the With code to check the total length of the string. If >= 9 characters, implement the With statement, else keep the original text.
I have also noticed that I cannot use AddColumns on an existing collection. It appears to add columns to a collection, I need to create a new collection. Is that correct?
Thanks @Runner55552 - I'm glad that works for you. Please note that it's possible to select multiple posts as solutions in situations like this.
Thanks. This looks like it would work. Accepted the first suggestion above from @SpongYe as the solution, partly because it came in first, but both solutions appear to work.
Hi @Runner55552
If you're looking for some formula to apply this transformation to a collection, let's say you have a collection that looks like this:
ClearCollect(colData,
{Code: "WW031-231"}, {Code: "DT031-222"}
)
You can call the AddColumns function to apply this transformation to the collection.
AddColumns(colData,
"NewCode",
With({splitStr:Split(Code, "-")},
Left(Code, 2) & "-" &
Mid( First(splitStr).Value,3, Len(First(splitStr).Value)-2) & "-" &
Last(splitStr).Value
)
)
Hi @Runner55552
I used Set but use what you are more comfortable with:
Set(OriginalString, "WW031-231");
Set(FirstString, Left(OriginalString, 2));
Set(SeconString, Mid(OriginalString, 3, 3));
Set(ThirdString, Right(OriginalString, Len(OriginalString) - 6));
Set(
CombinedString,
Concatenate(FirstString, "-", SeconString, "-", ThirdString)
)
If you have any questions or feedback, please let me know. Have a great day! 😊
-----------------------
PowerYsa Power Platform Enthusiast [LinkedIn] | [Youtube]
I love to share my knowledge and learn from others. If you find my posts helpful, please give them a thumbs up 👍 or mark them as a solution ✔️. You can also check out my [@PowerYSA] for some cool solutions and insights. Feel free to connect with me on any of the platforms above. Cheers! 🍻