DATEDIFF in DAX: the distance between two dates

Ship time, invoice age, task delay: with DATEDIFF you calculate the distance between two dates in DAX in days, weeks, months, quarters or years. What matters is what the function actually counts.

In short: DATEDIFF(start, end, DAY) returns the number of days from start to end. With MONTH, QUARTER or YEAR, DATEDIFF counts the month, quarter or year boundaries in between, not full months or years. If the end is before the start, the result is negative; if a date is blank, it stays blank.

As a column: the ship time of each order

Ship Days = DATEDIFF(Sales[OrderDate], Sales[ShipDate], DAY)

As a calculated column in the Sales table, every order then shows the number of days until it shipped. A column fits because the value belongs to each row and does not change with the filter; more in the lesson on calculated columns.

In a measure: average ship time

Avg Ship Days = AVERAGEX(Sales, DATEDIFF(Sales[OrderDate], Sales[ShipDate], DAY))

A measure has no current row. AVERAGEX therefore goes through the orders, calculates the distance per row and takes the average. In the sample model it is 11.6 days for the North region and 8.8 for West. The lesson on SUMX and iterators shows how iterators work.

What DATEDIFF actually counts

FormulaResultWhy
DATEDIFF(DATE(2025, 12, 31), DATE(2026, 1, 1), YEAR)1one year boundary, although only one day lies in between
DATEDIFF(DATE(2026, 1, 31), DATE(2026, 2, 1), MONTH)1one month boundary
DATEDIFF(DATE(2026, 3, 31), DATE(2026, 4, 1), QUARTER)1one quarter boundary
DATEDIFF(DATE(2026, 1, 3), DATE(2026, 1, 4), WEEK)1a new week starts from Saturday to Sunday
DATEDIFF(DATE(2026, 3, 10), DATE(2026, 3, 3), DAY)-7end before start
DATEDIFF(DATE(2026, 1, 1), DATE(2026, 1, 2), HOUR)24hours, minutes and seconds work too

We compared all values with Power BI Desktop. For age or years of service in full years, DATEDIFF with YEAR is therefore only enough if the end is a year end; otherwise a person can come out a year "older" than they are. There is an example in the case salary structure of the HR course.

Common mistakes

Start and end swapped

DATEDIFF counts from the first to the second date. Swapped, a delay comes out as a negative number.

Blank dates

If the ship date is missing, DATEDIFF stays blank. AVERAGEX skips blank values, so the average only covers shipped orders. That is usually right, but the report should say so.

TODAY in a column

DATEDIFF(Invoice[Date], TODAY(), DAY) as a column is only recalculated on refresh. For a daily age you need a measure or a regular refresh.

Keep learning

Write to us

Choose language

Select your preferred language