How to Set Global Variable in Power Apps?

Are you new to Power Apps? Then, are you facing difficulties setting a global variable in Power Apps? If Yes! I am here.

Microsoft provides lots of features in Power Apps to build an attractive application. We can easily use global variables to access the same data in multiple functions without errors.

This Power Apps tutorial will help you briefly understand “How to Set Global Variable in Power Apps.”

Also, you will learn How to update a global variable in Power Apps and many more:

  1. Clear the Power Apps global variable
  2. Set Multiple Global Variables in Power Apps
  3. How to set the Global variable in the Power Apps table

Power Apps Global Variable

Power Apps Global variables can store information using a Set() function. Also, we can use this variable in the entire Power Apps app.

Global variables can be referenced for all Power Apps screens; they can hold any value, including string, number, boolean, record, table, etc.

Syntax of Global Variable in Power Apps:

Set(Variable, Value)

Where,

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

Set Global Variables in Power Apps

Let’s check how to set a global variable in a Power Apps app in the OnStart property.

Example:

Whenever a user navigates to or opens the Power Apps screen, the default background color of the screen should be displayed as “Grey,” as shown below:

Note,

You can use this variable in the entire Power Apps app’s screen.
Set global variable in Power Apps

Let me show you how to achieve it:

  • Open your Power Apps with respective Microsoft credentials.
  • Create Power Apps Canvas app -> Click on the App object [From left navigation] -> Set its OnStart property to:
Set(
    varBackground,
    Color.LightGray
)

Where,

  1. varBackground = Provide the variable name
  2. Color.LightGray = Provide the background color name you want to display on the screen.
How to set Power Apps global variable
  • 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
  • Select the Power Apps Screen -> Set its Fill property to:
varBackground

Where,

  1. varBackground = Now, refer to the variable name here.
Set a Power Apps global variable
  • Run OnStart, Save, Publish, and Preview the app. The Power Apps screen will display the background color as “Grey.”
Power Apps set global variable

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

Update Global Variables in Power Apps

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

Example,

As shown before, we have set the screen’s background color on the Power Apps screen as “Grey.”

But whenever users need to change the background color to “AntiqueWhite,” they can click on a Reload icon.

Power Apps update a global variable

You can achieve it by following the below steps:

  • On the Power Apps screen, insert a Reload icon -> Set its OnSelect property to:
Set(
    varBackground,
    Color.AntiqueWhite
);

Where,

  1. varBackground = Name of the global variable
  2. AntiqueWhite = It is an update value.
How to update Power Apps global variable
  • Save, Publish, and Preview the app. Whenever the user clicks on a reload icon, the background color will change to “AntiqueWhite.”
Update a Power Apps global variable

This is how I worked on updating the Power Apps Global Variables.

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.

Example:

As shown before, we have successfully set a global variable to set the screen’s background color. However, if users prefer the original background color, they may click the “Cancel” icon to revert to the default setting.

How to Clear Power Apps Global Variables

Follow the below steps to achieve it:

  • Insert a Cancel icon -> Set its OnSelect property to:
Set(
    varBackground,
    Blank()
);
Power Apps clear the Global Variables
  • Save, Publish, and Preview the app. Users can click the “Cancel” icon whenever they need the original background color.
How to Clear Global Variables in Power Apps

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

Set Multiple Global Variables in Power Apps

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

Example:

On the Power Apps screen, I have set multiple global variables, i.e., [varBackground], [varHeader], [varUserEmail], and [varUserName]. These variables display the default background color, header, and user name whenever a user navigates to the screen, as shown below:

Set Multiple Global Variables in Power Apps

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

  • On the Power Apps app -> Click on the App’s OnStart -> Set its property code like below:
Set(
    varBackground,
    RGBA(
        241,
        244,
        249,
        1
    )
);
Set(
    varHeader,
    RGBA(
        220,
        153,
        153,
        1
    )
);
Set(
    varUserEmail,
    User().Email
);
Set(
    varUserName,
    User().FullName
)

Where,

  1. varBackground = First Global Variable Name
  2. RGBA(241,244,249,1)) = Color Value
  3. varHeader = Second Global Variable Name
  4. RGBA(220,153,153,1) = Color Value
  5. varUserEmail = Third Global Variable name
  6. User().Email = User Email Value
  7. varUserName = Fourth Global Variable Name
  8. User().FullName = User Full Name Value
How to set Multiple Global Variables in Power Apps
  • Click on the Run OnStart button under the App object.
How to set Power Apps Multiple Global Variables
  • Then, select any Power Apps screen -> Insert a Text label and set its Text property as:
varUserName

Where,

  1. varUserName = Name of the global variable that stores the logged-in user name.
Set Power Apps Multiple Global Variables
  • Similarly, If you want to change the background color of the screen -> Select the screen -> Set its Fill property to:
varBackground
Set the Multiple Global Variables in Power Apps
  • Select the Text label -> Set its Fill property to:
varHeader             
How to set the Multiple Global Variables in Power Apps
  • Save, Publish, and Preview the app. The text label will display the background color, header, and user name whenever a user navigates to the screen.
Power Apps Set Multiple Global Variables

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

Set Global variable in Power Apps table

To set a global variable in the Power Apps table, follow the below steps:

Example,

I added a button control on my Power Apps screen and created a table using the global variable. This table contains the below columns and rows.

IssueIssue Handled ByStatusIssue Raised By
Network IssueBlairCompletedJohn Smith
Laptop HangingChuckCancelledSerena
Inability to access EmailJohanna LorenzOn-goingEmily
Laptop turns on and off repeatedlyLidiaCompletedLily

Then, whenever the user clicks on a Button control, the Data table will display the global variable records [varIssue].

Power Apps set Global variable in table

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

  • On the Power Apps screen -> Insert a Button control -> Set its OnSelect property to:
Set(
    varIssue,
    Table(
        {
            Issue: "Laptop Hanging",
            'Issue Handled By': "Chuck",
            Status: "Cancelled",
            'Issue Raised By': "Serena"
        },
        {
            Issue: "Network Issue",
            'Issue Handled By': "Blair",
            Status: "Completed",
            'Issue Raised By': "John Smith"
        },
        {
            Issue: "Inability to access Email",
            'Issue Handled By': "Johanna Lorenz",
            Status: "On-going",
            'Issue Raised By': "Emily"
        },
        {
           Issue: "Laptop turns on and off repeatedly",
            'Issue Handled By': "Lidia",
            Status: "Completed",
            'Issue Raised By': "Lily"
        }
    )
)

Where,

  1. varIssue = Global variable name
  2. Issue, ‘Issue Handled By,’ Status, ‘Issue Raised By’ = Table Headers/ Columns
Set Global variable in Power Apps table
  • Insert a Data Table -> Set its Items property as:
varIssue
How to set Global variable in Power Apps table
  • Save, Publish, and Preview the app. When the user clicks a button control, the data table will display the global variable records [varIssue].
Set Power Apps Global variable in table

This is how to set a global variable in the Power Apps table.

Conclusion

I trust this Power Apps tutorial explained easily to set a global variable in Power Apps Using a global variable in Power Apps allows you to access the same data in multiple functions without errors easily.

By this tutorial, you have learned how to set a global variable in Power Apps and how to update or clear the Power Apps global variable.

Moreover, I have covered how to set multiple global variables in Power Apps and created a table using the global variable.

You may also like: