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
Anonymous
Not applicable

M query to expand table not including first month

Hello,

 

A few months ago someone helpfully provided me with the M query below. This was designed to expand a table to include a row per month of activity between the designated dates. I have adapted the query below, but for some reason, the first month of absence is not included. Can anyone fix the query or suggest an alternative solution?

 

(#"Absence Start Date" as date, #"Absence End Date" as date ) => let
    enddate = if #"Absence End Date" = null then DateTime.Date( DateTime.LocalNow() ) else  #"Absence End Date", 
    days = Duration.Days( enddate - #"Absence Start Date" ),
    listDates = List.Dates( #"Absence Start Date", days, #duration( 1, 0, 0, 0 ) ),
    firstDates = List.Select( listDates, each Date.Day( _ ) = 1 ),
    monthYear = List.Transform( firstDates, each Text.Start( Date.MonthName( _ ), 3 ) & "- " & Text.End( Number.ToText( Date.Year( _ ) ), 2  ) )
    
in
    monthYear

Tiff

1 ACCEPTED SOLUTION
Mariusz
Community Champion
Community Champion

Hi @Anonymous 

 

It looks like my work Smiley Happy, please see the adjusted function below.

(#"Absence Start Date" as date, #"Absence End Date" as date ) => let
    enddate = if #"Absence End Date" = null then DateTime.Date( DateTime.LocalNow() ) else  #"Absence End Date", 
    days = Duration.Days( enddate - #"Absence Start Date" ) + 1,
    listDates = List.Dates( #"Absence Start Date", days, #duration( 1, 0, 0, 0 ) ),
    monthYear = List.Transform( listDates, each Text.Start( Date.MonthName( _ ), 3 ) & "- " & Text.End( Number.ToText( Date.Year( _ ) ), 2  ) ),
    distMonthYear = List.Distinct( monthYear )
    
in
    distMonthYear

 

Best Regards,
Mariusz

If this post helps, then please consider Accepting it as the solution.

Please feel free to connect with me.
Mariusz Repczynski

 

View solution in original post

2 REPLIES 2
Mariusz
Community Champion
Community Champion

Hi @Anonymous 

 

It looks like my work Smiley Happy, please see the adjusted function below.

(#"Absence Start Date" as date, #"Absence End Date" as date ) => let
    enddate = if #"Absence End Date" = null then DateTime.Date( DateTime.LocalNow() ) else  #"Absence End Date", 
    days = Duration.Days( enddate - #"Absence Start Date" ) + 1,
    listDates = List.Dates( #"Absence Start Date", days, #duration( 1, 0, 0, 0 ) ),
    monthYear = List.Transform( listDates, each Text.Start( Date.MonthName( _ ), 3 ) & "- " & Text.End( Number.ToText( Date.Year( _ ) ), 2  ) ),
    distMonthYear = List.Distinct( monthYear )
    
in
    distMonthYear

 

Best Regards,
Mariusz

If this post helps, then please consider Accepting it as the solution.

Please feel free to connect with me.
Mariusz Repczynski

 

Anonymous
Not applicable

That's worked perfectly, thanks so much for your help then and now!

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.