cancel
Showing results for 
Search instead for 
Did you mean: 
Reply
mohankvs
Frequent Visitor

Split Strings in Gallery

Hi All,

 

My app has one gallery, with ID column and Description column. I have two rowsDescription column has 2 lines. I need to split these two lines and create two entries for same ID.

 

I used below code. It splits the description into multiple rows but ID gets repeated.

 

Can someone please let me know what mistake I am making in this code? Thanks in advance.

 

Clear(PrimaryCollection);Clear(TempCollection);Clear(SecondaryCollection);

ForAll(Gallery1.AllItems,Collect(PrimaryCollection,{ID:lbl_ID.Text,Desc:lbl_Desc.Text}));

ForAll(Gallery1.AllItems,Collect(TempCollection, Split(lbl_Desc.Text,"* ")));

ForAll(TempCollection,

Collect(SecondaryCollection,{ID:lbl_ID.Text,Desc:Result}))

 

 

 

6 REPLIES 6
cwebb365
Super User
Super User

Something like this should work. 

Clear(PrimaryCollection);

ForAll(Gallery1.AllItems,Collect(PrimaryCollection,{ID:lbl_ID.TextDesc:First(Split(lbl_Desc.Text,"* "))}));

ForAll(Gallery1.AllItems,Collect(PrimaryCollection,{ID:lbl_ID.TextDesc:Last(Split(lbl_Desc.Text,"* "))}));

 

Then you can sort PrimaryCollection by ID etc. 

mohankvs
Frequent Visitor

@cwebb365 Thanks for your suggestion. It solves 50%. What I see now is 

 

ID 1, Text2, and ID 2, Example 2. 

 

ID 1, Text1 and ID 2, Example 1 entries are missing. 

 

I noticed that there is a space before the sentence starts and this is causing SPACE to be considered as first record. Also, what if i have three bullet points instead of two? Since this is taking only FIST and LAST, how can i handle the remaining records incase if i have more than two bullet points in future? Thanks for your help.

cwebb365
Super User
Super User

You can substitue FirstN(Split(lbl_Desc.Text,"* "),2) and Last to get the 2nd and 3rd. 

cwebb365
Super User
Super User

For Dynamic, I guess you could do it backwards. 

 

Do 

Clear(PrimaryCollection);Clear(SecondaryCollection);

ForAll(Gallery1.AllItems AS GalItems,Collect(PrimaryCollection,{DescItems: ForAll(AddColumns(Split(GalItems.Desc,"* "),"ID",GalItems.ID),Collect(SecondaryCollection,{ID:ID,Desc:Result}))}));

 

Basically Nesting ForAll's and using AddColumns to get the alias.ID (id?) from the Gallery1.Allitems into the secondary collection. So it Loops through, then loops through and gets a table of the split's and then loops through those adding new items to the secondary. 

 

You will need to put if logic's or something to ignore just " "'s or something. Or remove them afterwards with a remove(filter(secondarycollection,desc = " ")) or something like that, but this should get you what you need. Might not be the most performant due to loops but if the item count isn't super high in the scenario should be fine. 

mohankvs
Frequent Visitor

@cwebb365 Thanks for your help. I am getting a syntax error, trying to fix it. It says "The function "Clear" has some invalid arguments"

cwebb365
Super User
Super User

🤷‍♂️ You're going to have to post up what you got or something. I just converted my test functions into your collection names, but it shouldn't be off much. 

Helpful resources

Announcements
Ignite 2022

WHAT’S NEXT AT MICROSOFT IGNITE 2022

Explore the latest innovations, learn from product experts and partners, level up your skillset, and create connections from around the world.

Power Apps Africa Challenge 2022

Power Apps Africa Challenge

Your chance to join an engaging competition of Power Platform enthusiasts.

Super User 2 - 2022 Congratulations

Welcome Super Users

The Super User program for 2022 - Season 2 has kicked off!

September Events 2022

Check out all of these events

Attend in person or online, there are incredible conferences and events happening all throughout the month of September.

Users online (3,701)