Ravindra BagaleCourses & study guides मराठी Track your progress

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!

Loading DAX practice tasks...

  1. Year of the batch (Easy): Calculated column on Batches: Batch Year = year of BatchDate.
  2. Month number (Easy): Calculated column on Batches: Month No = month number (1 to 12) of BatchDate.
  3. Month name (Easy): Calculated column on Batches: Month Name = BatchDate shown as a 3-letter month (Mar, Apr...).
  4. Day of week (Medium): Calculated column on Batches: Week Day = weekday number of BatchDate with Monday = 1.
  5. Quarter label (Medium): Calculated column on Batches: Qtr = "Q" joined with the quarter number of BatchDate.
  6. Days left in the month (Medium): Calculated column on Batches: Days Left = days from BatchDate to the last day of its month.
  7. First batch date (Medium): Measure: First Batch = the earliest BatchDate.
  8. Production span in days (Hard): Measure: Span Days = days between the earliest and latest BatchDate.
  9. May units (Hard): Measure: May Units = UnitsMade added for batches made in month 5.
  10. Weekend batches (Hard): Measure: Weekend Batches = number of batches on Saturday or Sunday.

Ravi Sir's Cards for this lesson

मराठी cards