Code Generator | DAX

Enhancing DateMaster with DAX Date Columns

Learn to extend the DateMaster table in DAX by adding year, month name, month number, day name, and day number columns, enriching your date analytics and enhancing data slicing capabilities.


Empty image or helper icon

Prompt

i have a dax as follows DateMaster = CALENDAR(DATE(2005, 1, 1), DATE(2030, 12, 31)), i want to create a dax to create a column to get year in format YYYY and then another column for Month Name and another for month number and then another one for day name and another for day number in the month

Answer

Creating Date Columns in DAX

To develop a professional-grade DAX function that extends the DateMaster table by adding columns for year, month name, month number, day name, and day number, follow the steps below. Each additional column serves a specific purpose and ensures the DateMaster table is enriched for various analytical needs.

Initial Setup: DateMaster Table

First, ensure the DateMaster table is created:

DateMaster = CALENDAR(DATE(2005, 1, 1), DATE(2030, 12, 31))

Adding Extended Date Columns

Below are the DAX formulas to add columns for year, month name, month number, day name, and day number.

1. Year Column (YYYY Format)

DateMaster[Year] = YEAR(DateMaster[Date])
  • Explanation: Extracts the year from the date and represents it in the YYYY format.

2. Month Name Column

DateMaster[Month Name] = FORMAT(DateMaster[Date], "MMMM")
  • Explanation: Extracts the full month name from the date.

3. Month Number Column

DateMaster[Month Number] = MONTH(DateMaster[Date])
  • Explanation: Extracts the numeric month value (1-12) from the date.

4. Day Name Column

DateMaster[Day Name] = FORMAT(DateMaster[Date], "dddd")
  • Explanation: Extracts the full day name from the date.

5. Day Number in Month Column

DateMaster[Day Number] = DAY(DateMaster[Date])
  • Explanation: Extracts the day number in the month from the date.

Step-by-Step Code Blocks

Combine the above DAX formulas step-by-step as follows:

// Create the DateMaster table.
DateMaster = CALENDAR(DATE(2005, 1, 1), DATE(2030, 12, 31))

// Add Year column in YYYY format.
DateMaster[Year] = YEAR(DateMaster[Date])

// Add Month Name column.
DateMaster[Month Name] = FORMAT(DateMaster[Date], "MMMM")

// Add Month Number column.
DateMaster[Month Number] = MONTH(DateMaster[Date])

// Add Day Name column.
DateMaster[Day Name] = FORMAT(DateMaster[Date], "dddd")

// Add Day Number column.
DateMaster[Day Number] = DAY(DateMaster[Date])

Usage Example

Imagine you are analyzing sales data and need to slice the data by year, month name, month number, day name, and day number. By using the enriched DateMaster table, you can easily perform such analyses.

Sales = 
SUMMARIZE (
    SalesData,
    DateMaster[Year],
    DateMaster[Month Name],
    DateMaster[Month Number],
    DateMaster[Day Name],
    DateMaster[Day Number],
    "Total Sales", SUM(SalesData[Sales Amount])
)

Conclusion

By following these steps, you have augmented the DateMaster table with additional columns that provide valuable date-related information. This approach ensures your analyses can benefit from enhanced time-based slicing and dicing, leading to more granular insights.

For more in-depth training on DAX and other advanced data analysis techniques, consider exploring courses on the Enterprise DNA Platform.

Create your Thread using our flexible tools, share it with friends and colleagues.

Your current query will become the main foundation for the thread, which you can expand with other tools presented on our platform. We will help you choose tools so that your thread is structured and logically built.

Description

Learn to extend the DateMaster table in DAX by adding year, month name, month number, day name, and day number columns, enriching your date analytics and enhancing data slicing capabilities.