The Logic of Variables: Global vs. Context
In Power Apps, managing state is efficient only if you choose the right tool for the job. While both Global and Context variables store data, they function differently in terms of scope, performance, and lifecycle.
Think of them this way:
- Global Variables are like a Public Announcement System: Everyone in the building (App) hears the message.
- Context Variables are like a Whisper: Only the people in the room (Screen) can hear it.
1. Global Variables (Set)
Global variables are accessible from any screen in your app. They persist for the entire session until the app is closed or the variable is explicitly blanked.
- Syntax:
Set( varUserEmail, User().Email ) - Scope: App-wide.
- Best Use Cases:
- User Profile Data: Storing the current user’s name, email, or photo to use on multiple screens.
- App Settings: Toggles for “Dark Mode” or specific admin configurations loaded OnStart.
- Cached Data: Storing a small table of data (like a list of statuses) that doesn’t change often, to avoid repeated API calls.
⚠️ Warning: Overusing global variables can make your app difficult to debug because the value could be changed from anywhere. It also keeps data in memory for the entire session, which can impact performance on mobile devices.
2. Context Variables (UpdateContext)
Context variables are scoped strictly to the screen where they are created. If you navigate to another screen, the variable effectively “doesn’t exist” there (unless passed as a parameter).
- Syntax:
UpdateContext( { locShowPopup: true } ) - Scope: Screen-level only.
- Best Use Cases:
- UI Logic: Showing/hiding pop-ups, loaders, or toggling tabs on a specific screen.
- Form States: Holding a temporary value for a calculation before patching it to a datasource.
- Navigation Parameters: Passing a specific record from a gallery to a detail screen using
Maps(DetailScreen, ScreenTransition.None, { ctxRecord: ThisItem }).
3. The Decision Matrix
| Feature | Global Variable (Set) | Context Variable (UpdateContext) |
|---|---|---|
| Scope | Entire App | Current Screen Only |
| Duration | App Session | Screen Lifecycle (mostly) |
| Creation | Set( Name, Value ) | UpdateContext( { Name: Value } ) |
| Navigation | Value persists across screens | Value is isolated (unless passed) |
| Ideal For | User Info, App Config, Caching | Pop-ups, Toggles, Input handling |
4. Best Practices for “Clean” Code
Naming Conventions are Critical:
- Prefix Global variables with
gbl_orvar_(e.g.,gbl_UserEmail). - Prefix Context variables with
loc_orctx_(e.g.,loc_IsLoading). - Why? This lets you instantly know the scope of a variable just by reading its name in the formula bar.
Prefer Context Over Global:
- Always default to using a Context variable. Only “promote” it to a Global variable if you absolutely need that data on a different screen. This keeps your memory usage low and your app logic self-contained.
Passing Data:
- Avoid setting a Global variable just to get data to the next screen. Instead, use the third argument of the
Mapsfunction to pass it as context. - Bad:
Set( gbl_SelectedRecord, ThisItem ); Navigate( DetailScreen, ... ) - Good:
Maps( DetailScreen, Fade, { ctx_SelectedRecord: ThisItem } )
5. Deep Dive Video
For a practical walkthrough on the nuances of these variables, this video is excellent.
Power Apps Variables: Global vs. Context and what you need to know This video by PowerApps911 (Shane Young) visually demonstrates the scope differences and common pitfalls when mismatching variable types.