This article is part of a weekly blog series that delivers tips related to Alteryx Designer by Alteryx.
Finally, this is 80th blog!
How to Find the End of the Previous Month
By the way, date calculations can be surprisingly troublesome. What do you think? If you convert values to a DateTime or Date type, you can use a variety of convenient functions.
Suppose you have the last day of a given month and want to find the last day of the previous month. For example, let’s consider January 31, 2025. Since we want to go back one month, you might think of a calculation like this:
DateTimeAdd('2025-01-31',-1,"Month")
Let’s actually calculate it.


It correctly returns the month-end date one month earlier.
However, let’s look at the results across an entire year.

It turns out to be incorrect for several months. For February, the result becomes January 29 instead of January 31, which is the actual month-end. And for April, June, September, and November, the result becomes the 30th even though the previous month has 31 days. In other words, DateTimeAdd(date, -1, “Month”) only adjusts the month portion. Since the last day of the month varies depending on the month, simply adding or subtracting months does not always produce the correct result.
There are several ways to avoid this problem.
- Trim the date to the month level with DateTimeTrim function to get the first day of the month, then subtract one day
- Subtract one month, then use the “lastofmonth” option in the DateTimeTrim function
Neither method can be done with a single function alone, but both can be achieved by combining functions.
Use DateTimeTrim to Trim to the Month, Set It to the First Day, and Subtract One Day
The function looks like this (assuming the input field is [Date]).
DateTimeAdd(DateTimeTrim([Date],"Month"),-1,"Day")
If your original input is already only the first day of the month, you can simply subtract one day without using DateTimeTrim.
Subtract One Month and Use the “lastofmonth” Option in DateTimeTrim
The function looks like this (assuming the input field is [Date]):
DateTimeTrim(DateTimeAdd([Date],-1,"Month"),"lastofmonth")
Using these methods, you can calculate the end of the previous month as shown below.

Conclusion
- I introduced methods for calculating the end of the previous month
- This can be achieved by combining DateTimeTrim and DateTimeAdd
Sample Workflow Download
The next post will be…
The next post is planned to cover tips related to date and time operations.

コメント