Using variables and collections in Power Apps

Power Apps gives you the ability to use variables within your Power App. This blog post will describe the different kinds of variables and how to use them.
The three types of variables I’ll describe in this post are local variables, global variables and collections.

Local variable

A local variable is a single row variable that exists only within one specific screen. Because of this, this variable cannot be used across your entire Power App.

Setting the variable

You can set these variables using the UpdateContext() function:

UpdateContext({varLocal:"Local variable"})

The variable needs to be wrapped within accolades. Within the accolades, you need to provide the variable with a name (in the above example, the variable is called varLocal), followed by a colon and then the actual value of the variable (in the above example, the value is Local variable).

String values need to be wrapped within quotation marks, integer values don’t need quotation marks.

If you want to store a record (table) within your variable, you have to wrap the value within a second set of accolades in which you provide the column name, followed by a colon and then the column value:

UpdateContext({varLocalRecord:{Name:"Rik de Koning",Email:"rik@about365.nl"}})

You can also store a predefined record within your variable, such as the User() attribute:

UpdateContext({varLocalUser:User()})

To access a specific property from the record, you can call the variable, followed by a dot and then the property itself:

varLocalUser.FullName

If you want to set more than one local variables at once, you can split them with a comma within the accolades:

UpdateContext({varLocal:"Local variable", varLocalSecond:"Second local variable"})

Global variable

A global variable is a single row variable that exists within your entire Power Apps, so this variable can be used within all screen across your entire Power App.

Setting the variable

You can set these variables using the Set() function:

Set(varGlobal,"Global variable")

This function works a bit different than the UpdateContext() function; you first provide the variable with a name (which is varGlobal in the above example), followed by a comma and then the actual value of the variable (which is Global variable in the above example).

The method of storing a record inside your global variable works in the same way you store it in a local variable:

Set(varGlobalRecord,{Name:"Rik de Koning",Email:"rik@about365.nl"})

You cannot set more than one global variable within a single Set() command. If you need to set more than one global variables, you need to use the Set() function multiple times:

Set(varGlobal,"Global variable"),
Set(varGlobalSecond,"Second global variable")

Clearing variables

You cannot delete variables, so if you want to clear your variable, you need to set the value of your variable by using the Blank() function:

Clearing a local variable

UpdateContext({varLocal:Blank()})

Clearing a global variable

Set(varGlobal,Blank())

Used variables

To view all variables used within your Power App and their values, you can go to File > Variables. From there, you can see both Global and Local variables.
Global variables can be viewed by selecting the Global section.
Local variables can be viewed by selecting the name of your screen where there variable is used.

Please note that you will only see the variable after is has been set once!

Collection

Collections are multi row valued variables. You can look at them as arrays or tables. Collections can be used across your entire Power App.

Initializing a collection

You can initialize a collection by using the ClearCollect() function:

ClearCollect(colCollection,{Name: "Rik de Koning",Email: "rik@about365.nl"})

The ClearCollect() function will clear the entire content of the collection if it already exists, so be careful when using the ClearCollect() function. More on this below

First, you need to provide a name for the collection (which is colCollection in the above example). Then you need to provide the rows inside your collection, wrapped within accolades. Each column needs to be provided with a name (which are Name and Email in the above example) and a value for that specific row (which are Rik de Koning and rik@about365.nl in the above example).

If you want to add more than one row into your collection, you need to add a comma after your first row (after the accolades!) and add another row definition wrapped within accolades:

ClearCollect(colCollection,
	{Name: "Rik de Koning",Email: "rik@about365.nl"},
	{Name: "Someone Else",Email: "someoneelse@about365.nl"}
)

You can also store a predefined record within your collection row, such as the User() attribute or even an entire or filtered SharePoint list:

ClearCollect(colUsers,User())

The above example stores the FullName, Email and Image for the logged in user

Appending rows to a collection

To append rows to a collection, you need to use the Collect() function. It basically works the same as the ClearCollect() function, so you need to provide the collection name you want to append a row to and after that you need to provide the values for the columns.

In the below examples, I use a custom form in Power Apps from which I perform the described actions:

Collect(
	colUsers,
	{FullName:txtFullName.Text, Email: txtEmail.Text, Image:UploadedImage1.Image}
)

Deleting rows from a collection

To remove rows from a collection, you need to use the RemoveIf() function:

RemoveIf(colUsers,Email=ThisItem.Email)

First, you need to provide the name of the collection from where you want to remove a row from. Then, you need to provide the expression that will lookup the corresponding row for you.

In the above example the row which contains the Email address from the gallery item will be removed from the collection.

Clearing an entire collection

To clear an entire collection, you need to use the Clear() function:

Clear(colUsers)

The only thing you need to provide is the name of the collection you want to clear. This will not remove the collection, it will only delete all rows inside the collection.

‘Stringify’ your collection

Sometimes, you need to output the values from your collection and not all targets will support arrays. Luckily, you can ‘convert’ your collection into a string using the Concat() function:

UpdateContext(
    {
        varString: Concat(
            colUsers,
            Concatenate(
                FullName,
                " (",
                Email,
                ")"
            ),
            Char(10)
        )
    }
)

In the above example, I store the converted collection into a local variable called varString using the UpdateContext() function.

The Concat() function needs 3 attributes:

  • The collection that needs to be ‘converted’ into a string
    In the above example, this is the colUsers collection.
  • The value(s) that needs to be extracted from the collection
    In my case, I want a combination of both FullName and Email where Email needs to be wrapped inside brackets so the result will be <FullName> (<Email>).
    To combine values, you need the Concatenate() function. Each attribute specifies the value that needs to be combined with each other, separated by a comma. In the above example, the following attibutes are combined:

    • FullName
    • ‘ (‘ as a manual string value
    • Email
    • ‘)’ as a manual string value
  • The separator that will separate each row from each other inside the string.
    In my case, I want each row from the collection to be put on a new line. The line feed character for a newline is Char(10).
    When using another attribute, e.g. a semicolon, please make sure that you put that separator between quotation marks (“;”)

Used collections

To view all collections used within your Power App and their contents, you can go to File > Collections. You can then select a specific collection and it will show you a preview of its contents:

Please note that the preview will only show the first 5 items within your collection and that the collection will only show after it has been initialized.

 

4 Replies to “Using variables and collections in Power Apps”

  1. Is it possible to use collection inside Set, if not, is there a way to make a collection global between two screens?

    0
    0
  2. Say for instance I want to create a collection based on a filtered column in my list?
    I tried:
    ClearCollect(colCollectionName, Filter(ListName, ColumnName = ColumnValue))
    However, the collection returns empty.

    0
    0
    1. That should work. Make sure to set your ColumnValue between quotes if it contains a string value. If you don’t, your condition expects the value to be an integer and it won’t work.
      Example: TextField = “Some text”

      If you have a multi select field (eg. people, choice or lookup), you must use the following condition:
      “Some text” in MultiChoiceField

      0
      0

Leave a Reply

Your email address will not be published. Required fields are marked *

This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

The reCAPTCHA verification period has expired. Please reload the page.

This site uses Akismet to reduce spam. Learn how your comment data is processed.