Skip to main content

How to Use Context Variables in Power Apps with UpdateContext

· 4 min read
Jagdish Kumawat
Founder @ Dewiride

A context variable holds a value for one screen: whether something is loading, which item is selected, a table of rows. UpdateContext creates it or changes it. Other formulas on the screen then use it by name, and recalculate when it changes.

Syntax​

Syntax
UpdateContext( { Variable1: Value1 [, Variable2: Value2, ... ] } )

Pass one record: each field name is a variable, each value its new value. The variable is created the first time, with no declaration. UpdateContext works only in behavior properties (UpdateContext function).

Microsoft lists what a context variable can hold: a single value, a record, a table, an object reference, or any result from a formula. It's scoped to its screen, and keeps its value until the app closes, even when you leave the screen and come back.

Name context variables with the loc prefix (Power Apps Naming Conventions).

Example 1: A Loading Flag​

The Career Development Plan app's My Plan screen loads data when it opens. A true-or-false variable says whether it's still loading:

My Plan Screen → OnVisible (start and end)
UpdateContext({locIsLoading: true});
// ...load the data...
UpdateContext({locIsLoading: false})

A spinner shows while it's true (Visible: locIsLoading), and the list shows when it's false (How to Add a Loading Spinner in Power Apps).

Example 2: A Table of Rows​

The same screen keeps the person's competencies and responses in two more variables:

My Plan Screen → OnVisible (the loading part)
UpdateContext({
locMyPlan: Sort(
Filter(RoleCompetencies, JobTitle = MyJobTitle && IsActive),
Competency
)
})

The gallery then uses locMyPlan as its rows. The full formula is in Part 5 of the series.

See the Variables​

Open Variables (the {x} icon on the left) and expand Context variables: they're grouped by screen. After the screen had run in preview, locIsLoading was false and the two tables were there (empty for the admin, whose job title has no competencies):

The Variables pane with Context variables expanded to My Plan Screen, listing locIsLoading (Boolean: false), locMyPlan (Table: 0 rows) and locMyResponses (Table: 0 rows), and Collections reading No collections defined yet

UpdateContext, Set or a Collection?​

UseWhen
UpdateContextThe value belongs to one screen
SetSeveral screens need it (a global variable)
A collectionYou'll add, change or remove rows in the app (ClearCollect)
A named formulaThe value can be worked out from other values, and never set (Named formulas)

Common Mistakes​

MistakeWhat happensDo this instead
Using a context variable on another screenIt isn't available therePass it with Navigate's third argument, or use Set
Expecting a table in a variable to be delegableIt's an in-memory tableFilter in the query that fills it
A variable for a value that never changesExtra steps to keep it setUse a named formula

Troubleshooting​

SymptomLikely causeFix
The variable is blank in StudioThe formula that sets it hasn't runPreview the screen, or Alt+click the button
Behavior function in a non-behavior propertyUpdateContext in a property such as TextUse it in OnSelect or OnVisible (fix)

FAQ​

Can I set several variables at once?​

Yes: UpdateContext({ locA: 1, locB: "x" }).

How do I clear one?​

Microsoft's advice is to set it to Blank().

Conclusion​

UpdateContext({ name: value }) creates or updates a variable for the current screen. It can hold a flag, a record or a table, and the Variables pane shows its current value.

Additional Resources​

Stay Updated

Subscribe to our newsletter for the latest tutorials, tech insights, and developer news.

By subscribing, you agree to our privacy policy. Unsubscribe at any time.