Skip to main content

Notifications

Power Apps - Building Power Apps
Answered

Combining Multiple 'UpdateContext' Into a Single One Does Not Always Seems to Properly Set Values

(0) ShareShare
ReportReport
Posted on by 9

Hello:

 

Below is how I typically set multiple local context variables via the "UpdateContext" function per Microsoft's recommendation for efficiency:

 

UpdateContext(
 {
 filteredHistory:Filter(ResourcesHistory,ResourceID=currentResource.ResourceID).ArchivedOn,
 distinctHistory:RenameColumns(Distinct(filteredHistory,ArchivedOn),"Result","ArchivedOn"),
 sortedDistinctArchivedOn:SortByColumns(distinctHistory,"ArchivedOn",Descending)
 }
);

 

With the above, the local variable 'sortedDistinctArchivedOn' does not get populated correctly (is blank).

 

EDITED/ADDED comments for additional clarifications:

I thought about the combined "UpdateContext" function running concurrently when used in that format, BUT I tested it out several times before posting the question, and in the combined "UpdateContext" format, the second variable 'distinctHistory', which is dependent on the first variable 'filteredHistory' DOES get set correctly every time (which led me to believe that it was not running concurrently), and it was only the last variable 'sortedDistinctArchivedOn' that did not get set correctly with each test.

 

Furthermore, I can't recall from which Microsoft documents I read this, but according to MS, Power Apps executes functions/commands in a linear fashion (even when written in combined format), and the only exception where concurrency is used is in using the "Concurrent" function.

 

However, if I code the same logic using multiple "UpdateContext" functions as shown below, then the local variable 'sortedDistinctArchivedOn' is populated correctly with the sorted distinct dates.

 

UpdateContext({filteredHistory:Filter(ResourcesHistory,ResourceID=currentResource.ResourceID).ArchivedOn});
UpdateContext({distinctHistory:RenameColumns(Distinct(filteredHistory,ArchivedOn),"Result","ArchivedOn")});
UpdateContext({sortedDistinctArchivedOn:SortByColumns(distinctHistory,"ArchivedOn",Descending)});

 

 

Does anyone know the reason why or am I doing something wrong?

Categories:
  • WarrenBelz Profile Picture
    WarrenBelz 145,636 on at
    Re: Combining Multiple 'UpdateContext' Into a Single One Does Not Always Seems to Properly Set Values

    Thanks @emrrken ,

    So this is solved now?

  • Verified answer
    emrrken Profile Picture
    emrrken 9 on at
    Re: Combining Multiple 'UpdateContext' Into a Single One Does Not Always Seems to Properly Set Values

    @WarrenBelz I should have clarified this in my initial post -- I actually also thought about the combined "UpdateContext" function running concurrently when used in that format, BUT I tested it out several times before posting the question, and in the combined "UpdateContext" format, the second variable 'distinctHistory', which is dependent on the first variable 'filteredHistory' DOES get set correctly every time (which led me to believe that it was not running concurrently), and it was only the last variable 'sortedDistinctArchivedOn' that did not get set correctly with each test.

     

    Furthermore, I can't recall from which Microsoft documents I read this, but according to MS, Power Apps executes functions/commands in a linear fashion (even when written in combined format), and the only exception where concurrency is used is in using the "Concurrent" function.

     

    Based on the observed behavior, I have arrived at the same conclusion you did, is that in the combined "UpdateContext" format, Power Apps is setting the variables concurrently (despite results).  So linear dependency setting of context variables using the combined "UpdateContext" format is not guaranteed to work consistently, so if the context variables being set are dependent upon each other linearly, then you need to separate them individually with "UpdateContext" function according to your logic requirements.

  • Verified answer
    WarrenBelz Profile Picture
    WarrenBelz 145,636 on at
    Re: Combining Multiple 'UpdateContext' Into a Single One Does Not Always Seems to Properly Set Values

    Hi @emrrken ,

    That is because that structure runs the elements concurrently, meaning they cannot be dependent on another element - you can do this

    UpdateContext({filteredHistory:Filter(ResourcesHistory,ResourceID=currentResource.ResourceID).ArchivedOn});
    UpdateContext({distinctHistory:RenameColumns(Distinct(filteredHistory,ArchivedOn),"Result","ArchivedOn"});
    UpdateContext({sortedDistinctArchivedOn:SortByColumns(distinctHistory,"ArchivedOn",Descending})

    or this

    With(
     {
     wHistory:
     Filter(
     ResourcesHistory,
     ResourceID = currentResource.ResourceID
     ).ArchivedOn
     },
     With(
     {
     wDistinct:
     RenameColumns(
     Distinct(
     wHistory,
     ArchivedOn
     ),
     "Result",
     "ArchivedOn"
     )
     },
     UpdateContext(
     {	
     sortedDistinctArchivedOn:
     SortByColumns(
     wDistinct,
     "ArchivedOn",
     Descending
     )
     }
     )
     )
    );

     

    Please click Accept as solution if my post helped you solve your issue. This will help others find it more readily. It also closes the item. If the content was useful in other ways, please consider giving it Thumbs Up.

    Visit my blog Practical Power Apps

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

Microsoft Kickstarter Events…

Register for Microsoft Kickstarter Events…

Announcing Our 2025 Season 1 Super Users!

A new season of Super Users has arrived, and we are so grateful for the daily…

Announcing Forum Attachment Improvements!

We're excited to announce that attachments for replies in forums and improved…

Leaderboard

#1
WarrenBelz Profile Picture

WarrenBelz 145,636

#2
RandyHayes Profile Picture

RandyHayes 76,287

#3
Pstork1 Profile Picture

Pstork1 64,942

Leaderboard