How to Set Global Variable in Power Apps?

This Power Apps tutorial will explain all the information about how to set global variable in Power Apps. Like:

  • Set a Global Variable in the Power Apps OnStart Property
  • Manage a Power Apps Data Table Using Global Variables
  • How to Set Multiple Global Variables in Power Apps
  • How to Update Global Variables in Power Apps
  • How to Clear Power Apps Global Variables

Power Apps Global Variable

  • Microsoft provides lots of features in Power Apps to build an attractive application. And most of the time, we can use variables as temporary storage in the app.
  • We can use global variables in Power Apps to store a piece of information by using a Set() function. Also, we can use this variable in the entire Power Apps Canvas app.
  • Global variables can be referenced for all Power Apps screens; it can hold any value, including string, number, boolean, record, table, etc…
  • By using a global variable in Power Apps, you can easily access the same data in multiple functions without any errors.
  • When inserting more global variables in Power Apps, you must use multiple Set() functions.

Syntax of Global Variable in Power Apps:

Set(Variable, Value)

Where,

  • Set(): This Power Apps Set() function helps us to set the value of the global variable
  • Variable: Global variable
  • Value: It holds a number, text, record, table, string, boolean, or, an object in the function

Set Global Variables in Power Apps OnStart

Here, we will discuss how to set a global variable in a Power Apps Canvas app in the OnStart property.

Example:

1. I have created a table of records using a global variable [gvProducts] in a Power Apps Canvas app. This table contains the below columns and rows.

ProductNameCategoryPriceColorQuantity
Office ChairOffice1500Brown5
Dining TableHome1000Red10
Lawn & Garden AccessoriesOutdoor1900Green30
Baby FurnitureChildren900Blue15
Bowls and FeedersPet Furniture1000White25

2. Now, I want to display the global variable records [gvProducts] on the Power Apps Data table control.

How to create global variables in Power Apps

To achieve the above example, follow the below steps. Such as:

3. Open your Power Apps with respective Microsoft credentials -> Create Power Apps Canvas app -> Click on the App object [From left navigation] -> Select OnStart and set its property code like below.

OnStart = Set(
    gvProducts,
    Table(
        {
            ProductName: "Office Chair",
            Price: 1500,
            Category: "Office",
            Quantity: 5,
            Color: "Brown"
        },
        {
            ProductName: "Dining Tabel",
            Price: 1000,
            Category: "Home",
            Quantity: 10,
            Color: "Red"
        },
        {
            ProductName: "
Lawn & Garden Accessories",
            Price: 1900,
            Category: "Outdoor",
            Quantity: 30,
            Color: "Green"
        },
        {
            ProductName: "Baby Furniture",
            Price: 900,
            Category: "Children",
            Quantity: 15,
            Color: "Blue"
        },
        {
            ProductName: "
Bowls and Feeders",
            Price: 1000,
            Category: "Pet Furniture",
            Quantity: 25,
            Color: "White"
        }
    )
)

Where,

  • gvProducts = Global variable name
  • ProductName, Price, Category, Quantity, Color = Table Headers/ Columns
  • “Office Chair”, 1500, “Office”, 5, “Brown” = Table Values/ Rows
How to Set Global Variables in Power Apps

4. Then, click on the App object -> Select the More commands option () -> Click on the Run OnStart button like below.

How to Set a Global Variable in Power Apps

5. On the Power Apps Screen -> Insert a Data Table [tbl_ProductDetails] -> Set its Items property as:

Items = gvProducts

6. To display the table fields on the Power Apps data table, click on the Edit fields and select data fields as per your need.

Set Global Variable in Power Apps

7. Run OnStart, Save, Publish, and Preview the app. The Power Apps data table displays all items from the global variable.

power apps set global variable onstart

This is how to set a global variable in the Power Apps using the OnStart property.

Manage a Power Apps Data table using Global variables

Next, we will see how to manage a Power Apps data table using global variables.

Example:

1. I have a Power Apps data table, i.e., [tbl_ProductDetails]. This data table contains the global variable records [gvProducts]. When a user selects a specific record in the data table, he/she will get all the related records on another screen.

Refer to the below image:

power apps set global variable onstart

To do so, follow the below steps. Such as:

2. On the Power Apps screen -> Insert a Data table and set its Items property as:

Items = gvProducts
powerapps where to set global variable

3. Add a new blank screen and rename it to “DetailsScreen” -> Insert a Text label and set its Text property [Product Details].

Manage a Data Table Using the Power Apps Global Variable

4. Now, go back to the previous screen -> Select all columns/fields inside the data table -> Set its OnSelect property as shown below.

OnSelect = Navigate(DetailsScreen)

Where,

  • Navigate() = This function can be used to redirect to another screen
  • DetailsScreen = Second screen
powerapps change global variable value

5. Now, on the second screen -> Insert the Text label to display the respective record values from the data table -> Set its Text property code like below.

Text = tbl_Productdetails.Selected.ProductName

Where,

  • tbl_Productdetails = Data table name
  • ProductName = Selected record

6. Similarly, if you want to display the remaining values of a specific record, i.e., [Category], [Color], [Quantity], and [Price], then set their Text properties as shown below.

Text = tbl_Productdetails.Selected.Category         // For Category Value
Text = tbl_Productdetails.Selected.Color               // For Color Value
Text = tbl_Productdetails.Selected.Quantity        // For Quantity Value
Text = tbl_Productdetails.Selected.Price             //  For Price Value

7. Save, Publish, and Preview the app. The text label controls will display all the records based on the selected record from the Power Apps data table, as in the screenshot below.

power apps change global variable

This is how to manage a Power Apps data table using a global variable.

Set Multiple Global Variables in Power Apps

Let’s see how to set multiple global variables in Power Apps.

Example:

1. I have created another Power Apps screen. On the screen, I have set multiple global variables, i.e., [varBackground], [varHeader], [varUserEmail], and [varUserName].

2. Once you create a global variable, it will apply to the entire app.

set global variable powerapps

To work around this example, follow the below-mentioned steps.

3. On the Power Apps app -> Click on the App’s OnStart and set its property code like below.

OnStart = Set(
    varBackground,
    RGBA(
        241,
        244,
        249,
        1
    )
);
Set(
    varHeader,
    RGBA(
        220,
        153,
        153,
        1
    )
);
Set(
    varUserEmail,
    User().Email
);
Set(
    varUserName,
    User().FullName
)

Where,

  • varBackground = First Global Variable Name
  • RGBA(241,244,249,1)) = Color Value
  • varHeader = Second Global Variable Name
  • RGBA(220,153,153,1) = Color Value
  • varUserEmail = ThirdGlobal Variable name
  • User().Email = User Email Value
  • varUserName = Fourth Global Variable Name
  • User().FullName = User Full Name Value
set multiple global variables powerapps

4. Click on the Run OnStart button under the App object.

5. Then, select any Power Apps screen -> Insert a Text label and set its Text property as:

Text = varUserName

Where,

  • varUserName = Name of the global variable which stores the background color.

6. Similarly, If you want to change the background color of the screen and add color to the header, then set its Fill property as:

Fill = varBackground        // For Screen Color
Fill = varHeader                // For Header Color
set multiple global variables powerapps

This is how to set multiple global variables in the Power Apps Canvas app.

Update Global Variables in Power Apps

Now, I will show you how to update or change a global variable value using the Button click. To do so, follow the below steps.

1. On the Power Apps app -> Click on the Apps OnStart and set its property code like below.

OnStart = Set(
    varScreenName,
    "Success Screen"
)

Where,

  • varScreenName = Global Variable Name
  • “Success Screen” = Variable Value
set global variable on start powerapps

2. Select a Power Apps Screen -> Insert a Text label and set its Text property as:

Text = varScreenName

Where,

  • varScreenName = Global Variable Name
Update a Global Variable in Power Apps

3. Then, insert a Button control and set its OnSelect property code like below.

OnSelect = Set(
    varScreenName,
    Blank()
)

Where,

  • varScreenName = Name of the global variable
  • Last Screen = It is an update value

4. Save, Publish, and Preview the app. Once you click on the button control, you will get an updated value on a text label, as shown below.

Update Global Variables from Power Apps

This is how to update a global variable value in Power Apps.

How to Clear Power Apps Global Variables

Whenever you want to clear or remove a global variable from the Power Apps, you must set the variable value using the Blank() function. To achieve it, follow the below-mentioned steps. Such as:

1. On the Power Apps screen -> Insert a Button control and set its OnSelect property as:

OnSelect = Set(varScreenName,Blank())

Where,

  • varScreenName = Global Variable Name
  • Blank() = This Power Apps Blank() function is used to remove the global variable

2. Save, Publish, and Preview the app. When a user clicks on the button control, it will remove the global variable value on the text label as in the screenshot below.

How to Clear Power Apps Global Variable

This is how to clear the global variables in the Power Apps.

Conclusion

Whenever you want to use a variable in the entire Power Apps app, you can set a global variable to store the information in any value like string, record, table, text, etc…

This Power Apps tutorial explained how to set a global variable in Power Apps. Here, we learned how to set global variables from the Power Apps OnStart property and manage the Power Apps data table using global variables.

Also, we discussed how to set multiple global variables in Power Apps and how to update & clear global variables in Power Apps.

You may also like: