Filter SharePoint items with date in the same month in Power Automate
Posted On October 3, 2021
“How to filter items with date in the same month/year as current date in Power Automate, e.g. Created date column value is 2021-09-15 or 2021-09-01 and current date is 2021-09-29.”
If you use Power Automate to build reports and reminders, you often work with a date. You don’t want all SharePoint items at the same time, but only the ones that are relevant for the report/reminder. The ones that were recently created, need a review, are waiting for approval, or require some other date related task.
All the previous reminder posts were based on a specific date – flows running daily working with today’s date. But what about a monthly report? A report running once a month, processing all items with date in the current month?
Get the start of a month
You’ll need two dates to build the right Filter Query in the ‘Get items’ action – the start and the end of the month.
Power Automate has an expression that’ll give you the start of a month from any date. Enter a date as a parameter to the startOfMonth(…) expression and it’ll return the first day at 00:00. For example, get the beginning of the current month:
Since the date of this post is 2021-10-03, the expression will return the start of October 2021.
That’s the first date you need. The next one is the end of the month.
Get the end of a month
Unlike the start of a month, there’s no expression to get the end of a month directly. But you can calculate it if you combine two expressions together. The first one is the startOfMonth(…) to get the start of this month, as explained above. And since the next month is exactly 1 month away, you can add a month to that date.
The result (at the time of this post) will be the start of November 2021.
That’s the second date you need. Now it’s time to combine them in the Filter Query.
Build the Filter Query
You’re looking for all items with a date between the two dates. The earliest possible date is the start of the month. The latest possible date is just before the start of the next month.
date <is greater than or equal> start of the month
date <is less than> start of the next month
Such ‘Get items’ will return only items with the relevant date in the current month.
When you filter items with date in a specific month in Power Automate, you must work with a range. You need the start date, the end date, and filter only items in between. In this situation it’s the start of the month and the start of the next month. The operators then define that dates at the start of the month should be included, but dates at the start of the next month shouldn’t.
The solution above is using a custom Date column and current date, but you can use any date column and any month. Change the column, change the utcNow(…) expression for a specific date, and filter items only in that specific month.