Power Apps
Power Apps,  Power Platform

How to use Variables in PowerApps?

Each programming language uses variable and variables play very important role in any programming language. In PowerApps we use variables little bit in different way. Here we do not define type of variable but we use them through declaration.

In PowerApps, There are three types of variables, and each type of variable has its own scope.

[su_table responsive=”yes”]

Variable Type Scope Function
Global App Set()
Context (Local) Screen UpdateContext({:});Navigate()
Collection App Collection();ClearCollection()

[/su_table]

Important Points-

  1. We do not declare variables explicitly here as we do in other programming languages. 
  2. None of these functions create variables, they only assign value to variables.
  3. All variables hold value in memory while the app runs. Once the app is closed, the variable loses its value.
  4. You can remove variables by removing all functions- Set(), UpdateContext(), Navigate(), Collect() & ClearCollect().
  5. Based on variable context, use variable name to read its value anywhere in your app.
  6. If your context variable, global variable & Collection has the same name then Context variable will take precedence. If you still want to access your global variable or collection, then use the disambiguation operator [@Variable].
  7. Variables can hold any value, including strings, numbers, records, and tables.

How to see variables in PowerApps?

To see variables, Go to File menu and Select variables in Left-hand pane- 

Global Var

Local Var

If you are using a global variable, then select the Global tab. And if you are using a context variable which will be available under screen1(screen default name) tab.

To show all the places where the variable is defined and used, please click on your variable name. In above screen if I will click on VarGlobal/VarLocal then I can see its detail-OnClick_GlobalVar

OnClick_LocalVar

When to use variable-

As a storage of value/records: In Variable you can store value and records which can be used later for calculation or series of actions.

Enable/Disable/Show/Hide controls: we use variable(boolean-type) to enable or disable control in our Apps. Example- when we design accordion in PowerApps we use lots of variable of boolean type and reset their value on subsequent clicks.

How formulas work in Canvas apps-

Formulas are used to calculate a value and recalculation happens automatically as value changes.

Actions are performed in the order in which they appear in the formula.The next function won’t start until the current function has completed. We use semicolons to create a list of actions to perform.

UpdateContext({VarLocal:1});Set(VarGlobal,0)

Functions like Back and Navigate,UpdateContext,Refresh, Update, UpdateIf, Patch, Remove, RemoveIf ,Collect, Clear, ClearCollect etc  change the state of the app, they can’t be automatically recalculated. You can use them in the formulas for the OnSelect, OnVisible, OnHidden, and other On… properties, which are called behavior formulas.

 

This is the basis of the PowerApps variable. In the next article I will explain each variable in detail.

-Gopenly

Leave a Reply

%d bloggers like this: