How to Use ClearCollect and Bind Data into a Gallery in PowerApps Canvas Apps: Filter Employees by Age Greater Than 30
Today, someone from one of the WhatsApp groups I am a member of asked for help:
Hello everyone,
I have a scenario where I am fetching records from Dataverse and displaying them in a horizontal gallery control. I need to display certain columns of the Dataverse records in the gallery, but only based on specific conditions. Could anyone guide me on how to achieve this?
Thank you in advance!
In this blog, we will explore how to use the ClearCollect function and bind data into a Gallery, leveraging SharePoint as the data source. Our goal is to filter employees whose age is greater than 30 and display the results in a Gallery.
Step 1: Set Up the SharePoint Data Source
Create a SharePoint List:
Navigate to your SharePoint site and create a list named Employees.
Add the following columns:
- Title (Single line of text) - Represents the employee's name.
- Age (Number) - Represents the employee's age.
Populate the List:
Add some sample data:
| Title | Age |
|---|---|
| John Smith | 28 |
| Mary Jones | 35 |
| Alice Brown | 40 |
| David White | 29 |
| Sarah Green | 32 |
Step 2: Connect the Data Source in PowerApps
Open PowerApps Studio and create a new Canvas App.
- Click on Data in the left-hand menu.
- Select + Add Data and search for SharePoint.
- Connect to your SharePoint site and select the Employees list.
Step 3: Add a Button to Load the Filtered Data
Insert a button into the Canvas App:
-
Insert a button into the Canvas App:
- Go to the Insert menu and select Button.
- Set the button's Text property to
"Filter Employees".
-
Set the button's OnSelect property to the following formula:
ClearCollect( FilteredEmployees, Filter(Employees, Age > 30) )Explanation:ClearCollect: Clears any existing data in the collection and adds the new filtered data.FilteredEmployees: Name of the collection to store filtered employee data.Filter(Employees, Age > 30): Filters theEmployeeslist where theAgecolumn is greater than 30.
Step 4: Add a Gallery to Display the Filtered Data
Insert a Gallery:
- Go to the Insert menu and select Gallery.
- Choose a vertical layout.
Bind the Gallery to the FilteredEmployees Collection:
- Select the Gallery.
-
Set its Items property to:
FilteredEmployees
Customize the Gallery:
- Click on the first item in the Gallery to select the template.
-
Add a Label to display the employee name:
-
Set the Text property to:
ThisItem.Title
-
Set the Text property to:
-
Add another Label to display the age:
-
Set the Text property to:
ThisItem.Age
-
Set the Text property to:
- Save and run the app by pressing
F5. - Click the Filter Employees button.
- Verify that the Gallery updates to display only employees aged greater than 30.
- Mary Jones (Age 35)
- Alice Brown (Age 40)
- Sarah Green (Age 32)
Step 6: Optional Enhancements
-
Add Search Functionality:
- Insert a Text Input box for the search term.
-
Modify the filter formula to include a search condition:
ClearCollect( FilteredEmployees, Filter(Employees, Age > 30 && StartsWith(Title, TextInput1.Text)) )
-
Show a Message When No Records Are Found:
- Insert a Label below the Gallery.
-
Set its Visible property to:
IsEmpty(FilteredEmployees) -
Set its Text property to:
"No employees found."