Important Date and Time Functions in DAX (Power BI)

Important Date and Time Functions in DAX (Power BI)

DAX provides a variety of date and time functions to analyze and manipulate date-related data. Below are some of the most commonly used ones, explained with examples using a Sample Superstore dataset.


1. TODAY()

  • Purpose: Returns the current date.
  • Example Use Case: Calculate the number of days since an order was placed.

Dataset:

Order IDOrder Date
12024-01-01
22024-03-15
32024-07-10

DAX Formula:

Days Since Order = DATEDIFF('Orders'[Order Date], TODAY(), DAY)

Output (assuming today is 2024-11-17):

Order IDOrder DateDays Since Order
12024-01-01321
22024-03-15247
32024-07-10130

2. YEAR()

  • Purpose: Extracts the year from a date.
  • Example Use Case: Create a column for the year of an order.

DAX Formula:

Order Year = YEAR('Orders'[Order Date])

Output:

Order IDOrder DateOrder Year
12024-01-012024
22024-03-152024
32024-07-102024

3. MONTH()

  • Purpose: Extracts the month from a date.
  • Example Use Case: Group orders by month.

DAX Formula:

Order Month = MONTH('Orders'[Order Date])

Output:

Order IDOrder DateOrder Month
12024-01-011
22024-03-153
32024-07-107

4. FORMAT()

  • Purpose: Converts a date to a specific format.
  • Example Use Case: Display dates in “Month-Year” format.

DAX Formula:

Formatted Date = FORMAT('Orders'[Order Date], "MMMM YYYY")

Output:

Order IDOrder DateFormatted Date
12024-01-01January 2024
22024-03-15March 2024
32024-07-10July 2024

5. WEEKDAY()

  • Purpose: Returns the day of the week as a number (1 = Sunday, 7 = Saturday).
  • Example Use Case: Determine if an order was placed on a weekend.

DAX Formula:

Order Day Number = WEEKDAY('Orders'[Order Date])

Output:

Order IDOrder DateOrder Day Number
12024-01-012 (Monday)
22024-03-156 (Friday)
32024-07-104 (Wednesday)

6. EOMONTH()

  • Purpose: Returns the last day of the month for a given date.
  • Example Use Case: Calculate the number of days left in the month.

DAX Formula:

End of Month = EOMONTH('Orders'[Order Date], 0)
Days Left = DATEDIFF('Orders'[Order Date], [End of Month], DAY)

Output:

Order IDOrder DateEnd of MonthDays Left
12024-01-012024-01-3130
22024-03-152024-03-3116
32024-07-102024-07-3121

7. DATEDIFF()

  • Purpose: Calculates the difference between two dates in days, months, or years.
  • Example Use Case: Calculate the age of a product since its launch.

Dataset:

Product IDLaunch Date
1012020-05-10
1022022-08-15
1032023-11-01

DAX Formula:

Years Since Launch = DATEDIFF('Products'[Launch Date], TODAY(), YEAR)

Output:

Product IDLaunch DateYears Since Launch
1012020-05-104
1022022-08-152
1032023-11-010

8. NOW()

  • Purpose: Returns the current date and time.
  • Example Use Case: Calculate how many hours have passed since an order was placed.

Dataset:

Order IDOrder DateOrder Time
12024-11-1708:00 AM
22024-11-1702:00 PM

DAX Formula:

Hours Since Order = DATEDIFF(
    'Orders'[Order Date] + TIMEVALUE('Orders'[Order Time]),
    NOW(),
    HOUR
)

Summary of Key Functions

FunctionDescription
TODAY()Returns the current date.
YEAR(), MONTH()Extracts the year or month from a date.
FORMAT()Formats a date into a custom text representation.
WEEKDAY()Returns the day of the week as a number (1-7).
EOMONTH()Returns the last day of the month for a given date.
DATEDIFF()Calculates the difference between two dates in days, months, or years.
NOW()Returns the current date and time.

These examples illustrate how DAX Date and Time functions can be applied to analyze and manipulate dates in Power BI effectively.

Learn Power BI from our experts with 10 Years of Data Analytics expertise for Multinational Companies : Click Here