Sub topic 6
Adding year, quarter, month, and week columns
This chapter assumes you can write basic DAX, you know the difference between fact and dimension tables, and you have already created a daily Date table with CALENDAR. Here you add the calendar attributes that report visuals group and filter by.
Why the table needs these columns
A date table with only a Date column can answer "what happened on this day?" Reports usually ask about months, quarters, and weeks. If you put those attributes in the date table, every fact table related to it can use them. You do not repeat the logic in each fact table.
Each attribute usually needs two columns:
- A display column, such as
Monthwith the value "Mar". - A numeric column, such as
Month Numberwith the value 3.
Text sorts alphabetically, so "Apr" comes before "Jan". The numeric column gives the next chapter something to sort by.
Year, quarter, and month in one expression
ADDCOLUMNS wraps the CALENDAR call and adds columns in a single 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" )
)Inside ADDCOLUMNS, [Date] refers to the date in the current row. The Year-Month column is useful because its text sorts correctly as written ("2024-02" comes before "2024-10"). It also keeps January 2024 and January 2025 apart, which the Month column alone does not.
Adding week columns
Weeks are less tidy than months, because a week can span two months or two years. You must choose a definition. WEEKNUM takes a second argument for it:
| Return type | Week starts on | Notes |
|---|---|---|
| 1 (default) | Sunday | Week 1 contains January 1 |
| 2 | Monday | Week 1 contains January 1 |
| 21 | Monday | ISO 8601: week 1 contains the first Thursday |
Here is a version that adds weekday and ISO week columns.
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" ),
"Day of Week Number", WEEKDAY ( [Date], 2 ),
"Day Name", FORMAT ( [Date], "ddd" ),
"ISO Week", WEEKNUM ( [Date], 21 ),
"ISO Year", YEAR ( [Date] - WEEKDAY ( [Date], 2 ) + 4 ),
"Week Start", [Date] - WEEKDAY ( [Date], 2 ) + 1
)WEEKDAY ( [Date], 2 ) returns 1 for Monday through 7 for Sunday. Subtracting it and adding 1 gives the Monday of that week, which makes a good label for a weekly axis. Adding 4 instead lands on the Thursday of the week, and the year of that Thursday is the ISO year.
Common errors
- Referencing a new column in the same
ADDCOLUMNS. The added columns cannot see each other. Repeat the expression, asISO Yeardoes, or add the dependent column as a separate calculated column afterward. FORMATdepends on locale.FORMAT ( [Date], "MMM" )returns names in the language of the model. Use the numeric columns for logic, and use the text columns only for display.- Wrong data type in the numeric columns. Check that the numeric columns are whole numbers. You will rely on them in the next chapter.
- Text used for filtering. If a measure compares against
"Q1", a typo silently returns blank. Compare againstQuarter Numberinstead.
Checking the result
Open Table view and select the Date table. Confirm that the first row is January 1 of the first year and that Month Number runs from 1 to 12. Put Month in a table visual and notice that the months are alphabetical. That is expected at this stage, and the next chapter fixes it.
Exercise
A report shows weekly sales with Year and ISO Week on the axis. The last days of December 2024 appear under week 1 of 2024 instead of week 1 of 2025. What is wrong, and which column fixes it?
Show answer
Year is the calendar year of the date, while ISO Week follows ISO 8601, where week 1 can begin in the previous December. Pairing them mixes two definitions. Use ISO Year with ISO Week, or plot Week Start instead.