web
You’re offline. This is a read only version of the page.
close
Skip to main content

Notifications

Announcements

Community site session details

Community site session details

Session Id :
Power Platform Community / Forums / Power Apps / Update a column value ...
Power Apps
Unanswered

Update a column value in a collection using ForAll

(1) ShareShare
ReportReport
Posted on by 117

Hi,

I am using ForAll to loop through a collection, I would like to change a column value for the referenced record based on a condition

I read about Record Scope and it seems the following code should work

ForAll(CommunityProfile, If(Condition, {Community :"AA"}))

I also tried the following:

ForAll(CommunityProfile, If(Condition, CommunityProfile[@Community] = "AA"))

and they both didn't work 

 

any help!

 

Thanks

Categories:
I have the same question (0)
  • Verified answer
    RandyHayes Profile Picture
    76,297 Super User 2024 Season 1 on at

    @Alaa-Alarori 

    One thing to always keep in mind about ForAll, is that it is really not entirely like a For Loop that you might be used to in a programing environment. Primarily - ForAll *returns* a table.  The action you take within the ForAll determines the "shape" of the table returned.  ForAll's can be used to do actions within them that impact data, but they require more steps to do so.

     

    So, in your first formula:

    ForAll(CommunityProfile, If(Condition, {Community :"AA"}))

    Would return a Record with a Community column with the value "AA" for every record in CommunityProfile where the Condition was true.  If there were 5 records that met the condition in CommunityProfile, you would have a table with 5 rows, with one column called Community and all with the value of "AA".  HOWEVER, you didn't actually do anything with the results of the ForAll...so, nothing happened (not that it was going to be what you wanted anyway).

     

    Your second formula just wouldn't work anyway!  You can't assign values quite like that. So we won't go much more on that one.

     

    NOW...what you want to do.  You have a few options.  One option is to simply use an UpdateIf function on your collection.  So, something like this:

    UpdateIf(CommunityProfile, Condition, {Community:"AA"})

    This would actually update the Community column with "AA" in any record in CommunityProfile that meets the condition you have.  (Also note: the UpdateIf will return a table of modified records, should you need to capture the results for some reason.)

     

    The other option is to use the ForAll, but it is much more challenging.  The reason is, you would either Patch/Update the record currently being referenced in the ForAll, or re-shape a resulting table to return from your ForAll.

    If you use the ForAll to Patch/Update, then you need to have a reference to the current record.  Within the ForAll, you can make use of any of the fields/columns of the current record in the ForAll, but you cannot reference the entire record.  So, if your data has some identifying component to it (like an ID), then you can reference that to lookup and Patch/Update the actual value in the datasource.  Something like:

    ForAll(CommunityProfile, 
     If(Condition, 
     Patch(CommunityProfile, 
     Lookup(CommunityProfile, ID=CommunityProfile[@ID]), 
     {Community:"AA"}
     )
     )
     )

    You can see that gets more complex.

     

    That second option in the ForAll is to re-shape the data and return it in your ForAll.  This would involve returning either the same record schema (which you'd have to type out for each column) or a different altered schema.

     

    But...I don't believe that (based on your original question and perceived intention) this is what you want to do.

     

    I hope this is clear and helpful for you.

     

     

  • Verified answer
    v-xida-msft Profile Picture
    on at

    Hi @Alaa-Alarori ,

    Do you want to changed a column value in for the referenced record within the ForAll loop based on a Condition?

     

    Based on the formula that you mentioned, I think there is something wrong with it. In addition, I think the Disambiguation operator (@) could not achieve your needs within your ForAll formula.

    I have made a test on my side, I think the RenameColumns function could achieve your needs, please modify your formula as below:

     

    ForAll(
    RenameColumns(CommunityProfile, "PrimaryColumn", "PrimaryColumn1"),
    If(
    Condition,
    Patch( /* <-- Use Patch function to update value */
    CommunityProfile,
    LookUp(CommunityProfile, PrimaryColumn = PrimaryColumn1),
    {
    Community: "AA"
    }
    )
    )
    )

    Note: The PrimaryColumn represents the column in your CommunityProfile collection, which used to identify a record uniquely.

     

    More details about the RenameColumns function in PowerApps, please check the following article:

    RenameColumns function

     

    Best regards,

     

  • Alaa-Alarori Profile Picture
    117 on at

    Hi @v-xida-msft 

    this works like a charm!

    thanks for the code example 

     

    @RandyHayes 

    Your explanation is aowsome, it's really beneficial to understand the result of each function, this will change the way I assemble formulas.

    thanks for taking the time to explain in details

     

    Best,

    Alaa

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

Forum hierarchy changes are complete!

In our never-ending quest to improve we are simplifying the forum hierarchy…

Ajay Kumar Gannamaneni – Community Spotlight

We are honored to recognize Ajay Kumar Gannamaneni as our Community Spotlight for December…

Leaderboard > Power Apps

#1
WarrenBelz Profile Picture

WarrenBelz 796 Most Valuable Professional

#2
Michael E. Gernaey Profile Picture

Michael E. Gernaey 327 Super User 2025 Season 2

#3
Power Platform 1919 Profile Picture

Power Platform 1919 268

Last 30 days Overall leaderboard