Microsoft PowerApps: Collect, Clear and ClearCollect functions

Collect Function

The Collect function adds records to a data source. The items to be added can be:

  • A single value: The value is placed in the Value field of a new record. All other properties are left blank.
  • A record: Each named property is placed in the corresponding property of a new record. All other properties are left blank.
  • table: Each record of the table is added as a separate record of the data source as described above. The table isn’t added as a nested table to a record. To do this, wrap the table in a record first.

When used with a collection, additional columns will be created as needed. The columns for other data sources are fixed by the data source and new columns can’t be added. If the data source doesn’t already exist, a collection is created.

Collections are sometimes used to hold global variables or make a temporary copy of a data source. Canvas apps are based on formulas that automatically recalculate as the user interacts with an app. Collections don’t enjoy this benefit and their use can make your app harder to create and understand. Before using a collection in this manner, review working with variables.

 

Syntax :

CollectDataSourceItem, … )

  • DataSource – Required. The data source that you want to add data to. If it doesn’t already exist, a new collection is created.
  • Item(s) – Required. One or more records or tables to add to the data source

The example below , adds two records to the IceCream collection that includes a quantity of pistachio and orange ice cream. :

 

 

The IceCream collection gets updated , from only having the “Strawberry” row to also having our two rows added from the code above.

 

 

 

Clear Function

The Clear function deletes all the records of a collection. The columns of the collection will remain.

Note that Clear only operates on collections and not other data sources. You can use RemoveIfDataSource, true ) for this purpose. Use caution as this will remove all records from the data source’s storage and can affect other users. You can use the Remove function to selectively remove records.

Clear has no return value. It can only be used in a behavior formula.

 

Syntax:

ClearCollection )

  • Collection – Required. The collection that you want to clear.

The example below , removes all records from the IceCream collection. :

 

     The IceCream collection gets updated , all the previous records have been deleted

 

ClearCollect Function

The ClearCollect function deletes all the records from a collection. And then adds a different set of records to the same collection. With a single function, ClearCollect offers the combination of Clear and then Collect.

ClearCollect returns the modified collection as a table. ClearCollect can only be used in a behavior formula.

 

Syntax:

ClearCollectCollectionItem, … )

  • Collection – Required. The collection that you want to clear and then add data to.
  • Item(s) – Required. One or more records or tables to add to the data source.

The example below , clears all data from the IceCream collection and then adds a record that includes a quantity of strawberry ice cream.:

 

    The IceCream Collection gets cleared just like in the Clear example but in this case after being cleared we simultaneously add a record.