Hey @Melissa,
I asked the same questions but I guess some people feel the need to create things that are way far into the future. Completely not right (I know!) and there may be something suspicious going on, but there are actual documents that some individuals created that date to 2046.
I’ll share with you the original M-script (rather than the one where I’ve added the Table.Buffer).
Please don’t judge me for horrible scripting! I’m new to M and had to figure this out in the last few weeks. It’s a custom 445 calendar with weeks ending on Fridays (new week begins Saturday) and with 1/1 and 12/31 as being the start and end of each year.
let fnDateTable = (StartDate as date, EndDate as date) as table =>
let
DayCount = Duration.Days(Duration.From(Date.AddDays(EndDate,1) - StartDate)),
Source = List.Dates(StartDate,DayCount,#duration(1,0,0,0)),
TableFromList = Table.FromList(Source, Splitter.SplitByNothing()),
ChangedType = Table.TransformColumnTypes(TableFromList,{{“Column1”, type date}}),
RenamedColumns = Table.RenameColumns(ChangedType,{{“Column1”, “Date”}}),
InsertYear = Table.AddColumn(RenamedColumns, “Year”, each Date.Year([Date])),
InsertCalendarMonth = Table.AddColumn(InsertYear, “CalendarMonthofYear”, each Date.Month([Date])),
InsertCalendarDay = Table.AddColumn(InsertCalendarMonth, “CalendarDayofMonth”, each Date.Day([Date])),
InsertDateInt = Table.AddColumn(InsertCalendarDay, “DateInt”, each [Year] * 10000 + [CalendarMonthofYear] * 100 + [CalendarDayofMonth]),
InsertDayWeek = Table.AddColumn(InsertDateInt, “DayInWeek”, each Date.DayOfWeek([Date])),
InsertDayName = Table.AddColumn(InsertDayWeek, “DayOfWeekName”, each Date.ToText([Date], “dddd”)),
Added1stofYear = Table.AddColumn(InsertDayName, “1st of Year”, each Date.StartOfYear([Date]), type date),
AddedP01Close = Table.AddColumn(Added1stofYear, “P01 Close”,
each if Date.DayOfWeek([1st of Year]) = 1 then Date.AddDays([1st of Year],25)
else if Date.DayOfWeek([1st of Year]) = 2 then Date.AddDays([1st of Year],24)
else if Date.DayOfWeek([1st of Year]) = 3 then Date.AddDays([1st of Year],23)
else if Date.DayOfWeek([1st of Year]) = 4 then Date.AddDays([1st of Year],29)
else if Date.DayOfWeek([1st of Year]) = 5 then Date.AddDays([1st of Year],28)
else if Date.DayOfWeek([1st of Year]) = 6 then Date.AddDays([1st of Year],27)
else if Date.DayOfWeek([1st of Year]) = 7 then Date.AddDays([1st of Year],26)
else 0, type date),
AddedP02Close = Table.AddColumn(AddedP01Close, “P02 Close”, each Date.AddDays([P01 Close],28), type date),
AddedP03Close = Table.AddColumn(AddedP02Close, “P03 Close”, each Date.AddDays([P02 Close],35), type date),
AddedP04Close = Table.AddColumn(AddedP03Close, “P04 Close”, each Date.AddDays([P03 Close],28), type date),
AddedP05Close = Table.AddColumn(AddedP04Close, “P05 Close”, each Date.AddDays([P04 Close],28), type date),
AddedP06Close = Table.AddColumn(AddedP05Close, “P06 Close”, each Date.AddDays([P05 Close],35), type date),
AddedP07Close = Table.AddColumn(AddedP06Close, “P07 Close”, each Date.AddDays([P06 Close],28), type date),
AddedP08Close = Table.AddColumn(AddedP07Close, “P08 Close”, each Date.AddDays([P07 Close],28), type date),
AddedP09Close = Table.AddColumn(AddedP08Close, “P09 Close”, each Date.AddDays([P08 Close],35), type date),
AddedP10Close = Table.AddColumn(AddedP09Close, “P10 Close”, each Date.AddDays([P09 Close],28), type date),
AddedP11Close = Table.AddColumn(AddedP10Close, “P11 Close”, each Date.AddDays([P10 Close],28), type date),
AddedP12Close = Table.AddColumn(AddedP11Close, “P12 Close”, each Date.EndOfYear([Date]), type date),
AddedCurrentPeriodClose = Table.AddColumn(AddedP12Close, “Current Period Close”, each if [Date] <=[P01 Close] then [P01 Close]
else if [Date] <=[P02 Close] and [Date] > [P01 Close] then [P02 Close]
else if [Date] <=[P03 Close] and [Date] > [P02 Close] then [P03 Close]
else if [Date] <=[P04 Close] and [Date] > [P03 Close] then [P04 Close]
else if [Date] <=[P05 Close] and [Date] > [P04 Close] then [P05 Close]
else if [Date] <=[P06 Close] and [Date] > [P05 Close] then [P06 Close]
else if [Date] <=[P07 Close] and [Date] > [P06 Close] then [P07 Close]
else if [Date] <=[P08 Close] and [Date] > [P07 Close] then [P08 Close]
else if [Date] <=[P09 Close] and [Date] > [P08 Close] then [P09 Close]
else if [Date] <=[P10 Close] and [Date] > [P09 Close] then [P10 Close]
else if [Date] <=[P11 Close] and [Date] > [P10 Close] then [P11 Close]
else if [Date] <=[P12 Close] and [Date] > [P11 Close] then [P12 Close]
else 0, type date),
InsertPostingPeriod = Table.AddColumn(AddedCurrentPeriodClose, “Posting Period”, each if [Date] <=[P01 Close] then “01”
else if [Date] <=[P02 Close] and [Date] > [P01 Close] then “02”
else if [Date] <=[P03 Close] and [Date] > [P02 Close] then “03”
else if [Date] <=[P04 Close] and [Date] > [P03 Close] then “04”
else if [Date] <=[P05 Close] and [Date] > [P04 Close] then “05”
else if [Date] <=[P06 Close] and [Date] > [P05 Close] then “06”
else if [Date] <=[P07 Close] and [Date] > [P06 Close] then “07”
else if [Date] <=[P08 Close] and [Date] > [P07 Close] then “08”
else if [Date] <=[P09 Close] and [Date] > [P08 Close] then “09”
else if [Date] <=[P10 Close] and [Date] > [P09 Close] then “10”
else if [Date] <=[P11 Close] and [Date] > [P10 Close] then “11”
else if [Date] <=[P12 Close] and [Date] > [P11 Close] then “12”
else 0),
InsertReportingDate = Table.AddColumn(InsertPostingPeriod, “Reporting Date”, each [Posting Period]&"/"&“01”&"/"& Text.From(Date.Year([Date]))),
ChangedType1 = Table.TransformColumnTypes(InsertReportingDate,{{“Reporting Date”, type date}}),
RemovedColumns = Table.RemoveColumns(InsertReportingDate,{“1st of Year”, “P01 Close”, “P02 Close”, “P03 Close”, “P04 Close”, “P05 Close”, “P06 Close”, “P07 Close”, “P08 Close”, “P09 Close”, “P10 Close”, “P11 Close”, “P12 Close”}),
//InsertCloseYear = Table.AddColumn(RemovedColumns, “CloseYear”, each Date.Year([Current Period Close]),type text),
InsertYearNumber = Table.AddColumn(RemovedColumns, “YearNumber”, each Date.Year([Current Period Close])),
InsertShortYear = Table.AddColumn(InsertYearNumber, “ShortYear”, each Text.End(Text.From(Date.Year([Current Period Close])),2)),
InsertQuarterInt = Table.AddColumn(InsertShortYear, “QuarterInt”, each if Date.Month([Current Period Close]) < 4 then 1
else if Date.Month([Current Period Close]) > 3 and Date.Month([Current Period Close]) <7 then 2
else if Date.Month([Current Period Close]) >6 and Date.Month([Current Period Close]) < 10 then 3
else 4),
InsertQuarterofYear = Table.AddColumn(InsertQuarterInt, “QuarterofYear”, each if Date.Month([Current Period Close]) = 1 then “Q1”
else if Date.Month([Current Period Close]) = 2 then “Q1”
else if Date.Month([Current Period Close]) = 3 then “Q1”
else if Date.Month([Current Period Close]) = 4 then “Q2”
else if Date.Month([Current Period Close]) = 5 then “Q2”
else if Date.Month([Current Period Close]) = 6 then “Q2”
else if Date.Month([Current Period Close]) = 7 then “Q3”
else if Date.Month([Current Period Close]) = 8 then “Q3”
else if Date.Month([Current Period Close]) = 9 then “Q3”
else “Q4”),
InsertQuarterYear = Table.AddColumn(InsertQuarterofYear, “QuarterYear”, each [QuarterofYear] & “-” & Text.From(Date.Year([Current Period Close]))),
InsertQuarterYearInt = Table.AddColumn(InsertQuarterYear, “QuarterYearInt”, each [YearNumber] *10000 + [QuarterInt] * 100),
InsertMonthofYear = Table.AddColumn(InsertQuarterYearInt, “MonthCloseofYear”, each Date.Month([Current Period Close])),
InsertMonthYearInt = Table.AddColumn(InsertMonthofYear, “MonthCloseYearInt”, each [YearNumber] * 10000 + [MonthCloseofYear] *100),
InsertMonthName = Table.AddColumn(InsertMonthYearInt,“MonthCloseName”, each Date.ToText([Current Period Close], “MMMM”)),
InsertYearMonth =Table.AddColumn(InsertMonthName, “YearMonthClose”, each Text.From(Date.Year([Current Period Close])) & “/” & Text.From(Date.Month([Current Period Close]))),
InsertWeekCal = Table.AddColumn(InsertYearMonth, “WeekEndStaging”, each if [DayOfWeekName] = “Saturday” then 6
else if [DayOfWeekName] = “Sunday” then 5
else if [DayOfWeekName] = “Monday” then 4
else if [DayOfWeekName] = “Tuesday” then 3
else if [DayOfWeekName] = “Wednesday” then 2
else if [DayOfWeekName] = “Thursday” then 1
else 0),
InsertWeekEnding1 = Table.AddColumn(InsertWeekCal, “WeekEnding1”, each Date.AddDays([Date], [WeekEndStaging])),
InsertWeekEnding2 = Table.AddColumn(InsertWeekEnding1,“WeekEnding”,each if Duration.Days(#date(Number.From([Year]),12,31) - [Date]) < 6 and Date.Year([WeekEnding1]) <> [YearNumber] then #date(Number.From([Year]),12,31) else [WeekEnding1], type date),
InsertWeekEndingNumber = Table.AddColumn(InsertWeekEnding2, “WeekEndingNumber”, each (Number.From(Date.Year([WeekEnding])) * 10000) + (Number.From(Date.Month([WeekEnding])) * 100) + Number.From(Date.Day([WeekEnding])), Int64.Type),
InsertWeekofYear = Table.AddColumn(InsertWeekEndingNumber,“WeekofYear”, each Date.WeekOfYear([Date], Day.Saturday)),
RemovedColumns1 = Table.RemoveColumns(InsertWeekofYear,{“WeekEndStaging”, “WeekEnding1”}),
ChangedType2 = Table.TransformColumnTypes(RemovedColumns1,{{“CalendarDayofMonth”, Int64.Type},{“DateInt”, Int64.Type},{“MonthCloseYearInt”, Int64.Type}, {“QuarterYearInt”, Int64.Type}, {“QuarterInt”, Int64.Type}, {“YearNumber”, Int64.Type}})
in
ChangedType2
in
fnDateTable