Hello! Welcome back to our journey into mastering DAX.
In the last lesson, you created the essential foundation for all time intelligence analysis: a dynamic, single-column date table using CALENDARAUTO. You also learned the critical step of marking it as the official date table for your data model.
While that was a crucial first step, a single column of dates isn't very useful for creating reports. We can't easily filter by year or compare performance by quarter. In this lesson, we will make our Date table truly powerful. We will add several useful columns to it—specifically Year, Quarter, Month Name, and Day of Week—using DAX functions. This will allow you to slice, dice, and analyze your sales data across different time granularities.
1. Enriching the Date Table with Calculated Columns
We will be adding these new columns as calculated columns to the Date table you created. As a quick refresher from an earlier lesson, a calculated column is created using a DAX formula and its values are calculated for each row, then stored in the data model. This is perfect for static attributes of a date, like the year or month it belongs to.
We'll add each column one by one. This approach makes it very clear what each DAX function does.
For a great overview of the functions we'll be using, let's turn to a video from WiseOwlTutorials. We will watch it in segments as we build our table.
DAX for Power BI Part 2.6 - Dates in Calculated Columns
This video provides clear, step-by-step instructions for creating date-related calculated columns. We'll start with the 'Year' and 'Quarter'.
Watch from 08:18 to 09:07. The presenter demonstrates how to use the YEAR and QUARTER functions to create new calculated columns.
Your Turn: Add Year and Quarter Columns
Now, let's apply this to your Power BI file.
- In Power BI, navigate to the Data view and select your
Datetable. - From the Table tools ribbon, click New column.
- In the formula bar, enter the following DAX expression and press Enter:
Year = YEAR('Date'[Date]) - Create another new column for the quarter. For better readability in our reports, we'll format it with a "Q" prefix. Use this formula:
TheQuarter = "Q" & QUARTER('Date'[Date])&operator in DAX concatenates (joins) the text "Q" with the number returned by theQUARTERfunction.
2. Handling Months and Weekdays: The Sorting Challenge
When adding columns for month names (e.g., "January") or weekday names (e.g., "Monday"), we face a common challenge: sorting. By default, Power BI will sort text columns alphabetically, which would result in "April, August, December..." instead of the correct chronological order.
To solve this, the best practice is to create two columns:
- One for the name (for display in visuals).
- One for the number (for sorting purposes).
Let's see how this is done.
DAX for Power BI Part 2.6 - Dates in Calculated Columns
Continuing with the WiseOwlTutorials video, let's see how to create columns for the month number and month name.
Please watch from 09:07 to 10:10. Notice how the presenter first creates Month Number using the MONTH function and then Month Name using the FORMAT function.
The FORMAT function is incredibly versatile. By providing the format string "MMMM", we get the full month name. Using "MMM" would give us the abbreviated name (e.g., "Jan").
Your Turn: Add Month and Day of Week Columns
Let's add the necessary columns for both months and weekdays.
- Month Number: Create a new column with the formula:
Month Number = MONTH('Date'[Date]) - Month Name: Create another new column using
FORMAT:Month Name = FORMAT('Date'[Date], "MMMM") - Day of Week Number: We'll use the
WEEKDAYfunction. This function has an optional second argument that defines the start of the week. We'll use2to specify that Monday is 1 and Sunday is 7, a common business convention. Create the new column:Day of Week Number = WEEKDAY('Date'[Date], 2) - Day of Week Name: Finally, use the
FORMATfunction again. The string"dddd"gives the full day name. Create the new column:Day of Week = FORMAT('Date'[Date], "dddd")
You should now have a Date table with several new, useful columns.
3. The Crucial Step: Sorting by Column
You've created the columns, but your Month Name and Day of Week columns will still sort alphabetically in visuals. Now we'll fix that using the Sort by Column feature.
This feature tells Power BI to use the order of a different column (our numeric column) to sort the text column.
Creating a simple date table in Power BI
The experts at SQLBI demonstrate exactly how to fix the sorting order for months. The same logic applies to our Day of Week column.
Watch this short clip from 03:30 to 04:02. The presenter selects the 'Month' column and then uses the 'Sort by Column' feature to sort it by 'Month Number'.
Your Turn: Apply the Correct Sorting
- In the Data view, select the
Month Namecolumn in yourDatetable. - The Column tools ribbon will appear. Click Sort by column and choose
Month Numberfrom the dropdown. - Now, select the
Day of Weekcolumn. - Click Sort by column again and choose
Day of Week Number.
Your date table is now properly configured for both analysis and correct sorting in visuals. You can also hide the "number" columns from the Report view by right-clicking them in the Fields pane and selecting "Hide" so that report users don't get confused.
A Quick Reference and an Alternative Method
You've now learned the fundamental way to add attributes to a date table. For future reference, there's a huge variety of columns you can add.
How to create a DAX date table
This blog post from BI Jo serves as an excellent reference guide or 'cheat sheet' for a wide array of date-related DAX formulas.
You don't need to read this in detail right now, but bookmark it. Scroll through the section 'DAX CODE FOR NEW DATA INFORMATION COLUMNS' to see the variety of formulas available for columns like 'Month Short', 'Financial Year', 'Week Number', and more.
Additionally, there is a more compact way to create the table and all its columns in a single DAX expression using the ADDCOLUMNS function. This function takes a table as its first argument and then a series of name/expression pairs to define the new columns.
Here is how our entire Date table could have been written in one go:
Date =
ADDCOLUMNS (
CALENDARAUTO(),
"Year", YEAR ( [Date] ),
"Month Number", MONTH ( [Date] ),
"Month Name", FORMAT ( [Date], "MMMM" ),
"Quarter", "Q" & QUARTER ( [Date] )
)
For now, the one-by-one column creation you did is perfectly fine and often easier to debug. But it's good to be aware of this more advanced technique.
Conclusion
Fantastic work! You have transformed a simple list of dates into a versatile and powerful dimension table that is ready for analysis.
Key Takeaways:
- You can enrich your date table by adding calculated columns for attributes like year, quarter, month, and day.
- Simple DAX functions like
YEAR(),MONTH(), andQUARTER()extract numeric parts of a date. - The
FORMAT()function is a powerful tool for creating custom text representations, such as full month names ("MMMM") or day names ("dddd"). - To ensure chronological sorting for text columns like
Month Name, you must create a corresponding numeric column (e.g.,Month Number) and use the Sort by Column feature.
Preview of the Next Lesson:
Our Date table is now complete, but it's still an island in our data model. It isn't connected to our Sales data. In the next lesson, we will complete our basic star schema by establishing the relationship between the Date table and the Sales fact table. This final step will unlock the ability to perform true time intelligence calculations.