How to Work With Power Apps Variables?

As a Power Apps developer, you should know how to use variables in Power Apps apps, especially when working with Canvas apps. Many professionals ask me this question all the time in the webinars. In this tutorial, I will explain how to work with Power Apps variables with examples.

After reading this tutorial, you will be confident using variables in Power Apps canvas apps.

Power Apps Variables

In Power Apps, variables store information that can be reused and manipulated within your app. It can be of any type, such as a number, string, boolean, record, table, etc.

Types of Variables in Power Apps

There are three main types of variables in Power Apps:

  1. Global Variables
  2. Context Variables
  3. Collections

1. Global Variables

Global variables are accessible from anywhere within the Power Apps app. They are used to store data that needs to be accessed across multiple screens. Global variables are created using the Set function.

Syntax:

Here is the syntax to create a global variable in PowerApps.

Set(VariableName, Value)

Example:

Here is an example.

Set(UserName, "John Doe")

In this example, a global variable named UserName is created and assigned the value “John Doe”. This variable can now be accessed from any screen in the Canvas app.

Check out Reset Variables in Power Apps

2. Context Variables

Context variables are used to store data that is specific to a particular screen in the Power Apps app. They are created using the UpdateContext or Navigate function and are only accessible within the screen where they are defined.

Syntax:

Here is the syntax to create a context variable in Power Apps:

UpdateContext({VariableName: Value})

Example:

Here is an example.

UpdateContext({IsVisible: true})

In this example, a context variable named IsVisible is created and assigned the value true. This variable can only be used within the screen where it is defined.

Check out How to Set Control Value in Button Click on Power Apps?

3. Collections

Collections are used to store a table of data in Power Apps. Unlike global and context variables, collections can store multiple records and fields. Collections are created using the Collect or ClearCollect function in Power Apps and can be manipulated using various collection functions.

Syntax:

Here is the syntax to create a collection in Power Apps.

Collect(CollectionName, Data)

or 

ClearCollect(CollectionName, Data)

Example:

Here is an example of how to create a collection in Power Apps.

Collect(Orders, {OrderID: 1, ProductName: "Laptop", Quantity: 2})

or 

ClearCollect(Orders, {OrderID: 1, ProductName: "Laptop", Quantity: 2})

In this example, a collection named Orders is created, and a record is added to it. The record contains three fields: OrderID, ProductName, and Quantity.

Check out Create an App in Power Apps using Copilot

Set Global Variable in Power Apps

Let me show you how to set a global variable in Power Apps. I created a canvas app from scratch for this purpose.

In this example, I will show you how to set a user name using a global variable, and we will display it on any other screen.

On the first screen, add a text input control and a button. In the button’s OnSelect property, add the formula below.

Set(UserName, TextInput1.Text)

Since I want the user to navigate to another screen after this, I added the below formula:

Set(UserName, TextInput1.Text); Navigate(Screen2, ScreenTransition.Fade)

Here, I assign the text input value to a global variable named UserName. You can see it in the screenshot below:

set global variable power apps

On screen 2, I just added a Text label control and the formula below to the Text property.

Text: UserName

Here is the screenshot; you can look at it.

global variable power apps

Now, you can preview or run the app. On the first screen, provide the user name and click the button. On the second screen, you can see the user name, which has been populated using a global variable.

power apps set global variable
How to set global variable power apps

This is how to set a global variable in Power Apps and access the value of the Power Apps global variable.

Check out Create an App from an Excel File in Power Apps

Set Variable to Blank in Power Apps

Sometimes, you need to set the global variable to blank in Power Apps.

To set the global variable to a blank value in Power Apps, you can use the Set function and assign it the Blank() function. This will effectively reset the variable to an empty state.

Here is the formula to set a variable to blank in Power Apps:

Set(<Variable Name>, Blank())

Example: Set(UserName, Blank())

To implement this, I added a button on Screen 2.

  • Go to the Insert tab.
  • Select Button.
  • Set the button’s Text property to something like “Reset” or “Clear”.

Then, with the button selected, go to the Properties pane. Find the OnSelect property and enter the formula:

Set(UserName, Blank())

Here is what the formula looks like in the screenshot below:

power apps set variable to blank

Now, when you run the app and click on the Reset button, the variable’s value will be reset. You can see in the screenshot below that the text label is empty.

power apps set global variable to blank

This is how to set a variable to blank in Power Apps.

Set Context Variable in Power Apps

Now, let me show you how to use context variables in Power Apps. I will use the same Power Apps canvas app.

I am using a text input control, a button, and a Text label on the first screen. When a user enters the user name in the text input control and then clicks on the button control, the user name will be displayed in the text label control.

In this case, we will use the Power Apps context variable.

  • Select the button and then the OnSelect property, and write the formula below.
UpdateContext({varUserName: TextInput1.Text})
  • Then, in the Text label’s Text property, write the variable name that is “varUserName“.
power apps set context variable
  • Now, preview the Power Apps app. You can see it displays the user name when the user clicks on the button.
set context variable in power apps

This is how to set context variables in the Power Apps canvas app.

Set Collection Variable in Power Apps

Now, let me show you how to set collection variables in Power Apps. I will use the same Power Apps canvas app.

Here, we will add items to a collection with a button click and finally bind them to a Power Apps gallery control.

On the screen, add a button control and add a gallery control. Then, select the button and add the formula below to the OnSelect property.

Collect(ShoppingCart, {ProductID: 1, ProductName: "Laptop", Quantity: 1})

It should look like the below screenshot.

power apps set variable

Next, add a gallery control to the screen. Then, select the Gallery control, choose the items property, and assign the variable like the below:

Items: ShoppingCart

Here is the screenshot for your reference.

power apps create variable

Now, run the Power Apps app and then click on the button. I clicked the button twice, and you can see that it displays two items in the Power Apps gallery control.

set variable power apps

This is how to work with Power Apps collection variables.

Set Global Variable OnStart in Power Apps

As a Power Apps developer, sometimes you might need to set global variable on start in Power Apps.

The OnStart property of the app is executed when the app is launched, making it an ideal place to set up initial values for global variables.

Let me show you an example.

Let’s say we want to display a welcome message to the user when they launch the app. We can set a global variable called WelcomeMessage with a default value on app start.

  • In Power Apps Studio, go to the left-hand navigation pane and select the “App” icon. With the “App” selected, go to the Properties pane on the right. Find the OnStart property and enter the following formula.
Set(WelcomeMessage, "Welcome to the Power App!")

Here is what it looks like; check the screenshot below:

power apps set global variable onstart

Now, since this is a global variable, it can be accessed anywhere in the app.

  • Add a label to your screen where you want to display the welcome message. Set the Text property of the label to the global variable WelcomeMessage:
Text: WelcomeMessage

Check out the screenshot below:

powerapps how to set variable on start
  • Before previewing the app, select the App icon and click “Run OnStart”. This will run the OnStart property, which will create the variables we declared.
powerapps set variable example
  • Now, when you preview the Power Apps app, you can see it displays a message like the one in the screenshot below.
powerapps set global variable

This is how to set global variable on start in Power Apps.

Set multiple variables on start in Power Apps

Sometimes, you might required to set multiple variables OnStart in Power Apps.

You can initialize multiple Power Apps global variables in the OnStart property by chaining Set functions together using semicolons. For example:

Set(WelcomeMessage, "Welcome to the Power App!"); Set(UserName, "Guest")

Conclusion

Variables are very useful when working with Power Apps canvas apps. In this tutorial, I explained different types of Power Apps variables. I have shown how to work with global variables, context variables, and collections in Power Apps with examples. Also, we saw:

  • Set Global Variable in Power Apps
  • Set Variable to Blank in Power Apps
  • Set Context Variable in Power Apps
  • Set Collection Variable in Power Apps
  • Set Global Variable OnStart in Power Apps
  • Set multiple variables on start in Power Apps