In this tutorial, you’ll learn how to create and manage variables in Power Apps using two essential functions—Set
and UpdateContext
. These functions help you store and reuse data across screens or within a single screen, depending on the type of variable you choose.
What Are Variables in Power Apps?
Variables in Power Apps allow you to store data temporarily while your app is running. There are two main types:
- Global Variables – Created using
Set()
, accessible across multiple screens. - Context Variables – Created using
UpdateContext()
, limited to the screen where they are defined.
Creating a Global Variable with Set()
Global variables are useful when you need to share data across different screens. Here’s how to create one:
- Insert a button into your app.
- Change its text to Set Variable.
- In the OnSelect property, enter the following formula:
Set(VAR_day, "Monday")
This sets the value "Monday"
to the global variable VAR_day
.
Displaying the Global Variable
- Insert a text label.
- Set its Text property to:
VAR_day
When you preview the app and click the button, the label will display Monday
.
Creating a Context Variable with UpdateContext()
Context variables are scoped to the screen where they are created. Here’s how to define one:
- Insert another button and label it Update Context Variable.
- In the OnSelect property, use this formula:
UpdateContext({VAR_month: "November"})
This creates a context variable VAR_month
with the value "November"
.
Displaying the Context Variable
- Insert a text label.
- Set its Text property to:
VAR_month
Clicking the button will display November
in the label—but only on the current screen.
Testing Variable Scope Across Screens
To understand the difference in scope:
- Create a new screen (e.g., Screen2).
- Add two labels:
- One with
Text = VAR_day
- One with
Text = VAR_month
- One with
When you preview the app:
- VAR_day will display correctly—it’s global.
- VAR_month will show an error—it’s local to Screen1.
Summary Table
Feature | Set() (Global) | UpdateContext() (Local) |
---|---|---|
Scope | All screens | Current screen only |
Syntax | Set(VAR_name, value) | UpdateContext({VAR_name: value}) |
Use case | Shared data across app | Temporary screen-specific data |
Final Tips
- Use
Set()
for data that needs to persist across screens. - Use
UpdateContext()
for screen-specific logic or temporary values. - Always preview your app to test variable behavior.
Leave a Comment