Extended Date Table (Power Query M function)

Hi everyone,

To make it easier to find I’ve placed the updated Power Query M code function for the extended Date Table / Dates Table in the category where it belongs… I hope you’ll find it useful and if there are any questions, just reach out and let me know! :wink:

// August 2022
There has been an overhaul of the M code from when it was initially shared. This affects column names and values. In the Analyst Hub (follow this link) you can copy a version of Extended Date table which returns a record. That record includes the new v2, the deprecated v1.32 and a change log.

Copy the M code below for the new V2 and paste it into a new blank query.

.

let
    fnDateTable = ( StartDate as date, EndDate as date, optional FYStartMonthNum as number, optional Holidays as list, optional WDStartNum as number, optional AddRelativeNetWorkdays as logical ) as table =>
    let
        FYStartMonth = List.Select({1..12}, each _ = FYStartMonthNum){0}? ?? 1,
        WDStart = List.Select({0..1}, each _ = WDStartNum){0}? ?? 0,
        CurrentDate = Date.From(DateTimeZone.FixedUtcNow()),
        DayCount = Duration.Days(Duration.From(EndDate - StartDate)) + 1,
        weekStart = Day.Monday,
        HolidaysProvided = Holidays <> null,

        Source = List.Dates(StartDate, DayCount, Duration.From(1)),
        AddToday = if List.Contains(Source, CurrentDate) then Source else List.Combine({Source, {CurrentDate}}),
        ToTable = Table.FromColumns({AddToday}, type table [Date = date]),

        /*  === Year Columns === */
        AddYearRecord = Table.AddColumn(ToTable, "YearRecord", each [
            Year = Date.Year([Date]),
            CurrYearOffset = Date.Year([Date]) - Date.Year(CurrentDate),
            YearCompleted = Date.EndOfYear([Date]) < Date.EndOfYear(CurrentDate)
        ], type [Year=Int64.Type, CurrYearOffset=Int64.Type, YearCompleted=logical]),
        ExpandedYear = Table.ExpandRecordColumn(AddYearRecord, "YearRecord", {"Year", "CurrYearOffset", "YearCompleted"}),

        /*  === Quarter Columns === */
        AddQuarterRecord = Table.AddColumn(ExpandedYear, "QuarterRecord", each [
            Quarter Number = Date.QuarterOfYear([Date]),
            Quarter = "Q" & Number.ToText(#"Quarter Number"),
            Start of Quarter = Date.StartOfQuarter([Date]),
            End of Quarter = Date.EndOfQuarter([Date]),
            #"Quarter & Year" = "Q" & Number.ToText(#"Quarter Number") & Date.ToText([Date], [Format = " yyyy"]),
            QuarternYear = [Year] * 10 + #"Quarter Number",
            CurrQuarterOffset = ((4 * Date.Year([Date])) + #"Quarter Number") - ((4 * Date.Year(CurrentDate)) + Date.QuarterOfYear(CurrentDate)),
            QuarterCompleted = #"End of Quarter" < Date.EndOfQuarter(CurrentDate)
        ], type [Quarter Number=Int64.Type, Quarter=text, Start of Quarter=date, End of Quarter=date, #"Quarter & Year"=text, QuarternYear=Int64.Type, CurrQuarterOffset=Int64.Type, QuarterCompleted=logical]),
        ExpandedQuarter = Table.ExpandRecordColumn(AddQuarterRecord, "QuarterRecord", 
            {"Quarter Number", "Quarter", "Start of Quarter", "End of Quarter", "Quarter & Year", "QuarternYear", "CurrQuarterOffset", "QuarterCompleted"}),

        /*  === Month Columns === */
        AddMonthRecord = Table.AddColumn(ExpandedQuarter, "MonthRecord", each [
            Month = Date.Month([Date]),
            Start of Month = Date.StartOfMonth([Date]),
            End of Month = Date.EndOfMonth([Date]),
            #"Month & Year" = Text.Proper(Date.ToText([Date], [Format = "MMM yyyy"])),
            MonthnYear = [Year] * 100 + Month,
            CurrMonthOffset = ((12 * Date.Year([Date])) + Month) - ((12 * Date.Year(CurrentDate)) + Date.Month(CurrentDate)),
            MonthCompleted = #"End of Month" < Date.EndOfMonth(CurrentDate),
            Month Name = Text.Proper(Date.ToText([Date], "MMMM")),
            Month Short = Text.Proper(Date.ToText([Date], "MMM")),
            Month Initial = Text.Start(Text.Proper(Date.ToText([Date], "MMMM")), 1) & Text.Repeat(Character.FromNumber(8203), Month),
            Day of Month = Date.Day([Date])
        ], type [Month=Int64.Type, Start of Month=date, End of Month=date, #"Month & Year"=text, MonthnYear=Int64.Type, CurrMonthOffset=Int64.Type, MonthCompleted=logical, Month Name=text, Month Short=text, Month Initial=text, Day of Month=Int64.Type]),
        ExpandedMonth = Table.ExpandRecordColumn(AddMonthRecord, "MonthRecord", 
            {"Month", "Start of Month", "End of Month", "Month & Year", "MonthnYear", "CurrMonthOffset", "MonthCompleted", "Month Name", "Month Short", "Month Initial", "Day of Month"}),

        /*  === Week Columns === */
        AddWeekRecord = Table.AddColumn(ExpandedMonth, "WeekRecord", each [
            WeekNumCalc = Number.RoundDown((Date.DayOfYear([Date]) - (Date.DayOfWeek([Date], weekStart) + 1) + 10) / 7),
            Week Number = if WeekNumCalc = 0 
                then Number.RoundDown((Date.DayOfYear(#date(Date.Year([Date]) - 1, 12, 31)) - (Date.DayOfWeek(#date(Date.Year([Date]) - 1, 12, 31), weekStart) + 1) + 10) / 7)
                else if (WeekNumCalc = 53 and (Date.DayOfWeek(#date(Date.Year([Date]), 12, 31), weekStart) + 1 < 4)) then 1 else WeekNumCalc,
            Start of Week = Date.StartOfWeek([Date], weekStart),
            End of Week = Date.EndOfWeek([Date], weekStart),
            #"Week & Year" = "W" & Text.PadStart(Text.From(#"Week Number"), 2, "0") & " " & Text.From(Date.Year(Date.AddDays(#"Start of Week", 3))),
            WeeknYear = Date.Year(Date.AddDays(#"Start of Week", 3)) * 100 + #"Week Number",
            CurrWeekOffset = (Number.From(#"Start of Week") - Number.From(Date.StartOfWeek(CurrentDate, weekStart))) / 7,
            WeekCompleted = #"End of Week" < Date.EndOfWeek(CurrentDate, weekStart)
        ], type [Week Number=Int64.Type, Start of Week=date, End of Week=date, #"Week & Year"=text, WeeknYear=Int64.Type, CurrWeekOffset=Int64.Type, WeekCompleted=logical]),
        ExpandedWeek = Table.ExpandRecordColumn(AddWeekRecord, "WeekRecord", 
            {"Week Number", "Start of Week", "End of Week", "Week & Year", "WeeknYear", "CurrWeekOffset", "WeekCompleted"}),

        /* === Day Columns === */
        AddDayRecord = Table.AddColumn(ExpandedWeek, "DayRecord", each [
            Day of Week Number = Date.DayOfWeek([Date], weekStart) + WDStart,
            Day of Week Name = Text.Proper(Date.ToText([Date], "dddd")),
            Day of Week Initial = Text.Proper(Text.Start(#"Day of Week Name", 1)) & Text.Repeat(Character.FromNumber(8203), Date.DayOfWeek([Date], weekStart) + WDStart),
            Day of Year = Date.DayOfYear([Date]),
            DateInt = [Year] * 10000 + [Month] * 100 + [Day of Month],
            CurrDayOffset = Number.From([Date]) - Number.From(CurrentDate),
            IsAfterToday = not ([Date] <= CurrentDate),
            IsWeekDay = if Date.DayOfWeek([Date], weekStart) > 4 then false else true,
            IsHoliday = if not HolidaysProvided then "Unknown" else List.Contains(Holidays, [Date]),
            IsBusinessDay = if (if Date.DayOfWeek([Date], weekStart) > 4 then false else true) and (if HolidaysProvided then not List.Contains(Holidays, [Date]) else true) then true else false,
            Day Type = if HolidaysProvided and List.Contains(Holidays, [Date]) then "Holiday" 
                else if (if Date.DayOfWeek([Date], weekStart) > 4 then false else true) = false then "Weekend" 
                else if (if Date.DayOfWeek([Date], weekStart) > 4 then false else true) = true then "Weekday" else null
        ], type [Day of Week Number=Int64.Type, Day of Week Name=text, Day of Week Initial=text, Day of Year=Int64.Type, DateInt=Int64.Type, CurrDayOffset=Int64.Type, IsAfterToday=logical, IsWeekDay=logical, IsHoliday= (if HolidaysProvided then Logical.Type else Text.Type), IsBusinessDay=logical, Day Type=text]),
        ExpandedDay = Table.ExpandRecordColumn(AddDayRecord, "DayRecord", 
            {"Day of Week Number", "Day of Week Name", "Day of Week Initial", "Day of Year", "DateInt", "CurrDayOffset", "IsAfterToday", "IsWeekDay", "IsHoliday", "IsBusinessDay", "Day Type"}),

        /* === ISO Columns === */
        InsertISOYear = Table.AddColumn(ExpandedDay, "ISO Year", each Date.Year(Date.AddDays(Date.StartOfWeek([Date], weekStart), 3)), Int64.Type),
        InsertISOqNum = Table.AddColumn(InsertISOYear, "ISO Quarter Number", each if [Week Number] > 39 then 4 else if [Week Number] > 26 then 3 else if [Week Number] > 13 then 2 else 1, Int64.Type),
        InsertISOqtr = Table.AddColumn(InsertISOqNum, "ISO Quarter", each "Q" & Number.ToText([ISO Quarter Number]), type text),
        InsertISOQuarter = Table.AddColumn(InsertISOqtr, "ISO Quarter & Year", each "Q" & Number.ToText([ISO Quarter Number]) & " " & Number.ToText([ISO Year]), type text),
        InsertISOqNy = Table.AddColumn(InsertISOQuarter, "ISO QuarternYear", each [ISO Year] * 10 + [ISO Quarter Number], Int64.Type),

        /* === Fiscal Columns === */
        AddFYnr = Table.AddColumn(InsertISOqNy, "FY number", each (if [Month] >= FYStartMonth and FYStartMonth >1 then [Year] +1 else [Year]), Int64.Type),
        AddFY = Table.AddColumn(AddFYnr, "Fiscal Year", each "FY" & Text.From([FY number]), type text),
        AddFQ = Table.AddColumn(AddFY, "Fiscal Quarter", each "FQ" & Text.From( Number.RoundUp( Date.Month( Date.AddMonths( [Date], - (FYStartMonth -1) )) / 3 )) & " " & Text.From([FY number]), type text),
        AddFQnYr = Table.AddColumn(AddFQ, "FQuarternYear", each [FY number] * 10 + Number.RoundUp( Date.Month( Date.AddMonths( [Date], - (FYStartMonth -1) )) / 3 ), type number),
        AddFM = Table.AddColumn(AddFQnYr, "Fiscal Period Number", each if [Month] >= FYStartMonth and FYStartMonth >1 then [Month] - (FYStartMonth-1) else if [Month] >= FYStartMonth and FYStartMonth =1 then [Month] else [Month] + (12-FYStartMonth+1), type number),
        AddFP = Table.AddColumn(AddFM, "Fiscal Period", each "FP" & Text.PadStart( Text.From([Fiscal Period Number]), 2, "0") & " " & Text.From([FY number]), type text),
        AddFMnYr = Table.AddColumn(AddFP , "FPeriodnYear", each [FY number] * 100 + [Fiscal Period Number], type number),
        FYCalendarStart = #date( Date.Year(StartDate)-1, FYStartMonth, 1 ),
        InsertFFD = Table.AddColumn(AddFMnYr, "FiscalFirstDay", each if [Month] >= FYStartMonth then #date([Year], FYStartMonth, 1) else #date([Year]-1, FYStartMonth, 1), type date),
        InsertFiscalWeekNumber = Table.AddColumn(InsertFFD, "Fiscal Week Number", each let
            FiscalWeekStart = Date.StartOfWeek([FiscalFirstDay], weekStart),
            CurrentWeekStart = Date.StartOfWeek([Date], weekStart),
            WeekDiff = Duration.Days(CurrentWeekStart - FiscalWeekStart) / 7
            in Number.RoundDown(WeekDiff) + 1, Int64.Type
        ),
        FWlogic = List.Contains( {null}, FYStartMonthNum),
        UpdateFYWeek = if FWlogic then Table.ReplaceValue(InsertFiscalWeekNumber, each [Fiscal Week Number], each if FYStartMonth =1 then [Week Number] else [Fiscal Week Number], Replacer.ReplaceValue, {"Fiscal Week Number"}) else InsertFiscalWeekNumber,
        AddFYW = Table.AddColumn( UpdateFYWeek, "Fiscal Week", each if FWlogic then "F" & [#"Week & Year"] else "FW" & Text.PadStart( Text.From([Fiscal Week Number]), 2, "0") & " " & Text.From([FY number]), type text),
        InsertFWeeknYear = Table.AddColumn(AddFYW, "FWeeknYear", each if FWlogic then [WeeknYear] else [FY number] * 100 + [Fiscal Week Number],  Int64.Type),

        /*  === Get Current Date Values === */
        CurrentDateRecord = Table.SelectRows(InsertFWeeknYear, each ([Date] = CurrentDate)),
        CurrentISOyear = CurrentDateRecord{0}[ISO Year],
        CurrentISOqtr = CurrentDateRecord{0}[ISO Quarter Number],
        CurrentYear = CurrentDateRecord{0}[Year],
        CurrentMonth = CurrentDateRecord{0}[Month],
        CurrentFiscalFirstDay = CurrentDateRecord{0}[FiscalFirstDay],
        PrevFiscalFirstDay = Date.AddYears(CurrentFiscalFirstDay, -1),
        CurrentFY = CurrentDateRecord{0}[Fiscal Year],
        CurrentFQ = CurrentDateRecord{0}[FQuarternYear],
        CurrentFP = CurrentDateRecord{0}[FPeriodnYear],
        CurrentFW = CurrentDateRecord{0}[FWeeknYear],

        /*  === Others & Clean up === */
        InsertISOYrOffset = Table.AddColumn(InsertFWeeknYear, "ISO CurrYearOffset", each [ISO Year] - CurrentISOyear, Int64.Type),
        InsertISOQtrOffset = Table.AddColumn(InsertISOYrOffset, "ISO CurrQuarterOffset", each ((4 * [ISO Year]) + [ISO Quarter Number]) - ((4 * CurrentISOyear) + CurrentISOqtr), Int64.Type),
        InsertFYoffset = Table.AddColumn(InsertISOQtrOffset, "Fiscal CurrYearOffset", each try (if [Month] >= FYStartMonth then [Year] + 1 else [Year]) - (if CurrentMonth >= FYStartMonth then CurrentYear + 1 else CurrentYear) otherwise null, Int64.Type),
        InsertCurrentFY = Table.AddColumn(InsertFYoffset, "IsCurrentFY", each if [Fiscal Year] = CurrentFY then true else false, type logical),
        InsertCurrentFQ = Table.RemoveColumns( Table.AddColumn(InsertCurrentFY, "IsCurrentFQ", each if [FQuarternYear] = CurrentFQ then true else false, type logical), {"FY number"} ),
        InsertCurrentFP = Table.AddColumn(InsertCurrentFQ, "IsCurrentFP", each if [FPeriodnYear] = CurrentFP then true else false, type logical),
        InsertCurrentFW = Table.AddColumn(InsertCurrentFP, "IsCurrentFW", each if [FWeeknYear] = CurrentFW then true else false, type logical),
        InsertPYTD = Table.AddColumn(InsertCurrentFW, "IsPYTD", each if CurrentYear-1 = [Year] and [Day of Year] <= CurrentDateRecord{0}[Day of Year] then true else false, type logical),
        InsertPFYTD = Table.AddColumn(InsertPYTD, "IsPFYTD", each if [Fiscal CurrYearOffset] = -1 then Duration.Days([Date] - PrevFiscalFirstDay) + 1 <= Duration.Days(CurrentDate - CurrentFiscalFirstDay) + 1 else false, type logical),
        InsertNetWorkdays = if AddRelativeNetWorkdays = true then Table.AddColumn(InsertPFYTD, "Relative Networkdays", each fxNETWORKDAYS( StartDate, [Date], Holidays ), Int64.Type) else InsertPFYTD,
        fxNETWORKDAYS = (StartDate, EndDate, optional Holidays as list) =>
        let
            ListOfDates = List.Dates( StartDate, Number.From(EndDate-StartDate)+1, Duration.From(1) ),
            DeleteHolidays = if Holidays = null then ListOfDates else List.Difference( ListOfDates, List.Transform(Holidays, Date.From )),
            DeleteWeekends = List.Select( DeleteHolidays, each Date.DayOfWeek( _, weekStart) < 5 ),
            CountDays = List.Count( DeleteWeekends)
        in
            CountDays,
        RemoveToday = Table.RemoveColumns(if not List.Contains(Source, CurrentDate) then Table.SelectRows(InsertNetWorkdays, each ([Date] <> CurrentDate)) else InsertNetWorkdays, {"Day of Year", "FiscalFirstDay"}),
        ChType = Table.TransformColumnTypes(RemoveToday, {{"Year", Int64.Type}, {"Quarter Number", Int64.Type}, {"Month", Int64.Type}, {"Day of Month", Int64.Type}, {"DateInt", Int64.Type}, {"Day of Week Number", Int64.Type}, {"ISO CurrYearOffset", Int64.Type}, {"ISO QuarternYear", Int64.Type}, {"ISO CurrQuarterOffset", Int64.Type}, {"Week Number", Int64.Type}, {"WeeknYear", Int64.Type}, {"MonthnYear", Int64.Type}, {"QuarternYear", Int64.Type}, {"FQuarternYear", Int64.Type}, {"Fiscal Period Number", Int64.Type}, {"FPeriodnYear", Int64.Type}, {"CurrWeekOffset", Int64.Type}, {"CurrMonthOffset", Int64.Type}, {"CurrQuarterOffset", Int64.Type}, {"CurrYearOffset", Int64.Type}, {"Fiscal CurrYearOffset", Int64.Type}, {"Fiscal Week Number", Int64.Type}}),
        ReorderCols = Table.ReorderColumns(ChType, {"Date", "Year", "CurrYearOffset", "YearCompleted", "Quarter Number", "Quarter", "Start of Quarter", "End of Quarter", "Quarter & Year", "QuarternYear", "CurrQuarterOffset", "QuarterCompleted", "Month", "Start of Month", "End of Month", "Month & Year", "MonthnYear", "CurrMonthOffset", "MonthCompleted", "Month Name", "Month Short", "Month Initial", "Day of Month", "Week Number", "Start of Week", "End of Week", "Week & Year", "WeeknYear", "CurrWeekOffset", "WeekCompleted", "Day of Week Number", "Day of Week Name", "Day of Week Initial", "DateInt", "CurrDayOffset", "IsAfterToday", "IsWeekDay", "IsHoliday", "IsBusinessDay", "Day Type", "ISO Year", "ISO CurrYearOffset", "ISO Quarter Number", "ISO Quarter", "ISO Quarter & Year", "ISO QuarternYear", "ISO CurrQuarterOffset", "Fiscal Year", "Fiscal CurrYearOffset", "Fiscal Quarter", "FQuarternYear", "Fiscal Period Number", "Fiscal Period", "FPeriodnYear", "Fiscal Week Number", "Fiscal Week", "FWeeknYear", "IsCurrentFY", "IsCurrentFQ", "IsCurrentFP", "IsCurrentFW", "IsPYTD", "IsPFYTD"}),
        ListCols = if FWlogic then Table.RemoveColumns(ReorderCols, {"ISO Quarter Number", "Fiscal Year", "Fiscal Quarter", "FQuarternYear", "Fiscal Period Number", "Fiscal Period", "FPeriodnYear", "Fiscal Week Number", "Fiscal Week", "FWeeknYear", "Fiscal CurrYearOffset", "IsCurrentFY", "IsCurrentFQ", "IsCurrentFP", "IsCurrentFW", "IsPFYTD"}) else Table.RemoveColumns(ReorderCols, {"Fiscal Period Number", "Fiscal Week Number", "ISO Quarter Number"})
    in
        ListCols
in
    Value.ReplaceType(fnDateTable, Value.ReplaceMetadata(Value.Type(fnDateTable), [
        Documentation.Name = " fxCalendar", 
        Documentation.Description = " Date table function to create an ISO-8601 calendar", 
        Documentation.LongDescription = " Date table function to create an ISO-8601 calendar", 
        Documentation.Category = " Table", 
        Documentation.Version = " 2.04: Optimized Version",
        Documentation.Source = " local", 
        Documentation.Author = " Melissa de Korte", 
        Documentation.Examples = { [Description = " See: https://forum.enterprisedna.co/t/extended-date-table-power-query-m-function/6390", 
            Code = " Optional paramters: #(lf)(FYStartMonthNum) Month number the fiscal year starts, Januari if omitted #(lf)(Holidays) Select a query (column) that contains a list of holiday dates #(lf)(WDStartNum) Switch default weekday numbering from 0-6 to 1-7 by entering a 1 #(lf)(AddRelativeNetWorkdays) if true adds a Relative Networkdays column to the date table #(lf)
                #(lf)Important notes: #(lf)[Fiscal Week] starts on a Monday and can contain less than 7 days in a First- and/or Last Week of a FY #(lf)[IsWeekDay] does not take holiday dates into account #(lf)[IsBusinessDay] does take optional holiday dates into account  #(lf)[IsPYTD] and [IsPFYTD] compare Previous [Day of Year] with the Current [Day of Year] number, so dates don't align in leap years #(lf)> No Fiscal columns will be added if (FYStartMonthNum) is omitted!", 
            Result = " " ] }
    ]))

.
Don’t want to invoke a function?
With the minor changes depicted below the output of the M code will be a table.

.
How to make a date table fully dynamic?

.
To many columns for your bussiness requirement?
By adding a single step you can select only those columns that are of intrest to you. In this video I’ll walk you throught how to do just that.

.
But wait, how do I sort that column?
@BrianJ has created a Cheat Sheet for this date table, it lists an example value, the data type and appropriate sort by columns. You can find that here.

.
Want to quickly setup your Dates table with Tabular Editor?
This script requires a date table with the name Dates and a Date key column, if you call yours something else, that’s fine of course, just update the name between double quotes in the first two variables.
Make other changes according to your personal preference.

var dateTableName = "Dates"; // Name of your date table
var dateKeyColName = "Date"; // Name of the date key column in the table
var colOperations = new List<Tuple<string, string, bool>>(){
    Tuple.Create("Quarter & Year", "QuarternYear", true),
    Tuple.Create("Month & Year", "MonthnYear", true),
    Tuple.Create("Month Name", "MonthOfYear", true),
    Tuple.Create("Month Short", "MonthOfYear", true),
    Tuple.Create("Month Initial", "MonthOfYear", true),
    Tuple.Create("Week & Year", "WeeknYear", true),
    Tuple.Create("Day of Week Name", "DayOfWeek", true),
    Tuple.Create("Day of Week Initial", "DayOfWeek", true),
    Tuple.Create("ISO Quarter & Year", "ISO QuarternYear", true),
    Tuple.Create("ISO Quarter", "ISO QuarterOfYear", true),
    Tuple.Create("Fiscal Year", "FiscalYearOffset", false),
    Tuple.Create("Fiscal Quarter", "FQuarternYear", false),
    Tuple.Create("Fiscal Period", "FPeriodnYear", false),
    Tuple.Create("Fiscal Week", "FWeeknYear", false)
};
var dateTable = Model.Tables.FirstOrDefault(t => t.Name == dateTableName);
if (dateTable != null){
    dateTable.DataCategory = "Time"; // Mark as Date table

    foreach (var column in dateTable.Columns){
        var operation = colOperations.FirstOrDefault(op => op.Item1 == column.Name);
        if (operation != null){
            var sortByColumn = dateTable.Columns.FirstOrDefault(c => c.Name == operation.Item2);
            if (sortByColumn != null){
                column.SortByColumn = sortByColumn; // Set Sort By
                sortByColumn.IsHidden = operation.Item3; // Set Hidden
            }
        }

        switch (column.DataType){
            case DataType.Double:
            case DataType.Int64:
            column.SummarizeBy = AggregateFunction.None; // Set Summarize By
            column.FormatString = "0"; // Set format numeric fields
            break;
            case DataType.DateTime:
            column.FormatString = "Short Date"; // Set format date fields
            break;
        }
    }

    var dateColumn = dateTable.Columns.FirstOrDefault(c => c.Name == dateKeyColName);
    if (dateColumn != null){
        dateColumn.IsKey = true; // Mark the column as a key
    }
}

.
Want to create your own Date table?
Keep these requirements in mind for Time Intelligence functions to work properly:

  • Must have a column of data type date (or date/time )—known as the date column
  • The date column must contain unique values
  • The date column must not contain BLANKs
  • The date column must not have any missing dates
  • The date column must span full years. Where a year isn’t necessarily a calendar year (January-December). And cover all dates from the earliest- to the last date present in your dataset.
  • The date table must be marked as a date table

.

You can let me know you’ve appreciated this post by giving it a :heart:

Note. If you always copy the latest version of the M code above, you can take advantage of modifications and/or updates as soon as they become available…

96 Likes

Just adding some other search terms: power query calendar table, calendar function

5 Likes

Hi Melissa:

This really cool and thoughtful of you for sharing!

Best regards,

Bill S

2 Likes

Thanks @Melissa, you are awesome :slight_smile:
Have a beautiful day
Najah

2 Likes

Agree just amazing work.

1 Like

Any chance we can get a separate Extended Time Table seeing we are suggested to split out time from date. Would be great if we could get an Extended Time Table to go with it. :slight_smile:

1 Like

Hi David,

Makes sense, you can find it here:

4 Likes

Thank you again Melissa!

1 Like

Very good Melissa. I’ve just voted for a new virtual pbi training about all this. I really hope you guys manage to get some time to do it.
Thank you so much

1 Like

Hi Pedro,

As a matter of fact @BrianJ and I are collaborating on a Time Intelligence series as we speak and of course we will also be covering the Date table. So if you haven’t already, subscribe to the Enterprise DNA YouTube channel so you won’t miss a thing! :wink:

4 Likes

oooohhhh that is soooooooo cooooll!!! and yes i am a dna youtube subscriber for quite a while now. And it’s a great idea to teach also about TI measures concerning last week or weeks or month or quarter regarding the beginning of the year.
Thanks a lot Melissa. That is great news!!!

1 Like

Muchas gracias Melisa, Awesome

1 Like

This is very helpful Melisa, you are a rockstar here.

1 Like

@Melissa I need to adjust the ISO Year column to match our “week-years.”
Our weeks run Sunday - Saturday, and our billing “months” end on the last Saturday in the calendar month.
I have been trying to adjust the code in the InsertWeekNumber= line to match this calendar. Can you explain out the last ‘else’ line in that code?
else Number.RoundDown((Date.DayOfYear([Date])-(Date.DayOfWeek([Date], Day.Sunday)+1)+10)/7), type number),
Specifically - Why is it adding 1, then 10 before dividing by 7?
Learning a ton here. . .
Thank you.

1 Like

Hi @kjssdca,

Thanks for your question.

By default the Power Query Date.DayOfWeek function returns a number between 0 and 6 starting on Sunday. To return the weekday number according to ISO8601 rules, you have to add the optional first-weekday value (= Day.Monday) and change the numbering so it runs between 1 and 7.
This explains the +1

The +10 / 7 is part of the algorithm for calculating the week number, you can find more on that here.

If you need assistance creating a customized week number, please create a new topic. You’ll find that there’s an increasing number of active members here who’d like to help out. :wink:

2 Likes

Thank you @Melissa for sharing! Date Table is getting better and better :+1:

Your code has opened my appetite to keep the “EndDate” dynamic, so it will always be the last day of the current year. That would save me from updating/maintaining the Dates table in all my reports next year(unless there is another way which I am not aware of :smiley: )

Using your M code, I have:
1- Removed “EndDate as date” from the first line and;
2- Added another line “EndDate = Date.EndOfYear(CurrentDate),” after the “Current Date”.

2 Likes

Hi @Hesham,

Within this category you can also find a topic on creating a dynamic Start- and Enddate.

.
In addition there will also be a video on that topic in the Time Intelligence series Brian and I are doing. We’ll add a link here as soon as it’s released to the Enterprise DNA Channel.

.
Thanks for sharing your method

4 Likes

Thanks @Melissa!!!

This post came just right in time!!

:+1: :+1: :+1:

2 Likes

Hi Melissa,
The optional parameters work fine, would it be possible to add a selection parameter to show Monday as Weekday 1 please, so users can select either 0 or 1.
Paul

1 Like

Hi Paul,

Thanks for your question!

:thinking: That sounds like a nice addition indeed. So added the optional WDStartNum parameter this allows you to set the [DayOfWeek] numbering from either 0-6 or 1-7. However if omitted, the standard numbering will be 0-6.

Keep in mind that this doesn’t affect the start of the week day, that will always remain Monday as is custom to this Calendar and the ISO week logic.

3 Likes