How to Set Context Variable in Power Apps?

Are you aware of how to set context variable in Power Apps? If not, there’s no need to worry!

This Microsoft Power Apps tutorial explains all the information on how to set the context variables in Power Apps using different examples. Such as:

  • Show Power Apps label text using a context variable
  • How to calculate numbers in Power Apps using context variables
  • How to add conditions in Power Apps using local variables
  • Manage Power Apps collection using context variables

Context or Local Variables in Power Apps

  • In Power Apps, we can create a context variable by using the UpdateContext() function to pass the value within the screen.
  • Power Apps context variables can hold different types of information, i.e., [A single Value], [Record], [Table], [An object reference], etc…
  • Except for the “Navigate()” function, context variables are limited to the context of a single screen only. You can’t set or use local variables in entire the Power Apps app.
  • Once your app is closed, the context variable’s value will be lost, and you must be recreated when the app is loaded again.
  • UpdateContext() function has no return value; you can use it only within a screen in the Power Apps app.

Syntax of a Local/Context variable in Power Apps:

UpdateContext({ContextVariable: Value})

Where,

  • UpdateContext = This function can used to create or update context variables in Power Apps
  • ContextVariable = Context Variable Name
  • Value = This value can be assigned to the context variable

Set Power Apps Context Variable to Show Text Label

Here, we will see how to set a Power Apps context variable to show the text label control.

Example:

1. I have created a Power Apps Canvas app with a welcome screen. When a user clicks on the button control, the label text [“Welcome to Power Apps”] will appear as in the screenshot below.

How to Set Context Variables in Power Apps

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

2. Open Power Apps -> Create Power Apps Canvas app -> Select a default screen and rename it as “WelcomeScreen”.

3. Insert a Button control -> Set its OnSelect property code like below.

OnSelect = UpdateContext({TextName: "Welcome to Power Apps"})

Where,

  • TextName = Power Apps Context Variable
  • “Welcome to Power Apps” = Context Variable Value
Show Power Apps label text using a context variable

4. Insert a Text label and set its Text property as:

Text = TextName

Where,

  • TextName = Context Variable Name
Show Power Apps label text using context variable

5. Save, Publish, and Preview the app. Whenever the user clicks on the button control, the text label will display as in the screenshot below.

How to Show Power Apps label text using context variable

This is how to show the Power Apps label text using a context variable.

Calculate Numbers in Power Apps using Context Variables

Next, we will discuss how to calculate numbers in Power Apps using context variables.

Example:

1. Suppose you want to calculate (Sum up) two numbers, i.e., [Enter 1st value] and [Enter 2nd Value] in the Power Apps Canvas app; you can use the context variable to get the “Result” value.

Refer to the below table:

Enter 1st valueEnter 2nd valueResult
10349872021

To work around this example, follow the below steps.

2. On the Power Apps Screen -> Insert two Text inputs, i.e., [txt_Value1] and [txt_Value2] to add numbers.

3. Insert a Button control (Result) -> Set its OnSelect property code like below.

OnSelect = UpdateContext({Result:txt_Value1 + txt_Value2})    

                                  // You can also add other mathematical operaions [-, *, /, %]

Where,

  • Result = Power Apps Local Variable Name
  • txt_Value1 + txt_Value2 = Names of the Power Apps text inputs where we can enter the numbers to sum up
How to Calculate Numbers in Power Apps Using Context Variables

4. Then, insert a Text label [lbl_ShowResult] and set its Text property as:

Text = Result

Where,

  • Result = Context Variable

5. Save, Publish, and Preview the app. Now, you can enter the numbers in text inputs and click on the button control to display the result on the text label.

Set Context Variables in Power Apps

This is how to calculate numbers in the Power Apps using context variables.

Add Conditions in Power Apps using Local Variables

Let’s see how to add conditions in Power Apps using local variables.

Example:

1. I have created a new Power Apps form [Food Order] using different controls. Such as:

  • Text input
  • Image
  • Text label
  • Dropdown, etc…

2. Now, I want to add a warning message [“Please enter the name“] by using a local variable as shown below.

How to add a condition in Power Apps using local variable

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

3. On the Power Apps app-> Select a new screen -> Add respective Power Apps controls to add the data.

4. Select Text input [txt_CustomerName] -> Set its OnChange property code as shown below.

OnChange = If(
    IsBlank(txt_CustomerName.Text),
    UpdateContext({ErrMessage: true}),
    UpdateContext({ErrMessage: false})
)

Where,

  • txt_CustomerName.Text = Text input name
  • ErrMessage = Local/Context variable
Add conditions in Power Apps using local variables

5. Now, insert a Text label [txt_ErrorMessage] and set its Text property and Visible property like below.

Text = "Please enter the name"

Visible = ErrMessage

Where,

  • “Please enter the name” = Text Value
  • ErrMessage = Context variable Name
Add conditions in Power Apps Canvas app using local variables

6. Save, Publish, and Preview the app. When a user enters the value in text input, the error/warning message will disappear, or else it will appear as in the screenshot below.

How to add conditions in Power Apps using context variable

This is how to add conditions in Power Apps using local/context variables.

Manage Power Apps Collection using Context Variable

Lastly, we will see how to manage the Power Apps collection using context variables.

Example:

1. I have created a Power Apps collection named [ExpenditureCollection]. There are two text inputs, i.e., [txt_Expenditure_1] and [txt_Expenditure_1]. Using these text inputs, we can add records to the Power Apps collection and display them on the gallery control.

2. I want to reset or clear the text input values after adding records. For that, I have added a button (Clear) control using context variables.

3. Whenever a user wants to clear the text input values, he/she clicks on the button control to remove the values.

Refer to the below image:

How to Manage Power Apps Collection using Context Variable

To achieve the above example, follow the below steps.

4. On the Power Apps screen -> Insert a Button control [Cancel] -> Set its OnSelect Property as:

OnSelect = UpdateContext({ExpenditureCollection: ScreenTransition.None});
Reset(txt_Expenditure_1);
Reset(txt_Expenditure_2)

Where,

  • ExpenditureCollection = Power Apps Collection Name
  • txt_Expenditure_1, txt_Expenditure_2 = Power Apps Text input Names
Manage Power Apps Collection using Context Variable

5. Save, Publish, and Preview the app. Whenever the user wants to clear the text input values, click on the button control as in the screenshot below.

Manage Power Apps Collection using Context Variables

This is how to manage Power Apps collection using context variables.

Conclusion

I trust this Power Apps tutorial provided complete information on Power Apps context variables with different examples. Such as:

  • Show Power Apps label text using a context variable
  • How to calculate numbers in Power Apps using context variables
  • How to add conditions in Power Apps using local variables
  • Manage Power Apps collection using context variables

You may also like: