Calculate last business day of each month

Hello.
I have a finished process, but I need to optimize a SQL query that I perform to extract the data.
This process is launched once a month and I need to calculate what was the last working day of the previous month to pass it on to the consultation.
Example
June = 06/30/2021
July = 07/30/2021
August = 08/31/2021
and so with every month.
I’ve been seeing similar questions on the forum but can’t get it.
Thanks.

1 Like

Hey @Aguirre

If you are processing something today. You need to find the last month’s last date am I right ?

@Aguirre - You can use below code get last day of the previous month…and then check if its Sat or Sunday…

If Sat then -1 day to get 07/30, if its sunday then do -2 get the last business day…

image

Hope this helps…

Correct, Really every day 10 of each month I am going to launch a process and I need to put in the SQL, the last working day of the month

Hello.
With your idea I have put an “IF” depending on the day, I have 1 or 2 days left from the date, which will be the one that goes through the SQL.
I have left it like that, any ideas to make it more efficient or is it okay?

Greetings and thank you.

find this statement integrating the SUN/SAT offset already:
Now.AddDays(-Now.Day + {-2,0,0,0,0,0,-1}(Now.AddDays(-Now.Day).DayOfWeek))

find starter help here:
GetLastWorkday_PreviousMonth.xaml (6.0 KB)

2 Likes

Thanks for your solution, it’s also worth it.

All the best.

This topic was automatically closed 3 days after the last reply. New replies are no longer allowed.