Power Apps  

How to use variables in PowerApps

Introduction

In Power Apps, a variable is a temporary storage location used to hold data while your app is running. It helps your app remember values such as user input, selected items, calculated results, or control states.

Think of a variable like a container that stores information so you can use it later in your app.

Why Do We Use Variables?

Variables are used to:

  • Store user information (e.g., logged-in user)

  • Control visibility (show/hide popups)

  • Store selected records

  • Save temporary calculations

  • Pass values between screens

Without variables, your app cannot dynamically respond to user actions.

In Microsoft Power Apps, variables are used to store temporary data that your app can use while it’s running. There are three main types of variables:

  • Global Variables

  • Context Variables

  • Collections

Let’s describe each with a clear explanation and practical example.

1️⃣ Global Variables

What is a Global Variable?

A Global Variable is accessible throughout the entire app, across all screens.

How to Create It?

Using the Set() function:

Set(VariableName, Value)

Example

Scenario: Store the logged-in user’s name when the app starts.

Step 1: Add this to App → OnStart

Set(CurrentUserName, User().FullName)

Step 2: Use it in a Label

Set the Label Text property to:

CurrentUserName

Result:

The label displays the logged-in user’s name everywhere in the app.

When to Use

  • Store login info

  • Store selected record

  • App-wide settings

  • Role-based access control

2️⃣ Context Variables

What is a Context Variable?

A Context Variable works only within a single screen. Other screens cannot access it unless passed manually.

How to Create It?

Using UpdateContext():

UpdateContext({VariableName: Value})

Live Example

Scenario: Show a popup when a button is clicked.

Step 1: Add a Button → OnSelect

UpdateContext({ShowPopup: true})

Step 2: Select Popup Container → Visible property

ShowPopup

Step 3: Add Close Button → OnSelect

UpdateContext({ShowPopup: false})

Result:

The popup shows and hides on that screen only.

When to Use

  • Toggle visibility

  • Store temporary screen data

  • Control popups

  • Store filter selection for one screen

3️⃣ Collections

What is a Collection?

A Collection is like a temporary table (multiple rows and columns). It can store multiple records.

How to Create It?

Using Collect() or ClearCollect():

Collect(CollectionName, Record)

or

ClearCollect(CollectionName, DataSource)

Live Example

Scenario: Store selected products in a cart.

Step 1: Button (Add to Cart) → OnSelect

Collect(CartCollection,
{
    ProductName: ThisItem.Title,
    Price: ThisItem.Price
})

Step 2: Add Gallery → Items property

CartCollection

Result:

Each clicked item gets added to the cart list.

Difference Summary Table

FeatureGlobal VariableContext VariableCollection
ScopeEntire AppSingle ScreenEntire App
Create UsingSet()UpdateContext()Collect()
Stores Multiple Records?NoNoYes
Used ForApp-wide valuesScreen logicData storage

Important Notes

Global and Context variables store:

  • Text

  • Number

  • Boolean

  • Record

Collections store:

  • Tables (Multiple records)

  • Can be used like a data source

Real-World Combined Example

Imagine building a Leave Request App in Microsoft Power Apps:

  • Global Variable → Store Logged-in User

  • Context Variable → Show/Hide Leave Form Popup

  • Collection → Store Temporary Leave Requests before submitting

Example:

Set(CurrentUser, User().Email);

UpdateContext({ShowLeaveForm: true});

Collect(LeaveCollection,
{
    StartDate: DatePicker1.SelectedDate,
    EndDate: DatePicker2.SelectedDate,
    Reason: TextInput1.Text
});

Simple Rule to Remember

  • Need value everywhere? → Global

  • Need value on one screen? → Context

  • Need table of data? → Collection

Conclusion

Variables in Power Apps are used to temporarily store data while the app is running. They help control app behavior, manage user input, and make applications interactive.

There are three types:

  • Global variables (app-wide)

  • Context variables (single screen)

  • Collections (store multiple records)