Ravindra BagaleCourses & study guides

13. DAX: Data Analysis Expressions

13.15 Date Functions

Today = TODAY()                                  -- current date
Now Time = NOW()                                 -- current date and time

Order Year = YEAR(Orders[Order Date])
Order Month = MONTH(Orders[Order Date])
Order Hour (DAX) = HOUR(Orders[Order DateTime])  -- 0 to 23

-- Calculated column: delivery time in minutes from the two timestamps
Delivery Mins (calc) =
IF(
    NOT ISBLANK(Orders[Delivered DateTime]),
    DATEDIFF(Orders[Order DateTime], Orders[Delivered DateTime], MINUTE)
)

-- Customer tenure in months (calculated column in Customer)
Tenure Months = DATEDIFF(Customer[Signup Date], TODAY(), MONTH)

Month End = EOMONTH(Orders[Order Date], 0)       -- last day of the same month
Start of Year = DATE(YEAR(Orders[Order Date]), 1, 1)

Week Number = WEEKNUM('Date'[Date], 2)            -- weeks starting Monday
Weekday No = WEEKDAY('Date'[Date], 2)             -- Monday = 1 … Sunday = 7

Other useful date functions: EDATE (add months), STARTOFMONTH, ENDOFMONTH, STARTOFYEAR, ENDOFYEAR, CALENDAR, CALENDARAUTO.

Ravindra Bagale's Tip

A common mistake is using TODAY() in measures and then being confused when the report shows different numbers each day, or when data refreshes later than expected. Base "current period" logic on the latest date with data, for example MAX(Orders[Order Date]), when that is what the business means. Don't make this mistake!