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
Ek common chuk mhanje 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, udaharan mhanje MAX(Orders[Order Date]), when that is what the business means. Chuk karu naka!