Filtering by a Date Range in Power Apps
Have you ever needed to filter a gallery in Power Apps by a date range? Maybe you want to return all items from the current week? Or maybe the previous week, pay period, month etc. In this blog post and corresponding video I’ll walk you through how to do this in just a few simple steps!
Step 1: Create a Collection of the Date Ranges
The first step in the process is to create a local collection to store all of your date range options. This will be used to populate a dropdown control so that we can dynamically filter our gallery. In my case, I put this local collection code in the OnVisible of the screen which I want to filter my gallery on. Here’s the code:
ClearCollect( colTimeFilter, { DisplayName: "This Week", StartDate: DateAdd( Today(), 1 - Weekday( Today(), StartOfWeek.Monday ), Days ), EndDate: DateAdd( Today(), 1 - Weekday( Today(), StartOfWeek.Monday ) + 6, Days ) }, { DisplayName: "Last Week", StartDate: DateAdd( Today(), -6 - Weekday( Today(), StartOfWeek.Monday ), Days ), EndDate: DateAdd( Today(), -6 - Weekday( Today(), StartOfWeek.Monday ) + 6, Days ) }, { DisplayName: "Last Pay Period", StartDate: DateAdd( Today(), -12 - Weekday( Today(), StartOfWeek.Monday ), Days ), EndDate: DateAdd( Today(), -12 - Weekday( Today(), StartOfWeek.Monday ) + 12, Days ) } )
Step 2: Add a Dropdown Control
Next, you’ll want to add a dropdown control to your screen and set the “Items” property of that control to your collection name (colTimeFilter). Make sure that the displayed field is set to “DisplayName”.
Step 3: Filter the Gallery
Now all that’s left to do is to filter the gallery! Just put this code in the “Items” property of your gallery (this assumes you are filtering off of a Date Only field called “Date” in your data source):
Sort( Filter( TimeClockData, Date >= ddTimeSelection.Selected.StartDate && Date <= ddTimeSelection.Selected.EndDate ), Date, Descending )
To see this all in action check out my video:
Recent Comments