Skip to main content
cancel
Showing results for 
Search instead for 
Did you mean: 

Register now to learn Fabric in free live sessions led by the best Microsoft experts. From Apr 16 to May 9, in English and Spanish.

Reply
BusyBusyBee
New Member

Custom month end date - second to last Friday

Hi there,

 

I'm wanting to create a custom column in Power Query based on the Date column to calculate the month end date being the second to last Friday of the month.

 

eg Date 13/03/2023 and month end date would be 24/03/2023

eg Date 07/02/2023 and month end date would be 17/02/2023

 

Any help would be greatly appreciated

1 ACCEPTED SOLUTION

=let fx=(dt)=>Date.AddDays(dt,-List.Select({7..14},each Date.DayOfWeek(Date.AddDays(dt,-_))=4){0}),a=Date.EndOfMonth([Date]),b=fx(a) in if [Date]>b then fx(Date.EndOfMonth(Date.AddMonths(a,1))) else b

View solution in original post

6 REPLIES 6
wdx223_Daniel
Super User
Super User

= let a=Date.EndOfMonth([Date]) in Date.AddDays(a,-List.Select({7..14},each Date.DayOfWeek(Date.AddDays(a,-_))=4){0})

Wow, that's great @wdx223_Daniel. I do have a slight problem with this where I should have specified in my note above. For any date that falls after the month end date it would then be allocated to the next month end date.

 

eg Date 13/03/2023 and month end date would be 24/03/2023

eg Date 25/03/2023 and month end date would be 21/04/2023

eg Date 17/04/2023 and month end date would be 21/04/2023

eg Date 28/04/2023 and month end date would be 19/05/2023

 

Would you have a suggestion on an updated formula?

= let fx=(dt)=>Date.AddDays(dt,-List.Select({7..14},each Date.DayOfWeek(Date.AddDays(dt,-_))=4){0}),a=Date.EndOfMonth([Date]),b=fx(a) in if [Date]>b then fx(Date.AddMonths(a,1)) else b

Thanks again @wdx223_Daniel 
There is a slight problem coming through with particular dates where it is being calculated incorrectly.

For a date between 18/02/2023 - 28/02/2023 the month end date is being calculated as 17/03/2023 instead of the 24/03/2023

 

For 2024, the dates between 20/04/2024 to 30/04/2024 the month end date is being calculated as the 17/05/2024 instead of 24/05/2024

 

It seems to be one section each year where it's calculating the month end date incorrectly but all other dates calculate fine.

 

Appreciate the help and time you have put into my issue

=let fx=(dt)=>Date.AddDays(dt,-List.Select({7..14},each Date.DayOfWeek(Date.AddDays(dt,-_))=4){0}),a=Date.EndOfMonth([Date]),b=fx(a) in if [Date]>b then fx(Date.EndOfMonth(Date.AddMonths(a,1))) else b

Awesome! That worked perfectly. Thanks so much.

Helpful resources

Announcements
Microsoft Fabric Learn Together

Microsoft Fabric Learn Together

Covering the world! 9:00-10:30 AM Sydney, 4:00-5:30 PM CET (Paris/Berlin), 7:00-8:30 PM Mexico City

PBI_APRIL_CAROUSEL1

Power BI Monthly Update - April 2024

Check out the April 2024 Power BI update to learn about new features.

April Fabric Community Update

Fabric Community Update - April 2024

Find out what's new and trending in the Fabric Community.

Top Solution Authors
Top Kudoed Authors