Store a table

Completed

In the previous examples, the tables were only used in the current context of the Data table or the Drop down control. Often in your app, you'll need to store a table and use it in multiple places within the app. To do this, Power Apps has a table variable called a collection.

Collections are a type of variable in Power Apps

Collections are table variables where you can store data in a structured format, as you would in a tabular data source, without writing to a data source. In other words, collections store values in rows and columns. You can use collections with table functions as you would any other data source. However, you can't use a collection with the Form control. If you have a developer background, you can think of a collection as an array. You don't have to initialize or predefine a collection. When you create it and set values, Power Apps sets it up for you.

Create a collection

Creating a collection can be done within your app anywhere an action can normally be taken. For example, you can create a collection when pressing a button by changing the OnSelect property of the button. You can also create a collection whenever you come to a certain screen by changing the OnVisible property of that screen. You can create a collection named collectMyFirstCollection using this formula.

Collect(collectMyFirstCollection, {Name: "George", FavoriteColor:"Orange"})

The would create a collection name collectMyFirstCollection. The collection would have a column named Name and another column named FavoriteColor. The collection would have one record (row) of data with George as the value of Name and Orange as the value for FavoriteColor. Notice the syntax is similar to the Table function from earlier in this module.

You could add another record to the collection by using this formula.

Collect(collectMyFirstCollection, {Name: "Nicole", FavoriteColor:"Purple"})

You can also add more than one record at a time by using this formula.

Collect(collectMyFirstCollection, {Name: "Jeff", FavoriteColor:"Blue"}, {Name: "Ralph", FavoriteColor: "Red"})

If you ran all those commands, your collection would look like this table:

Name FavoriteColor
George Orange
Nicole Purple
Jeff Blue
Ralph Red

You can use this collection as a data source for any control that needs a table, for example, in a Gallery or Drop-down control.

Remove data from your collection

To clear the existing data from your collection before you add new data, you can use the ClearCollect function. Taking the existing collection from the previous example, you can use this formula:

ClearCollect(collectMyFirstCollection, {Name: "Fred", FavoriteColor:"Green"})

This would have the effect of clearing all the records in the table before adding the new record. Your collection would now look like this:

Name FavoriteColor
Fred Green

You can also remove all the records from a collection by using the Clear function. This formula removes all the records from your collection but leaves your columns intact:

Clear(collectMyFirstCollection)