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

To celebrate FabCon Vienna, we are offering 50% off select exams. Ends October 3rd. Request your discount now.

Reply
phontalba
New Member

How do I calculate last business date in a month in M Query?

Hi, I want to add a custom conditional column that calculates the last business date of the month. I would like to use Date.EndofMonth and specify that it needs to be a week day.

1 ACCEPTED SOLUTION
NHarington
Frequent Visitor

If you need to do this in an M query you can use the following code as a custom column

 

let
EndOfMonthDate = Date.EndOfMonth([YourDateColumn]),
DayOfWeek = Date.DayOfWeek(EndOfMonthDate, Day.Sunday),
LastBusinessDay = if DayOfWeek = 0 then Date.AddDays(EndOfMonthDate, -2)
else if DayOfWeek = 6 then Date.AddDays(EndOfMonthDate, -1)
else EndOfMonthDate
in
LastBusinessDay

 

Make sure to replace [YourDateColumn] with your date column

View solution in original post

3 REPLIES 3
Anonymous
Not applicable

Hi @phontalba ,

Based on my testing, please try the following methods:

1.Create the sample table.

vjiewumsft_0-1724724733113.png

2.Create the new End of month column in power query editor.

vjiewumsft_1-1724724753147.png

vjiewumsft_2-1724724766064.png

 

vjiewumsft_3-1724724773303.png

3.Create the new weekday column.

vjiewumsft_4-1724724784910.png

4.Create the new column to calculate last business date in a month.

if [Weekday] = 5 then Date.AddDays([EndOfMonth], -1) 
        else if [Weekday] = 6 then Date.AddDays([EndOfMonth], -2) 
        else [EndOfMonth]

vjiewumsft_5-1724724796767.png

vjiewumsft_6-1724724803764.png

5.You can also use the Advanced editor.

vjiewumsft_7-1724724832451.png

let
    // Replace with your date column#date(2024, 8, 27)
    Source = Table.FromRecords({[Date = #date(2024, 8, 27)]}),
    AddEndOfMonth = Table.AddColumn(Source, "EndOfMonth", each Date.EndOfMonth([Date])),
    AddWeekday = Table.AddColumn(AddEndOfMonth, "Weekday", each Date.DayOfWeek([EndOfMonth], Day.Monday)),
    LastBusinessDay = Table.AddColumn(AddWeekday, "LastBusinessDay", each 
        if [Weekday] = 5 then Date.AddDays([EndOfMonth], -1) 
        else if [Weekday] = 6 then Date.AddDays([EndOfMonth], -2) 
        else [EndOfMonth]
    ),
    RemoveColumns = Table.RemoveColumns(LastBusinessDay,{"EndOfMonth", "Weekday"})
in
    RemoveColumns

// let
//     Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("ddGxDYAwEATBVizHSP47GzBkFEAFlvtvA8hZ6bONbn6M3IuKwy3JZ8R76brzXL5gCpVCo7BS2CjsFDqFg4ICC24XjheuF84X7hcCCAWEBEIDo4H5/2hgNDAaGA2MBkYDo4HRoKJB/TOYDw==", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Date = _t]),
//     #"Changed Type" = Table.TransformColumnTypes(Source,{{"Date", type datetime}})
// in
//     #"Changed Type"

 

Best Regards,

Wisdom Wu

If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

NHarington
Frequent Visitor

If you need to do this in an M query you can use the following code as a custom column

 

let
EndOfMonthDate = Date.EndOfMonth([YourDateColumn]),
DayOfWeek = Date.DayOfWeek(EndOfMonthDate, Day.Sunday),
LastBusinessDay = if DayOfWeek = 0 then Date.AddDays(EndOfMonthDate, -2)
else if DayOfWeek = 6 then Date.AddDays(EndOfMonthDate, -1)
else EndOfMonthDate
in
LastBusinessDay

 

Make sure to replace [YourDateColumn] with your date column

NHarington
Frequent Visitor

Hi, You can use the below dax for a new column.

 

LastBusinessDayOfMonth =
VAR EndOfMonthDate = EOMONTH([YourDateColumn], 0)
VAR DayOfWeek = WEEKDAY(EndOfMonthDate, 2)
RETURN
IF(DayOfWeek = 6, EndOfMonthDate - 1,
IF(DayOfWeek = 7, EndOfMonthDate - 2,
EndOfMonthDate
)
)

 

Make sure to replace [YourDateColumn] with your date column

Helpful resources

Announcements
September Power BI Update Carousel

Power BI Monthly Update - September 2025

Check out the September 2025 Power BI update to learn about new features.

August 2025 community update carousel

Fabric Community Update - August 2025

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

Top Solution Authors
Top Kudoed Authors