🔹 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!