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

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

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?

2 ACCEPTED SOLUTIONS

Accepted Solutions
WarrenBelz
Super User
Super User

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

View solution in original post

emrrken
Frequent Visitor

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

View solution in original post

3 REPLIES 3
WarrenBelz
Super User
Super User

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

emrrken
Frequent Visitor

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

Thanks @emrrken ,

So this is solved now?

Helpful resources

Announcements
Microsoft 365 Conference – December 6-8, 2022

Microsoft 365 Conference – December 6-8, 2022

Join us in Las Vegas to experience community, incredible learning opportunities, and connections that will help grow skills, know-how, and more.

Difinity Conference 2022

Difinity Conference 2022

Register today for two amazing days of learning, featuring intensive learning sessions across multiple tracks, led by engaging and dynamic experts.

European SharePoint Conference

European SharePoint Conference

The European SharePoint Conference returns live and in-person November 28-December 1 with 4 Microsoft Keynotes, 9 Tutorials, and 120 Sessions.

Power Apps Ideas

Changes to Ideas Coming

We are excited to announce a new way to share your ideas for Power Apps!

Top Solution Authors
Top Kudoed Authors
Users online (4,412)