Power Platform Variables and Collections
An app needs memory. A user types a name on screen one, and screen three must still know it. Variables and collections give a canvas app that memory.
Why Memory Matters
Canvas apps forget everything when a screen closes unless you store values deliberately. Variables hold single values such as a number, a name, or a true/false switch. Collections hold whole tables of rows that live inside the app.
Three Kinds of Variables
Global Variables
A global variable works on every screen. Create one with the Set function.
Set(gblUserName, "Priya") Set(gblTaxRate, 0.18)
Any label can now show gblUserName, and any formula can use gblTaxRate.
Context Variables
A context variable lives on one screen only. Create it with UpdateContext, and pass a record of names and values.
UpdateContext({locShowPopup: true})
UpdateContext({locCount: locCount + 1})
Context variables suit small jobs such as showing a pop-up or counting clicks on a single screen.
Named Formulas
Named formulas live in the App object under the Formulas property. They recalculate automatically whenever their inputs change, so you never need to refresh them by hand.
TotalWithTax = Sum(colCart, Price * Qty) * 1.18;
Collections
A collection is a small table stored in the device memory. Collect adds rows, ClearCollect replaces everything, and Clear empties the table.
ClearCollect(colCart, {Item: "Pen", Price: 10, Qty: 2});
Collect(colCart, {Item: "Book", Price: 120, Qty: 1});
Remove(colCart, ThisItem);
Clear(colCart)
Bind a gallery to colCart and the screen shows the shopping cart instantly.
Diagram: Where Values Live
+---------------- APP MEMORY ----------------+ | | | Global variable gblUserName --> ALL screens | | | +-- Screen: Home ---------------------+ | | | Context variable locShowPopup | | | +-------------------------------------+ | | | | Collection colCart (rows and columns) | | | Item | Price | Qty | | | | Pen | 10 | 2 | | | | Book | 120 | 1 | | +--------------------------------------------+
Layman Example: A Restaurant Order Pad
A waiter carries a pad. The table number written at the top stays true for the whole meal, much like a global variable. A quick note saying "customer is waiting for water" matters only at that table, much like a context variable. The list of dishes on the pad is the collection, and the waiter adds or crosses out lines as the guests decide.
Naming Rules That Save Time
| Type | Prefix | Example |
|---|---|---|
| Global variable | gbl | gblCurrentUser |
| Context variable | loc | locIsEditing |
| Collection | col | colSelectedItems |
Prefixes tell every reader where a value lives the moment they see its name.
The With Function
With creates a temporary named value inside a single formula. It keeps long expressions readable.
With({subtotal: Sum(colCart, Price * Qty)}, subtotal + subtotal * 0.18)
Best Practices
- Prefer named formulas for calculated values that depend on other data.
- Use context variables for screen-level toggles to keep global memory small.
- Load only the columns and rows you need into a collection.
- Clear collections that hold sensitive data when the user signs out.
- Check the Variables view under the View tab to inspect every value while testing.
Common Mistakes With Memory
Many beginners store everything in global variables and lose track of values. Others forget that collections vanish when the app closes. Save anything permanent to a data source with Patch or SubmitForm. Test the app after restarting it, because values from your last session may hide missing initialization steps in the App OnStart or the first screen.
Choosing the Right Tool
Ask three questions before creating any value. Does the value need to survive across screens? Does it hold one item or many rows? Does it calculate from other data? Single values on one screen call for a context variable. Single values used everywhere call for a global variable. Many rows call for a collection. Calculated values call for a named formula. Follow that short checklist and your apps stay predictable, small, and easy for teammates to understand.
Key Takeaways
Global variables travel everywhere, context variables stay on one screen, and collections hold rows. Choose the smallest scope that solves the problem and the app stays fast and easy to read.
