Sub topic 4
Creating the table with DAX
This chapter assumes you can open Power BI Desktop's Report, Table, and Model views. It also assumes you can write a basic DAX calculated column or measure, and that you know a fact table (such as Sales) from a dimension table (such as Date). You will build a date table as a DAX calculated table, choose its date range, and add calendar columns. Later chapters cover sorting, relationships, and marking the table.
What a calculated table is
A calculated table is a table whose rows come from a DAX expression instead of a data source. Power BI evaluates the expression when you create it and again at every model refresh. The result is stored in the model like any imported table.
To create one:
- In Report, Table, or Model view, select New table on the Table tools or Modeling ribbon.
- Type
Date = ...followed by a DAX expression that returns a table. - Press Enter. The new table appears in the Data pane.
The name before = becomes the table name. In this tutorial the table is called Date. Because Date is also a DAX function name, you must write it as 'Date'[Date] in some contexts, as the examples later in this chapter show.
Choosing the date range with CALENDAR
CALENDAR(start, end) returns a table with one column named Date. It has one row for every day from start to end, inclusive.
Date =
CALENDAR (
DATE ( YEAR ( MIN ( Sales[OrderDate] ) ), 1, 1 ),
DATE ( YEAR ( MAX ( Sales[OrderDate] ) ), 12, 31 )
)This expression reads the earliest and latest order dates in Sales. It takes only the year of each, then builds January 1 of the first year and December 31 of the last year. Two properties make the table valid for time intelligence:
- No gaps. Every calendar day in the range has a row.
- Whole years. Functions such as
TOTALYTDandSAMEPERIODLASTYEARassume the table covers complete years. If the table ended on, say, November 12, the last year would be incomplete and prior-year comparisons for that year would be missing days.
Rounding to whole years is the point of wrapping MIN and MAX in YEAR and DATE. Using CALENDAR(MIN(...), MAX(...)) directly would start and end mid-year.
Three ways to set the range
| Approach | Example | Strength | Weakness |
|---|---|---|---|
| Fixed dates | `CALENDAR(DATE(2020,1,1), DATE(2026,12,31))` | Predictable. Does not depend on any fact table. | You must edit it by hand when data outgrows the range. |
| Derived from the fact table | `MIN` and `MAX` of `Sales[OrderDate]` | Grows with the data at refresh. | Depends on one column. Misleading if that column holds bad dates. |
| `CALENDARAUTO` | `CALENDARAUTO()` | No table or column named in the formula. | Scans every date column in the model, so outliers stretch the range. |
Using CALENDARAUTO
CALENDARAUTO() examines the date and date/time columns in the model and returns a Date column from January 1 of the earliest year to December 31 of the latest year. It ignores dates in calculated columns and calculated tables, so it does not read the table you are creating.
The weakness is that it reads every date column. A BirthDate column with a value of 1900-01-01 in a customer table gives you a date table that begins in 1900, with about 45,000 rows. Check the first and last dates after you create the table (see "Check the result" below).
CALENDARAUTO accepts an optional argument for the last month of a fiscal year. CALENDARAUTO(6) returns whole fiscal years ending in June. If the earliest data is from March 2020 and the latest from November 2026, the table runs from July 1, 2019 to June 30, 2027. A later chapter uses this for fiscal calendars.
Adding calendar columns with ADDCOLUMNS
A single Date column is enough for relationships, but reports need to group by year, quarter, and month. ADDCOLUMNS(table, "Name", expression, ...) returns the table with extra columns. Inside the expressions, [Date] refers to the current row of the table you passed in.
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" )
)The table now has seven columns. Several choices here are deliberate:
- Numeric and text pairs.
Monthholds "Jan", "Feb", and so on, which sort alphabetically (Apr, Aug, Dec...).Month Numberholds 1 to 12. In the next chapters you will setMonthto sort byMonth Number. Create the numeric column now so it exists. - A text
Quarter. Prefixing "Q" makes labels like "Q3" that read clearly on a chart axis.Quarter Numberis the sort key. Year-Month. The text "2026-03" sorts correctly as text, because the year comes first and the month is zero-padded. It separates March 2025 from March 2026, which the plainMonthcolumn cannot do.
Combine a dynamic range with the columns
To get both a data-driven range and calendar columns, use variables so each part is readable:
Date =
VAR FirstYear = YEAR ( MIN ( Sales[OrderDate] ) )
VAR LastYear = YEAR ( MAX ( Sales[OrderDate] ) )
RETURN
ADDCOLUMNS (
CALENDAR ( DATE ( FirstYear, 1, 1 ), DATE ( LastYear, 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" )
)VAR definitions are evaluated once, before RETURN. This keeps the range logic out of the column list. If you later switch to fixed dates, you replace two lines.
Set the data type and format
A calculated table made this way has a Date column whose type is Date/time. The values have no time part, so display them as dates:
- Select the
Datecolumn in the Data pane. - On Column tools, set Data type to Date. The choices are Date/time, Date, and Time.
- Choose a date format such as
yyyy-MM-dd, which is unambiguous across regions.
You can also hide the helper columns later. This step matters because the date table must have a column of type Date or Date/time before you can mark it, and a clean Date type makes slicers show dates without times.
Check the result
After you create the table, confirm it before building anything on it. Open Table view and select Date:
- Sort the
Datecolumn ascending. The first row should be January 1 of your expected first year. - Sort descending. The last row should be December 31 of your expected last year.
- Check the row count in the status bar or with a quick measure. A full non-leap year has 365 rows and a leap year has 366. Seven years from 2020 to 2026 have two leap years (2020 and 2024), so 7 × 365 + 2 = 2,557 rows.
A temporary measure can compare the row count with the span of days:
Date Row Count = COUNTROWS ( 'Date' )
Date Span Days = DATEDIFF ( MIN ( 'Date'[Date] ), MAX ( 'Date'[Date] ), DAY ) + 1If the two numbers differ, the table has a gap or duplicates. A table built from CALENDAR should never have either. If the numbers differ, you probably edited the table into something else.
Edge cases and gotchas
- Time parts in the fact column. If
Sales[OrderDate]is a date/time such as 2026-03-14 15:30, the relationship toDate[Date](midnight values) will not match. Remove the time in Power Query, or add a calculated column such asINTorDATE(YEAR(...), MONTH(...), DAY(...))to the fact table. A later chapter covers relationships. - Empty or blank fact data.
MINof a column with no values returns blank, andYEARof blank gives 1899. The date table would start in 1899. Refreshing against an empty fact table, such as a staging environment, can produce this. - Bad dates in the fact table. A sentinel value like 1900-01-01 or 9999-12-31 in
OrderDatemakesMINorMAXreturn it. A table from 1900 to 9999 would have millions of rows. Filter or fix these values at the source, or use fixed dates. - Text from
FORMATdepends on locale.FORMAT([Date], "MMM")returns "Jan" in English and a different string in other languages. If your report is shared across regions, the month names follow the settings of the machine that refreshes the model. - Future dates. Time intelligence measures often need dates past the last sale, for example to show a full-year axis or a forecast. Rounding the end to December 31 of the last year covers that for the current year. Extend the end date, such as
LastYear + 1, if you need more. - Calculated tables refresh with the model. The range recomputes whenever the model refreshes. A report published in January that includes new data will extend the table automatically, as long as you derived the range from the data.
Summary of the working pattern
- Pick a range strategy: fixed, derived from the fact table, or
CALENDARAUTO. - Expand to whole years.
- Wrap the result in
ADDCOLUMNSand add numeric and text versions of each period. - Set
Dateto the Date data type with a clear format. - Check the first date, last date, and row count before you rely on the table.
Exercise
A model contains a Sales table with OrderDate from 2021-03-15 to 2025-09-30, and a Customers table with a BirthDate column whose earliest value is 1948-07-02. You create the table with Date = CALENDARAUTO(). What are the first and last dates of the table, and how would you get a table that starts on 2021-01-01 and ends on 2025-12-31 without hard-coding the dates?
Show answer
CALENDARAUTO scans all date columns, so it starts at January 1, 1948, because of BirthDate, and ends on December 31, 2025. To get 2021-01-01 to 2025-12-31, derive the range from Sales only: Date = CALENDAR(DATE(YEAR(MIN(Sales[OrderDate])), 1, 1), DATE(YEAR(MAX(Sales[OrderDate])), 12, 31)). This keeps whole years and ignores BirthDate.
Exercise
Your date table has the columns Year, Month (text such as "Jan"), and Month Number. The seven calendar years 2020 to 2026 give 2,557 rows in a correct table. A colleague's table built with CALENDAR shows 2,556 rows from 2020-01-01. Name one likely cause and the check that confirms it.
Show answer
Most likely the end date is one day early, for example 2026-12-30, or the start is one day late. Sort the Date column descending (or ascending) in Table view and check that the last row is December 31, 2026 (or the first is January 1, 2020). The Date Span Days measure also shows the range length.