Sometimes you need to find out, if a certain date is within a certain month. To do this, you need to know the start and the end of the month to check if your date is between the start and the end. This article shows you, how to get the beginning and the end of a month with Power Automate.
First we look on how to get the start of the month with the Power Automate startOfMonth function.
Then we learn how to calculate the end of the month in Power Automate in an expression.
At the end of the article, we show how to use start and end of the month in a Microsoft flow.
Power Automate StartOfMonth Function
The Power Automate startOfMonth function calculates the beginning of a month for a given timestamp. It returns a fate with all time components set to the lowest value. The result can optionally be formatted by a given format.
- timestamp (mandatory): The timestamp defines for which month the start of month should be calculated.
- format (optional): A format string to define the output format. (For details, take a look at: How to format date and time in Power Automate)
- The start of the month for the given timestamp (optionally formatted by the given format string).
Power Automate StartOfMonth Expression Example
To calculate the beginning of the month, you can simply pass the current time to startOfMonth with the utcNow function.
startOfMonth(utcNow()) => 2022-09-01T00:00:00.0000000Z
Power Automate End Of Month
You might think, calculating the end of the month in Power Automate is trivial now that you know the startOfMonth function. But no, there is no endOfMonth function. To get the end of the month, we have to combine some functions.
The way I do it, is probably not the only way to get the end of a month, but the basic idea is probably always the same.
So here is the master plan to get the end of the month.
How to get the end of the month
- Get the start of the month
- Add one month to the start of the month
- Subtract one second of the beginning of next month
So here is my Power Automate expression for the end of this month:
Let’s take a closer look at what is happening here!
1: Get the start of the month
This step is easy to understand, when you read the paragraph on the startOfMonth function.
2: Get the start of the month
Here we use to the addToTime function to add 1 Month.
3: Subtract one second of the beginning of next month
This one might be a little confusion: Why addSeconds, shouldn’t it be subSeconds? There is no subSeconds, but addSeconds accepts negative values, this is why we use the -1.
Note: If you care about milliseconds the solution is not perfect, because we only get the last second of the last day of the month, not the last millisecond.
Step-by-step: How To Get Start And End Of The Month In Power Automate
Let’s create two variables within a flow: one for the start of the month and one for the end of the month.
Create a new flow instant cloud flow
new instant cloud flow
Add an ‘Initialize variable’ step, set type to string and for value use the expression ‘startOfMonth(utcNow(),’G’)’
Power Automate startOfMonth expression
Add an ‘Initialize variable’ step, set type to string and for value use the expression ‘addSeconds(addToTime(startOfMonth(utcNow()),1,’Month’),-1,’G’)
Power Automate end of month expression
Do a test run
Result of test run: Start and end of September 2022