๐น 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!
