Sub topic 4
Creating the table with DAX
This chapter assumes you can open Power BI Desktop's Table view and Model view, write a calculated column, and tell a fact table from a dimension table. You will create the date table as a calculated table, a table whose rows come from a DAX expression.
Start with CALENDAR
CALENDAR takes a start date and an end date. It returns a table with one column named Date and one row for each day in that range.
To make the range follow your data, derive the boundaries from the fact table:
Date =
CALENDAR (
DATE ( YEAR ( MIN ( Sales[OrderDate] ) ), 1, 1 ),
DATE ( YEAR ( MAX ( Sales[OrderDate] ) ), 12, 31 )
)To create it, go to Table view, choose New table on the ribbon, and paste the expression. MIN and MAX find the earliest and latest order dates. YEAR and DATE then move those boundaries to January 1 and December 31. This gives you whole years with no gaps, which a valid date table requires. The DATE function also drops any time portion in OrderDate.
CALENDAR or CALENDARAUTO
CALENDARAUTO takes no start and end dates. It scans every date and datetime column in the model and returns whole years that cover all of them. It is shorter, but it has a known problem: any stray date column widens the range. A birth date from 1960 or a placeholder like 1900-01-01 will add decades of empty rows.
| Function | Range comes from | Good for | Risk |
|---|---|---|---|
| `CALENDAR` | Dates you give it, or expressions | Control over the range | Range can be too narrow if you hard-code it |
| `CALENDARAUTO` | All date columns in the model | Quick prototypes | Unrelated date columns stretch the range |
I prefer CALENDAR with MIN and MAX on the columns that matter. Reading from one fact column is predictable.
Add calendar columns with ADDCOLUMNS
A single Date column is not enough for reports. You also need year, quarter, and month columns to use on axes and slicers. ADDCOLUMNS wraps the CALENDAR result and adds columns in the same expression:
Date =
ADDCOLUMNS (
CALENDAR ( DATE ( 2020, 1, 1 ), DATE ( 2026, 12, 31 ) ),
"Year", YEAR ( [Date] ),
"Quarter Number", QUARTER ( [Date] ),
"Quarter", "Q" & QUARTER ( [Date] ),
"Month Number", MONTH ( [Date] ),
"Month", FORMAT ( [Date], "MMM" ),
"Year-Month", FORMAT ( [Date], "yyyy-MM" )
)Each new column is a name in quotes followed by an expression. Inside ADDCOLUMNS, [Date] refers to the current row of the calendar table. The expression is evaluated once per day.
Notice the pairs of columns: Quarter Number with Quarter, and Month Number with Month. Text such as "Apr" sorts alphabetically in visuals, which puts April before January. The numeric columns exist so you can set Sort by column on the text ones. The next chapters cover that setup.
Common errors
- Circular dependency or blank table. If the table is named
Dateand a column inside it is also referenced asDate, check that you wrote[Date]without a table prefix insideADDCOLUMNS. A prefix usually points at the wrong object. - Range too short. A hard-coded end year of 2026 hides data from 2027. Use
MAXor update the year. - Partial years. Starting at
MIN ( Sales[OrderDate] )directly leaves a partial first year. Year-to-date and prior-year comparisons then give misleading numbers. - Duplicate or missing days.
CALENDARitself never skips days, so these appear only if you filter the table later. - Wrong data type. If
Dateshows as a date and time, wrap the inputs inDATEas shown. Marking the table later needs a column of type Date.
Exercise
Your Sales table has order dates from 14 March 2021 to 2 November 2024. Write a calculated table named Date that covers whole calendar years and extends automatically when newer orders are loaded. What are the first and last dates it contains?
Show answer
Use CALENDAR ( DATE ( YEAR ( MIN ( Sales[OrderDate] ) ), 1, 1 ), DATE ( YEAR ( MAX ( Sales[OrderDate] ) ), 12, 31 ) ). It starts on 1 January 2021 and ends on 31 December 2024. Because it uses MIN and MAX, it widens when newer orders arrive.