Forum Discussion

PAPutzback2's avatar
PAPutzback2
Helper II
4 years ago
Solved

Simple calendar table creation with M Query with date range from fact table

I can never seem to find the post that covers how to create a calendar table with M, it is either DAX or some strange functions that need to be invoked, or a dummy table needs to bet up with seed dat...
  • PAPutzback2's avatar
    PAPutzback2
    4 years ago

    Thanks. I knew I could hard code the date in the query, but that would require opening the model to update the date if you had to change it. Perhaps you had patient data and the calendar was tied to birthdate and an older person was added, or housing data and earlier build dates start showing up. 

  • Anonymous's avatar
    Anonymous
    4 years ago

    Hi PAPutzback2 ,

     

    Based on my research, here's my solution

     

    let
        Source = "",
        MinDate = List.Min(MainTable[Date]),
        MaxDate = List.Max(MainTable[Date]),
        Days = Duration.Days(MaxDate-MinDate)+1,
        Duration = #duration(1, 0, 0, 0),
        Custom1 = List.Dates(MinDate,Days,Duration),
        #"Converted to Table" = Table.FromList(Custom1, Splitter.SplitByNothing(), null, null, ExtraValues.Error),
        #"Renamed Columns" = Table.RenameColumns(#"Converted to Table",{{"Column1", "Date"}})
    in
        #"Renamed Columns"

     

     

    This code gets the maximum date and minimum date from the main table to create a calendar table, hope it can help you.

     

    Reference: https://foresightbi.com.ng/microsoft-power-bi/creating-calendar-tables-with-power-query/

     

    Best Regards,

    Stephen Tao

     

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