Power Fx Variables in Canvas Apps: Set vs UpdateContext vs Collections vs Named Formulas | PowerPlatformTip

Power Fx Variables in Canvas Apps: Set vs UpdateContext vs Collections vs Named Formulas | PowerPlatformTip

Power Fx Variables Deep Dive 🔢

Power Apps is formula-first: values recalculate automatically as the user interacts. Reach for a variable only when you truly need to store state, and then pick the right kind by scope and lifetime.

TL;DR: Canvas apps have three variable types: global variables (Set, whole app), context variables (UpdateContext/Navigate, one screen), and collections (Collect/ClearCollect, a table). Prefer formulas first; use With() for a temporary value inside one formula, and Named Formulas for reusable derived values that recalculate automatically.

The three variable types

Type Scope Create with Use for
Global variable Whole app Set User, theme, selected record
Context variable One screen UpdateContext Screen state, screen params
Collection Whole app Collect A table you build locally

🔸 Variables are implicitly created the moment they appear in Set, UpdateContext, Navigate, Collect or ClearCollect. There’s no explicit declaration, and the type is inferred from first use (all uses must agree on type).

🔸 All variables live in memory while the app runs; when the app closes, their values are lost (initial value is blank). Persist to a data source with Patch/Collect, or to the device with SaveData.

Global variables: Set

🔸 Set( varName, value ) creates or updates a global variable available on every screen. It can hold a single value, a record, a table, an object reference, or any formula result.

🔸 Typical uses: store the logged-in user (Set( gvUser, User() )) in App.OnStart, hold style/theme values, or track the currently selected item.

🔸 Set captures a value at the moment it runs. If the underlying formula changes later, the variable does not auto-update. That manual refresh is error-prone, so prefer a formula or Named Formula where you can.

Context variables: UpdateContext / Navigate

🔸 UpdateContext( { name: value } ) creates or updates a context variable scoped to the current screen; you can’t reference it from another screen.

🔸 A common use is toggling UI state, for example UpdateContext({ locPopUp: true }) to show a popup and false to hide it, or flipping a boolean with UpdateContext({ locPopUp: !locPopUp }).

🔸 Navigate( Screen2, None, { param: value } ) passes a context variable into the next screen, similar to passing parameters between screens.

🔸 If a context variable and a global variable share a name, the context variable wins. Disambiguate the global with [@name].

Collections: Collect / ClearCollect

🔸 A collection is an app-scoped table you can add to, clear and modify row by row (Collect, ClearCollect, Clear).

🔸 Use collections for local or temporary tables: a shopping cart, staged edits, or data cached with SaveData/LoadData for offline use.

Beyond variables: With() and Named Formulas

🔸 With( { x: ... }, formula ) creates a value that exists only inside that one formula. It’s a good fit for computing something once and reusing it within the same expression, without a screen- or app-level variable.

🔸 Named Formulas (App → Formulas) define reusable, named values that recalculate automatically whenever their inputs change. They’re a good fit for derived values, for example nfSupervisor = LookUp(Employees, Email = gvUserEmail).Supervisor. Unlike Set, you don’t have to remember to refresh them, they behave like reactive constants, keeping logic in one place.

Which one should I use?

flowchart TD
    A["Need to store or reuse a value?"] --> B{"Can a plain formula<br/>do it directly?"}
    B -->|"Yes"| C["Just use the formula"]
    B -->|"No"| D{"Reusable, derived from<br/>other data, should stay current?"}
    D -->|"Yes"| E["Named Formula"]
    D -->|"No"| F{"Needed only inside<br/>one formula?"}
    F -->|"Yes"| G["With()"]
    F -->|"No"| H{"Scope needed?"}
    H -->|"Whole app"| I["Global variable: Set"]
    H -->|"One screen"| J["Context variable: UpdateContext"]
    H -->|"A table you build/edit"| K["Collection: Collect"]

🔸 Can a plain formula do it? Use a formula, it always stays current.

🔸 Reusable derived value? Use a Named Formula.

🔸 One-off value inside a single formula? Use With().

🔸 State needed across the whole app? Use a global variable (Set).

🔸 State for just one screen, or passing to the next screen? Use a context variable.

🔸 A table you build and edit locally? Use a collection.

⚠️ Watch out

🔸 Don’t over-use variables. Context variables in particular don’t recalculate automatically and can make an app harder to understand; Microsoft’s own guidance is to prefer formulas.

🔸 Type is locked by first use. Set(X, 1) then Set(X, "Hello") errors, since all definitions must agree on type.

🔸 Reserved names to avoid: Width, Height, Theme, SizeBreakpoints, ActiveScreen, and similar names that collide with app properties.

🛠️ FAQ

Set vs UpdateContext, what’s the real difference? Set makes a global variable (all screens); UpdateContext makes a context variable (one screen). Use global for app-wide state, context for screen-local state.

How do I pass a value to another screen? Use Navigate( TargetScreen, None, { param: value } ), which sets a context variable on the target screen.

When should I use a Named Formula instead of Set? When the value is derived from other data and should stay current. Named Formulas recalculate automatically, so you avoid stale Set values.

Do variables survive app restart? No. All variables reset to blank when the app closes. Persist with Patch/Collect (data source) or SaveData (device).

🔸 Power Apps app types, where Power Fx applies.

🔸 Tip #10, Use all variables in Power Apps.

🔸 All Power Apps tips · Knowledge base overview

Details reflect Microsoft Learn at time of writing. Always confirm current behavior in the Microsoft Power Fx / Power Apps documentation.