Collect & ClearCollect Functions

๐Ÿ”น Collect & ClearCollect Functions in Power Apps โ€“ Complete Guide

๐Ÿ“Œ Introduction

In Power Apps, Collect() and ClearCollect() are used to manage collections (temporary tables stored in memory). Collections help in storing, modifying, and manipulating data during an app session.

โœ… Why Use Collect & ClearCollect?

  • ๐ŸŽฏ Store temporary data locally.
  • โšก Perform bulk operations on data.
  • ๐Ÿš€ Filter, sort, or modify records efficiently without querying a database repeatedly.

๐Ÿ”น Step 1: Understanding Collections

โœ… What is a Collection?

A collection is a temporary in-memory data table used within Power Apps.

  • Unlike global variables, collections store multiple records.
  • They exist only during the app session unless saved externally.
  • Collections can be used for storing API responses, temporary lists, form data, etc.

๐Ÿ“Œ How to View Collections?

1๏ธโƒฃ Open Power Apps.
2๏ธโƒฃ Click on File > Collections to inspect stored data.


๐Ÿ”น Step 2: Using the Collect() Function

โœ… Syntax of Collect()

Collect(CollectionName, Records)
  • CollectionName โ†’ The name of the collection to store data.
  • Records โ†’ The data (single or multiple) to add to the collection.

Example 1: Create a Collection with Sample Data

1๏ธโƒฃ Insert a Button (Button1).
2๏ธโƒฃ Set the OnSelect property:

Collect(Employees, {ID: 1, Name: "John Doe", Age: 30})

๐Ÿ“Œ Now, a collection named Employees is created with one record.

Example 2: Add Multiple Records to a Collection

Collect(
    Employees, 
    {ID: 2, Name: "Alice", Age: 28},
    {ID: 3, Name: "Bob", Age: 35}
)

๐Ÿ“Œ Now, three records exist in the Employees collection.


๐Ÿ”น Step 3: Using the ClearCollect() Function

โœ… Syntax of ClearCollect()

ClearCollect(CollectionName, Records)
  • ClearCollect() first clears existing records in the collection and then adds new records.
  • It is useful when refreshing data or resetting a collection.

Example 1: Reset a Collection with New Data

ClearCollect(
    Employees, 
    {ID: 1, Name: "Emma", Age: 29},
    {ID: 2, Name: "Michael", Age: 40}
)

๐Ÿ“Œ Now, old data is removed, and only two new records remain!

Example 2: Populate a Collection from a Data Source

ClearCollect(Products, Filter(SharePointList, Category = "Electronics"))

๐Ÿ“Œ Now, Products contains only electronics from the SharePoint list!


๐Ÿ”น Step 4: Displaying Collections in a Gallery

1๏ธโƒฃ Insert a Gallery (Gallery1).
2๏ธโƒฃ Set its Items property:

Employees

๐Ÿ“Œ Now, the gallery displays all records from the Employees collection.


๐Ÿ”น Step 5: Using Collect() with User Input

โœ… Example: Add Data from Text Inputs to a Collection

1๏ธโƒฃ Insert Text Inputs (TextInput1, TextInput2) for Name & Age.
2๏ธโƒฃ Insert a Button (AddButton).
3๏ธโƒฃ Set the OnSelect property:

Collect(
    Employees, 
    {ID: CountRows(Employees) + 1, Name: TextInput1.Text, Age: Value(TextInput2.Text)}
)

๐Ÿ“Œ Now, users can add employees dynamically!


๐Ÿ”น Step 6: Removing Items from a Collection

โœ… Example: Remove an Item by ID

Remove(Employees, LookUp(Employees, ID = 1))

๐Ÿ“Œ Now, the employee with ID = 1 is removed!

โœ… Example: Remove All Records

Clear(Employees)

๐Ÿ“Œ Now, the collection is emptied!


๐Ÿ”น Step 7: Saving and Loading Collections

โœ… Example: Save a Collection to Local Storage

SaveData(Employees, "EmployeesData")

โœ… Example: Load Data from Local Storage

LoadData(Employees, "EmployeesData", true)

๐Ÿ“Œ Now, data is saved and loaded even after app restarts!


๐Ÿ”น Step 8: Best Practices for Collect & ClearCollect

โœ… Use ClearCollect() when refreshing data to avoid duplicates.

โœ… Use Collect() for adding data incrementally.

โœ… Limit collection size (Power Apps has a memory cap).

โœ… Use SaveData() for offline storage.

โœ… Use Labels to debug and track collection changes:

Label1.Text = CountRows(Employees)

๐Ÿ”น Conclusion

The Collect() & ClearCollect() functions help store and manage temporary data efficiently in Power Apps.

๐Ÿ’ก Key Takeaways:
โœ… Collect() adds records to a collection.
โœ… ClearCollect() clears a collection before adding new data.
โœ… Use collections for caching, filtering, and offline storage.
โœ… Limit data size to improve performance.

Now, you’re ready to manage data efficiently with collections!


Leave a Reply

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