Site header

Comprehensive calendar table in Power Query

The M code below can be used in Power Query to produce a comprehensive calendar table. This table will run from January 1st 2020 (the StartDate) to the end of the current year. The starting date can be amended by changing the highlighted text next to StartDate.

To create a table with this code in Power BI click Get data and then Blank query. Next select Advanced Editor and enter the code. Click on the 'Copy Code' button to copy the M code below:

let
  StartDate = #date(2020, 1, 1),
  
  // CREATE TABLE OF DATES
  Today = Date.From(DateTime.LocalNow()),
  EndDate = Date.EndOfYear(Today),
  #"List of Dates" = List.Dates(StartDate, Duration.Days(EndDate - StartDate) +1, #duration(1, 0, 0, 0)),
  #"Converted to Table" = Table.FromList(#"List of Dates", Splitter.SplitByNothing(), type table[Date = Date.Type]),
  
  // ADD COLUMNS
  // Add Year (eg. 2023)
  #"Add Year" = Table.AddColumn(#"Converted to Table", "Year", each Date.Year([Date]), Int64.Type),
  
  // Add dynamic year that shows 'Current Year', 'Last Year', or the year number (if >1 years ago)
  #"Add Year Default" = Table.AddColumn(#"Add Year", "Year (with current/last)", each if Date.Year(Today) = [Year] then "Current Year" else if Date.Year(Today)-1 = [Year] then "Last Year" else Text.From([Year]), type text),
  
  // Add Year-Month (eg. 2023-01)
  #"Add Year-Month" = Table.AddColumn(#"Add Year Default", "Year-Month", each Date.ToText([Date], "yyyy-MM"), type text),
  
  // Add Month-Year (eg. Jan 2023)
  #"Add Month-Year" = Table.AddColumn(#"Add Year-Month", "Month-Year", each Date.ToText([Date], "MMM yyyy"), type text),
  
  // Add Month Number (1-12)
  #"Add Month Number" = Table.AddColumn(#"Add Month-Year", "Month Of Year", each Date.Month([Date]), Int64.Type),
  
  // Add full and abbreviated month name (eg. Jan / January)
  #"Add Month Name Short" = Table.AddColumn(#"Add Month Name", "Month Name Short", each Date.ToText([Date] , "MMM", "EN-us"), type text),
  #"Add Month Name" = Table.AddColumn(#"Add Month Number", "Month Name", each Date.MonthName([Date], "EN-us"), type text),
  
  // Add dynamic year that shows 'Current Month', 'Last Month', or the month name
  #"Add Month Name Default" = Table.AddColumn(#"Add Month Name Short", "Month Name (with current/last)", each if Date.Month(Today) = [Month Of Year] then "Current Month" else if Date.Month(Today)-1 = [Month Of Year] then "Last Month" else [Month Name], type text),
  
  // Add start and end of month date (eg. 01/01/2023 / 31/01/2023)
  #"Add Start of Month" = Table.AddColumn(#"Add Month Name Default", "Start of Month", each Date.StartOfMonth([Date]), type date),
  #"Added End of Month" = Table.AddColumn(#"Add Start of Month", "End of Month", each Date.EndOfMonth([Date]), type date),
  
  // Add number of days in month (eg. 31 for January)
  #"Added Days in Month" = Table.AddColumn(#"Added End of Month", "Days in Month", each Date.DaysInMonth([Date]), Int64.Type),
  
  // Add start of week date
  #"Add Start of Week" = Table.AddColumn(#"Added Days in Month", "Start of Week", each Date.StartOfWeek([Date], Day.Monday), type date),
  
  // Add quarter number
  #"Add Quarter Number" = Table.AddColumn(#"Add Start of Week", "Quarter Number", each Date.QuarterOfYear([Date]), Int64.Type),
  
  // Add Year-Quarter (eg. 2020-Q1)
  #"Add Year-Quarter" = Table.AddColumn(#"Add Quarter Number", "Year-Quarter", each Text.From(Date.Year([Date])) & "-Q" & Text.From(Date.QuarterOfYear([Date])), type text),
  
  // Add full and abbreviated day name (eg. Sun / Sunday)
  #"Add Day Name Short" = Table.AddColumn(#"Add Year-Quarter", "Day Name Short", each Date.ToText([Date], "ddd", "EN-us"), type text),
  #"Add Day Name" = Table.AddColumn(#"Add Day Name Short", "Day Name", each Date.DayOfWeekName([Date], "EN-us"), type text),
  
  // Add day of month/year number, showing number of days since start of each
  #"Add Day of Month Number" = Table.AddColumn(#"Add Day Name", "Day of Month Number", each Date.Day([Date]), Int64.Type),
  #"Add Day of Year" = Table.AddColumn(#"Add Day of Month Number", "Day of Year Number", each Date.DayOfYear([Date]), Int64.Type),
  
  // Add day/week/month/quarter offsets, showing how long ago each was from the current date
  #"Add Day Offset" = Table.AddColumn(#"Add Day of Year", "Day Offset", each Number.From([Date] - Date.From(Today)) , Int64.Type),
  #"Add Week Offset" = Table.AddColumn(#"Add Day Offset", "Week Offset", each (Number.From(Date.StartOfWeek([Date], Day.Monday))- Number.From(Date.StartOfWeek(Today, Day.Monday))) / 7, Int64.Type),
  #"Add Month Offset" = Table.AddColumn(#"Add Week Offset", "Month Offset", each ([Year] - Date.Year(Today)) *12 + ([Month Of Year] - Date.Month(Today)), Int64.Type),
  #"Add Quarter Offset" = Table.AddColumn(#"Add Month Offset", "Quarter Offset", each ([Year] - Date.Year(Today)) * 4 + Date.QuarterOfYear([Date]) - Date.QuarterOfYear(Today), Int64.Type),
  #"Add Year Offset" = Table.AddColumn(#"Add Quarter Offset", "Year Offset", each [Year] - Date.Year(Today), Int64.Type),
  
  // Add weekday/weekend flag (0 = false, 1 = true)
  #"Add Is Weekend" = Table.AddColumn(#"Add Year Offset", "Is Weekend", each if Date.DayOfWeek([Date]) >= 5 then 1 else 0, Int64.Type),
  #"Add Is Weekday" = Table.AddColumn(#"Add Is Weekend", "Is Weekday", each if Date.DayOfWeek([Date]) < 5 then 1 else 0, Int64.Type)
  
in
  #"Add Is Weekday"