PowerApps Variables

Generally, in tools like Visual basics, we perform calculations explicitly and store the results in the variables. But this is not the case in Power Apps. As the input data changes, PowerApps recalculates the formulas. So, there is no need for creating and updating variables in PowerApp. So, we will be able to understand, create and maintain your App in PowerApps whenever possible. Do not use variables; However, sometimes variables only can give you the desired experience. Sometimes, we may use variables in Power Apps that will extend the model of Excel by adding behavior formulas. These formulas will run when a user performs some action. For example, the formula runs when the user selects a button. In a behavior formula, it is frequently useful to define a variable to be used in other formulas. In this blog, let us go through Power Apps variables in detail.

Powerapps variables

A variable is a temporary storage that can be defined and used anywhere within Power Apps. Variables are created and typed by default when they are displayed in the functions which define their values. Three types of Variables are available in PowerApps. They are:

Become a PowerApps Certified professional by learning this HKR PowerApps Training !

Global Variables:

Global variables are the single row variables which are available throughout the PowerApps App. They are similar to global variables in programming languages. Global variables hold text string, number, table, record, boolean, etc., as the data value. The scope of Global variables in PowerApps is within the App. So, we can use global variables within all the screens in our entire Application. To create a global variable, we just need to run the following function:

Set(global_variable, “Example”)

Here global_variable is the variable name, and “Example” is the value of that variable. 

The way we store a record in the global variable works similarly to how we store it in the local variable: 

Set(varGlobalRecord, {User:”ABC”, ID:”12345”})

It is not possible to define more than one global variable in only one Set() command. If we want to set more global variables, we must use multiple Set() functions.

Set(varGlobalOne, “First global variable”)

Set(varGlobalTwo, “Second global variable”)


We cannot delete the variables. For clearing the global variable, we must set the variable value using the Blank() function.

Set(GlobalVar, Blank())

How to create a global variable?

Button

Global variables

  • First, select the Canvas App from blank as the PowerApps type. 
  • Enter the name of the Application as GlobalVariable and choose the Tablet format.
  • From the insert table, add a label, text input, and a button.
  • Now select the button and modify the formula in OnSelect.
  • Set(Global_Variable, FirstInput.Text)
  • Then Choose the label field and modify the formula in the text field.
    Global_Variable
  • In order to check all the global variables in PowerApps, go to file, select variables. It will show all the global variables that are available in the Application. To verify all references, we need to click on the variable. That would show where the definition of the variable exists and where it will be used.
  • This Application will update the value of the variable as entered into the text field and accordingly display it in the label.

Become a Power Automate Certified professional by learning this HKR Power Automate Training

Powerapps Training

  • Master Your Craft
  • Lifetime LMS & Faculty Access
  • 24/7 online expert support
  • Real-world & Project Based Learning

Context Variables:

Context variables in PowerApps are similar to the parameters that we pass to the methods in the programming languages. Variables may be referenced through a single screen. The scope of Context variables in PowerApps is within the screen. To create a Context variable, we just need to run the following function:

Update Context({context_variable: ”Example”})

Here context_variable is the variable name, and “Example” is the value of that variable. Variables should be wrapped in curly braces. In curly braces, you have to give the variable with a name followed by a colon and then the actual value of the variable. The value of the String must be wrapped in quotation marks, and the integer values don’t require quotation marks.

When you need to store a record in the variable, you must wrap the value in other curly braces where you give the column name, then a colon, and the column value.

Update Context({context_variable: {UserName:”ABC”, ID:12345}})

We may also store a predefined record in the variable like user() attribute:

Update Context({contextVariableRecord: user()})

For accessing a particular property from a record, we can call that variable followed by a dot then the property.

varUser.UserName

More than one variable can be set at a time. A comma separates them within the curly braces. 

Update Context({context_variableOne: ”first one”, context_variableTwo: ”second one”})

For clearing the Context variable, we must set the variable value using the Blank() function.

Update Context({LocalVar, Blank()})

Want to know more about PowerApps,visit here PowerApps Tutorial !

How to create a Context variable?

Variables

  • First, select the Canvas App from blank as the PowerApps type. 
  • Enter the name of the Application as ContextVariable and choose the Tablet format.
  • From the insert table, add a label, text input, and a button.
  • Now select the button and modify the formula in OnSelect.
  • Update Context({context_variable:FirstInput.Text})
  • Then Choose the label field and modify the formula in the text field.
    Context Variable
  • In order to check all the Context variables in PowerApps, go to file, select variables. It will show all the screens that are available in the Application. To verify all the context variables in the screen, we need to select the screen. That would show where the definition of the variable exists and where it will be used.
  • This Application will update the value of the variable as entered into the text field and accordingly display it in the label.

Subscribe to our youtube channel to get new updates..!

Collections: 

Collections in PowerApps are the variables that are used to store tables that can be referenced within the Application. They are the multi-row valued variables. We may view them as tables or arrays. The scope of Collections in PowerApps is within the App. So, they can be used in the whole PowerApp Application. To create a collection, we just need to run the following function:

Collect(collect_variable, “Example”)

Here collect_variable is the variable name, and “Example” is the value of that variable. 

You can also set a collection using ClearCollect(). ClearCollect() is also used to clear the whole content in the Collection if it is already defined. 

ClearCollect(CollectionCol, {UserName: “ABC”, email:”abc@gmai.com”})

Here CollectionCol is the collection name, and the rows are given within the curly braces. Every column must be given with a name and mail id, and the values “ABC” and “abc@gmail.com” are written within the double-quotes. 

When we want to add more rows to the Collection, we must add a comma after the first row. Another row must be added within the curly braces. 

ClearCollect(CollectionCol, 

{UserName: “ABC”, email:”abc@gmai.com”},

{UserName: “EFG”, email:”efg@gmai.com”}

)

We may also store a predefined record in the collection row like user() an attribute or in filtered or entire SharePoint list:

ClearCollect(CollectionCol, user())

For removing a row from the Collection, we must use the RemoveIf() function.

RemoveIf(UserCol, email-ThisItem.email) 

Top 30 frequently asked PowerApps Interview Questions !


How to create a Collections variable?

Collections variable

  • First, select the Canvas App from blank as the PowerApps type. 
  • Enter the name of the Application as Collection Variable and choose the Tablet format. 
  • From the insert table, add a data table, five text inputs, and two buttons.
  • Now select the button and modify the formula in OnSelect and modify the text to Submit Collection.
  • Collect(formData Temp, {name:InputText1.Text, temp1:InputText2.Text, temp2:InputText3.Text, temp3:InputText4.Text, temp4:InputText5.Text})
  • Then Choose the Data table and modify the formula in the Items field.
    formDataTemp
  • For clearing the data from Collection, choose the second button and edit the text to clear data, and modify the OnSelect field with
    clear(formDataTemp)
  • To verify all collection variables in PowerApps, go to File and click Collections. It will show all the collection variables that are available in the Application. That would show the first five items from the collections variables.
  • This Application will update the value of the variable as entered into the text field and accordingly display it in the data table. We can click on the Clear Data button in order to clear data.

Powerapps Training

Weekday / Weekend Batches

Conclusion:

In this blog, we have learned about PowerApps Variables, types of variables in PowerApps, and creating global variables, Context variables, and collections. We hope you found this information useful! If you are looking for another topic associated with PowerApps, comment on it in the comments section. For more blogs like this, keep an eye out for HKR Trainings. 

Related Article:

Find our upcoming Powerapps Training Online Classes

  • Batch starts on 5th Apr 2023, Weekday batch

  • Batch starts on 9th Apr 2023, Weekend batch

  • Batch starts on 13th Apr 2023, Weekday batch

Global Promotional Image
 

Categories

Request for more information

Amani
Amani
Research Analyst
As a content writer at HKR trainings, I deliver content on various technologies. I hold my graduation degree in Information technology. I am passionate about helping people understand technology-related content through my easily digestible content. My writings include Data Science, Machine Learning, Artificial Intelligence, Python, Salesforce, Servicenow and etc.

Protected by Astra Security