Variables In PowerApps

Introduction

With the introduction of PowerApps, more and more users are moving into PowerApps development for easy and no-code development tools. However, users from a coding background found it difficult, as there were no variables in PowerApps. Not now, with the introduction of variables in PowerApps, it has become a Powerful no-code tool. So let's understand PowerApps Variables and how they work.

Types of Variables

Just as we have different scopes of variables in the programming language in PowerApps, we also have different scoped variables for the three different types of variables that are available in PowerApps, as mentioned below.

Variables types Scope Description Functions related to variable
Global variable App This is similar to a global variable in programming language and it can hold a number, text string, Boolean, record, table, etc as a data value Set
Context variables Screen This is similar to the parameter which we pass to methods or procedures and this variable can be referenced from only one screen UpdateContext Navigate
Collections App This variable can store a table which can be referenced anywhere in the app Collect, ClearCollect


Creating and Removing Variables

In all programming languages, we declare a variable before using the variable but such is not the case with PowerApps. So, in PowerApps we never declare variables explicitly. In terms of typing, PowerApps has implicit typing. So, we are not required to specify the type of variable for example int a = 0 in programming language indicates the variable is of type integer but in PowerApps we are not required to specify the type of variable.

To create a variable, we just need to run the function.

Set(global Variable, "Hello World")
Update Context({context Variable: "Hello World"})
Collect(collection Variable, "Hello World")

Where X is the variable name and Hello World is the value of that variable.

For removing the variable, we are required to remove all Set, UpdateContext, Navigate, Collect, or ClearCollect functions that implicitly establish the variable. If these functions are not present then there is no variable. We must even remove the variable reference if used in other places.

To properly understand all three different types of the variable, we can create a small example.

Global Variable

To understand the global variable, we can create a sample canvas app. Let's create it using the below step.

  1. Navigate to here
  2. Click on Create
  3. Select the Canvas app from the blank as a type of PowerApps
  4. Provide the name of the app as GlobalVariable and select the format as Tablet
  5. Add a text input, label, and button from the insert table
  6. Select the button and update the formula in OnSelect
    Set(global Variable, TextInput1.Text)
    
  7. Now select the label field and update the formula in the Text field
    global Variable
    
  8. To check all global variables in PowerApps click on File, then click on Variables. This would display all the global variables present in the app. To check all the references, click on the variable. This would display where the variable definition is present and where it is used.
    PowerApp
  9. This app will update the variable value as entered in the text field and display it in the label accordingly.
    Variables In PowerApps

Context Variable

To understand the context variable, we can create a sample canvas app. Let's create it using the below step:

  1. Navigate to here.
  2. Click on Create
  3. Select the Canvas app from the blank as a type of PowerApps
  4. Provide the name of the app as a context variable and select the format as Tablet
  5. Add a text input, label, and button from the insert table
  6. Select the button and update the formula in OnSelect.
    Update Context({contextVariable: TextInput1.Text})
    
  7. Now select the label field and update the formula in the Text field.
    context Variable
    
  8. To check all context variables in PowerApps click on File, then click on Variables. This displays all the screens present in the app. To check all context variables inside the screen, select the screen. To check all the references, click on the variable. This would display where the variable definition is present and where it is used.
    Context variables
  9. This app will update the variable value as entered in the text field and display it in the label accordingly.
    Text field

Collections Variable

To understand the collections variable, we can create a sample canvas app. Let's create it using the below step.

  1. Navigate to here
  2. Click on Create
  3. Select the Canvas app from the blank as a type of PowerApps
  4. Provide the name of the app as a collection variable and select the format as Tablet
  5. Add a five-text input, two-button, and Data table from the insert table
  6. Select the button update the formula in OnSelect and change the text to Submit Collection.
    Collect(formDataTemp, {
        Name: TextInput1.Text,
        temp1: TextInput2.Text,
        temp2: TextInput3.Text,
        temp3: TextInput4.Text,
        temp4: TextInput5.Text
    })
    
  7. Now select the Data table and update the formula in the Items field.
    formDataTemp
    
  8. To clear the data from the collection, select the second button change the text to Clear Data, and update the OnSelect field with the below formula.
    Clear(formDataTemp)
    
  9. To check all collections variables in PowerApps click on File, then click on Collections. This would display all the collection variables present in the app. It would also display the first five items in the collections variable.
    Collection variables
  10. This app will update the variable value as entered in all the text fields and display it in the data table accordingly. To clear the data, we can click on the Clear Data button.
    Data table

Conclusion

With the introduction of variables in PowerApps, it has become a powerful tool for no-code applications. Use variables in your application wisely, as they use the application memory.


Similar Articles