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.
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 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())
Become a Power Automate Certified professional by learning this HKR Power Automate Training
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 !
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 !
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:
Batch starts on 5th Apr 2023, Weekday batch
Batch starts on 9th Apr 2023, Weekend batch
Batch starts on 13th Apr 2023, Weekday batch