How to create variables in Power Apps: Set and UpdateContext
Variables in Power Apps store temporary values — a user's name, a total, or a yes/no state — so you can reuse them across an app. There are two main types, global variables and context variables, and knowing when to use each avoids bugs and keeps the app easier to maintain.
Prerequisites
- An account with access to Power Apps (Maker Portal at make.powerapps.com).
- A new or existing Canvas App where you can experiment.
- Basic familiarity with the interface: adding buttons and labels to a screen.
Step 1: Create a global variable with Set
The Set function creates a global variable, available on every screen of the app. Add a button to the screen, select it and, in the OnSelect property, type the code below. When you click the button (or Alt-click it in the editor), the variable comes into existence.
Set(varNomeUtilizador, "Ana Silva")
You don't need to declare the variable beforehand: it is born the moment you use it. The first value you assign also sets its type (text, number, boolean), so keep the same type throughout the app.
Step 2: Show the value on a screen
Add a label and set its Text property to the variable name. As soon as the button from Step 1 is clicked, the label shows the stored content.
varNomeUtilizador
Because it is global, you can place this same label on any other screen and the value still appears — you don't need to set it again.
Step 3: Create a context variable with UpdateContext
Context variables exist only on the screen where they are created. Use them for local state, such as showing or hiding a panel. The UpdateContext function takes a record with the name and value inside curly braces.
UpdateContext({ locMostrarDetalhes: true })
A common convention is to prefix global variables with "var" and context variables with "loc" (for "local"), so you can tell them apart at a glance when reading formulas.
Step 4: Toggle a value
For a button that shows and hides something, flip the boolean with the Not function. Put this code in a button's OnSelect and bind a panel's Visible property to the locMostrarDetalhes variable.
UpdateContext({ locMostrarDetalhes: Not(locMostrarDetalhes) })
Each click inverts the value: from false to true and back. This is the classic "Show more / Show less" button pattern.
Step 5: Pass a value between screens with Navigate
The Navigate function changes screen and can, at the same time, create a context variable on the destination screen. This is handy for sending, for example, the item selected in a list.
Navigate(EcraDetalhe, ScreenTransition.Cover, { locItem: Gallery1.Selected })
On EcraDetalhe, the locItem variable already exists and you can use it in labels to show the chosen record's data.
Step 6: Clear a variable
To reset a variable, assign it the value Blank(). This is useful, for example, when closing a form and you want to erase what was stored.
Set(varNomeUtilizador, Blank())
For context variables the principle is the same with UpdateContext({ locMostrarDetalhes: false }).
Verify the result
Preview the app with F5 (or the Play button). Click the button from Step 1 and confirm the label shows "Ana Silva". Test the toggle from Step 4 and watch the panel appear and disappear. In the editor, open the Variables pane (the variables icon in the left menu) to see all global and context variables and their current values.
Conclusion
You now know how to create global variables with Set, context variables with UpdateContext, and pass values between screens with Navigate. The natural next step is to learn collections, with ClearCollect, to store tables of data in memory. One final tip: prefer context variables whenever a value only makes sense on one screen — it keeps the app tidier and easier to debug. Which piece of your app's state would make the most sense to store in a variable?