cancel
Showing results for 
Search instead for 
Did you mean: 
Reply
JR-BejeweledOne
Super User
Super User

Nested ForAll with Text Split

I am bringing data from one collection into another but it has to be manipulated based on a specific column value.  If the column is empty the data comes over as is.

 

If the column is not empty, I need to do a split on the text (semi-colon separated) and collect a row for each item in the result.  The remaining data will be the same for each item.

 

SplitText.png

 

So the end result of this image would be (includes Country, Date and Holiday)

 

Country Date Holiday Region
Brazil 01/01/2021 New Year's Day

Manaus

Brazil 04/02/2021 Good Friday Manaus
Brazil 04/02/2021 Good Friday Sao Paulo
Brazil 04/02/2021 Good Friday Sorocaba
Brazil 05/01/2021 Labor Day Manaus
US 07/04/2021 Independence Day  
US 09/02/2021 Labor Day  

 

This is what I have come up with but it doesn't like it.   The collection Test is a new collection and all the values going into it are text.

 

CollectError.png

 

ForAll(HolidayCollection,
        If(IsBlank(Region),
           Collect(Test, 
            {
              Country: Country,
              Date: Date,
              HolidayName: HolidayName,
              Region: ""
            }
           ),

            ForAll(TrimEnds(Split(Region, ";")).Result,
            Collect(Test,
            {
                Country: HolidayCollection.Country,
                Date: HolidayCollection.Date,
                HolidayName: HolidayCollection.HolidayName,
                Region: Result
            }
            )
          )
        )
    )

 

 



If you like this post, give it a Thumbs up. If it answered your question, Mark it as a Solution to enable other users find it.
1 ACCEPTED SOLUTION

Accepted Solutions

Ah, I see that you have an extra ';' at the end of the region - which is generating that extra record. We can get rid of it by filtering the result of the split function for non-empty records only:

ForAll(
    HolidayCollection As HC,
    ForAll(
        Filter(
            Split(Coalesce(TrimEnds(HC.Region), " "), ";"),
            !IsBlank(Result)),
        Collect(
            Test,
            {
                Country: HC.Country,
                Date: HC.Date,
                Holiday: HC.Holiday,
                Region: Coalesce(TrimEnds(Result), Blank())
            })))

In this latest expression I also moved the TrimEnds to inside the Collect function as I think it is easier to read, but it could remain on the first parameter of the second ForAll as well.

View solution in original post

8 REPLIES 8
CarlosFigueira
Power Apps
Power Apps

You can use the Coalesce function to define some value for the 'Region' if it is blank, and use a single Collect expression for the Test collection, like in the example below:

ForAll(
    HolidayCollection As HC,
    ForAll(
        TrimEnds(Split(Coalesce(HC.Region, " "), ";")).Result,
        Collect(
            Test,
            { Country: HC.Country, Date: HC.Date, Holiday: HC.Holiday, Region: Result })))

Hope this helps!

I am not trying to define a region if it's blank.    If it's blank we need to leave that blank.   This collection will have other actions performed against it later and having the correct region or no region is critical to that.



If you like this post, give it a Thumbs up. If it answered your question, Mark it as a Solution to enable other users find it.

In my previous expression the region would be an empty string (similar to what you had in your original question). If you want it to be Blank (null), you can use the Coalesce function again:

 

ForAll(
    HolidayCollection As HC,
    ForAll(
        TrimEnds(Split(Coalesce(HC.Region, " "), ";")).Result,
        Collect(
            Test,
            {
                Country: HC.Country,
                Date: HC.Date,
                Holiday: HC.Holiday,
                Region: Coalesce(Result, Blank())
            })))

 

The attached app shows this expression. To open it, download it locally, then go to https://create.powerapps.com, and select Open -> Browse, and select the file that you saved.

Hope this helps!

JR-BejeweledOne
Super User
Super User

That worked but it gave me an extra row with a blank region.   That row shouldn't exist.

 

coalesceerror.png

 



If you like this post, give it a Thumbs up. If it answered your question, Mark it as a Solution to enable other users find it.

Ah, I see that you have an extra ';' at the end of the region - which is generating that extra record. We can get rid of it by filtering the result of the split function for non-empty records only:

ForAll(
    HolidayCollection As HC,
    ForAll(
        Filter(
            Split(Coalesce(TrimEnds(HC.Region), " "), ";"),
            !IsBlank(Result)),
        Collect(
            Test,
            {
                Country: HC.Country,
                Date: HC.Date,
                Holiday: HC.Holiday,
                Region: Coalesce(TrimEnds(Result), Blank())
            })))

In this latest expression I also moved the TrimEnds to inside the Collect function as I think it is easier to read, but it could remain on the first parameter of the second ForAll as well.

That worked but I need to add an additional step and can't figure it out.    Items are added to the 'Test' collection via the method we just worked with and also added when a new Holiday is added individually.

 

When it's added individually there is an If statement that checks to make sure the holiday doesn't already exist in the 'Test' collection.   I modified the formula and while it works, if there is a holiday that is in multiple regions it only adds the first one it finds and not the rest.  The If statement is the same one I use when adding individual holidays and it works great.

 

So If New Year's Day is in Manaus, Sao Paulo and Sorcaba, I get this:

 

FilterHoliday1.png

 

Instead of this:

 

FilterHoliday2.png

 

Clear(Test);
ForAll(
    HolidayCollection As HC,
    ForAll(
        Filter(
            Split(Coalesce(TrimEnds(HC.Region), " "), ";"),
            !IsBlank(Result)),
        If( CountIf(Test, Region in HC.Region && Holiday in HC.Holiday) = 0,
        Collect(
            Test,
            {
                Country: HC.Country,
                Date: HC.Date,
                Holiday: HC.Holiday,
                Year: HC.Year,
                Region: Coalesce(TrimEnds(Result), Blank())
                
            }
            )
        )
    )
)

 

 

 



If you like this post, give it a Thumbs up. If it answered your question, Mark it as a Solution to enable other users find it.

I'm not sure I understand your new requirement, so I updated the uploaded app (now called ForumThread905927b.msapp) with a new functionality to add new holidays as shown in the animation below. If this is what your scenario needs then hopefully it will solve your problem. Otherwise, can you save an app that shows what you are trying to do?

AddManyIfNotExist.gif

Hope this helps!

JR-BejeweledOne
Super User
Super User

Holidays are added to the collection either individually or in bulk (this thread).   Both methods of adding holidays need to check and make sure it doesn't already exist in the collection before adding it.

 

I was trying to add the piece that checks to see if the holiday exists in the collection and that's what I was having problems with.

 

 



If you like this post, give it a Thumbs up. If it answered your question, Mark it as a Solution to enable other users find it.

Helpful resources

Announcements
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.

Government Carousel

New forum: GCC, GCCH, DoD - Federal App Makers (FAM)

In response to the unique and evolving requirements of the United States public sector, Microsoft has created Power Apps US Government.

Users online (4,647)