DEV Community

Luca Liu
Luca Liu

Posted on • Edited on

How to Create Calendar Table in Power BI?

Why do you generally need to create a date table in Power BI, while it is not necessary in Tableau?

In Power BI, a date table is necessary because the DAX language does not support built-in date functions, requiring the creation of a date table to perform date-related analysis. In Tableau, no date table is needed as it supports built-in date functions and features for direct use of date fields in analysis.

How to create a date table in Power BI?

To create a date table in Power BI using DAX, you can follow these steps:

  1. Open Power BI Desktop and go to the Modeling tab.
  2. Click on New Table in the Modeling tab to create a new table.

Image description

  1. In the formula bar, enter the DAX code to generate the date table. Here's an example of the DAX code to create a date table:


Calendar = ADDCOLUMNS (
    CALENDAR (date(2021,1,1),date(2023,12,31)),
    "Year", YEAR ( [Date] ),
    "Quarter", ROUNDUP( MONTH ( [Date] )/3,0 ),
    "Month", MONTH ( [Date] ),
    "Week", WEEKNUM([Date]),
    "Year Quarter", YEAR ( [Date] ) & "Q" & ROUNDUP( MONTH ( [Date] )/3,0 ) ,
    "Year Month", YEAR ( [Date] ) * 100 + MONTH ( [Date] ),
    "Year Week", YEAR ( [Date] ) * 100 + WEEKNUM ( [Date] ),
    "Weekday", WEEKDAY([Date])
    )


Enter fullscreen mode Exit fullscreen mode

This DAX code creates a calendar table with additional columns for year, quarter, month, week, year quarter, year month, year week, and weekday.

Here's a breakdown of each part of the code:

  1. Calendar = : This is the name of the new table being created.
  2. ADDCOLUMNS ( ... ): This function adds new columns to an existing table.
  3. CALENDAR (date(2021,1,1),date(2023,12,31)): This function creates a table with a single column of dates, starting from January 1, 2021, to December 31, 2023.
  4. Year, YEAR ( [Date] ): This creates a new column named Year which extracts the year from the date in the Date column.
  5. Quarter, ROUNDUP( MONTH ( [Date] )/3,0 ): This creates a new column named Quarter which calculates the quarter based on the month in the Date column. The ROUNDUP function rounds up the result to the nearest whole number.
  6. Month, MONTH ( [Date] ): This creates a new column named Month which extracts the month from the date in the Date column.
  7. Week, WEEKNUM([Date]): This creates a new column named Week which calculates the week number based on the date in the Date column.
  8. Year Quarter, YEAR ( [Date] ) & Q & ROUNDUP( MONTH ( [Date] )/3,0 ): This creates a new column named Year Quarter which combines the year and quarter information.
  9. Year Month, YEAR ( [Date] ) * 100 + MONTH ( [Date] ): This creates a new column named Year Month which combines the year and month information into a single number.
  10. Year Week, YEAR ( [Date] ) * 100 + WEEKNUM ( [Date] ): This creates a new column named Year Week which combines the year and week information into a single number.
  11. Weekday, WEEKDAY([Date]): This creates a new column named Weekday which calculates the day of the week (1 for Sunday, 2 for Monday, and so on) based on the date in the Date column.

In summary, this code creates a calendar table with additional columns to provide various ways of organizing and analyzing date-related data.

dynamic date table

To create a dynamic date table for the most recent three years, you can use the following code.



Calendar = ADDCOLUMNS (
    CALENDAR(DATE(YEAR(TODAY())-1,1,1),DATE(YEAR(TODAY())+1,12,31)),
    "Year", YEAR ( [Date] ),
    "Quarter", ROUNDUP( MONTH ( [Date] )/3,0 ),
    "Month", MONTH ( [Date] ),
    "Week", WEEKNUM([Date]),
    "Year Quarter", YEAR ( [Date] ) & "Q" & ROUNDUP( MONTH ( [Date] )/3,0 ) ,
    "Year Month", YEAR ( [Date] ) * 100 + MONTH ( [Date] ),
    "Year Week", YEAR ( [Date] ) * 100 + WEEKNUM ( [Date] ),
    "Weekday", WEEKDAY([Date])
    )


Enter fullscreen mode Exit fullscreen mode

In the current DAX code, the date range is specified as one year ago to one year ahead using DATE(YEAR(TODAY())-1,1,1) and DATE(YEAR(TODAY())+1,12,31). This determines the range of dates included in the dynamic date table.


Explore more

Thank you for taking the time to explore data-related insights with me. I appreciate your engagement.

πŸš€ Connect with me on LinkedIn

πŸŽƒ Connect with me on X

🌍 Connect with me on Instagram

Top comments (0)