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?
Thanks @emrrken ,
So this is solved now?
@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.
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