Blog
Search - How to filter records by month and year using a delegable expression
June 29. 2021
A common question I often see is - how do I filter a table or SharePoint list by month and year? This post describes how to carry out this task using formula that is delegable.
A common requirement is to filter records by month and year, and a practical way to accomplish this is to provide month and year drop down boxes.
A natural way to build this feature is to call the Filter function and to utilize the Month and Year functions. The problem with this method is that it results in an expression that is not delegable, as shown in the screenshot beneath.
To avoid this delegation problem, we build a condition that filters records between the start and the end date of the month, instead of calling the Month and Year functions.
Demonstration - how to filter records by month and year dropdowns
To demonstrate the typical application of this technique, let's walk through the steps to build a screen that filters records by month and year drop downs.
First, let's add a dropdown control called drpMonth. We set the Items property to the following formula to populate the drop down with a sequence of numbers from 1 to 12.
Sequence(12,1,1)
Next, we'll add a dropdown control called drpYear and set the Items
property to the following formula. This populates the drop down with a range of year numbers, starting from the current year minus 6 years.
Sequence(12,Year(Now()) - 6,1)
We can now add a gallery or table table control, and set the Items property to the following formula. This formula derrives the start and end dates based on the dropdown values, and filters the "aquisition date" column by these values.
Filter(Property,The screenshot beneath highlights how this formula works as expected.
AquisitionDate >= Date(drpYear.Selected.Value,
drpMonth.Selected.Value,
1)
And
AquisitionDate < DateAdd(Date(drpYear.Selected.Value,
drpMonth.Selected.Value,
1),
1,
Months
)
)
Conclusion
To filter a set of records that match a selected month and year, we
can construct a filter expression that returns all records.that fall between
the start and the end date of the selected month. This technique will work in a delegable way.
- Categories:
- dates
Related posts
- Dates - How to convert a month name (eg January, Feburary, March) to month number (eg, 1, 2, 3)
- Dates - How to format date day numbers with suffix (st nd rd th)
- Formula - How to get the dates for the current week
- Dates - How to calculate UK tax weeks
- Dates - How to convert dates from MM DD YYYY to DD MM YYYY and vice versa
- Dates - How to detect overlapping times / scheduling conflicts
- Dates - How to display or convert Excel date serial numbers
- Dates - How to get the start date from week number
- Dates - How to check if a year is a leap year
- Dates - How to get the week number for a given date
- Dates - How to get the nth weekday for a month
- Dates - How to get the last weekday for a month
- Dates - How to get a numbered list of days or months, starting from a specified day or month
- Controls - How to display dates in a combo box