Sub topic 14
Common pitfalls and how to check your measures
This chapter assumes you can write the year-to-date, prior-year, year-over-year and moving-average measures from the earlier chapters. It also assumes you know how CALCULATE replaces filters on a date column. Here you will learn the usual reasons these measures return wrong numbers, and how to prove a measure is right before you publish it.
Time intelligence errors rarely produce an error message. The measure runs and returns a plausible number that is wrong. The defense is a short routine: check the date table, check the relationship, check each measure against numbers you can verify by hand.
Pitfalls in the date table
Most wrong results come from the date table, not from the measure.
Gaps and missing years
DATEADD, SAMEPERIODLASTYEAR and DATESINPERIOD assume one row per day with no holes. A table built from only the dates that appear in the fact table (for example by VALUES(Sales[Order Date])) has gaps on days without sales. The functions then shift to a date that does not exist, and the result is blank or wrong.
The date table must also span the full range of the fact data, in whole calendar years. If sales start in March 2020 and the table starts on March 1, 2020, the prior-year value for March 2021 is fine, but a year-to-date for 2020 is missing January and February.
Fact dates that never match
A fact row whose date is not in the date table is attached to the blank row of the dimension. It counts in grand totals but in no month or year, so the months do not add up to the total.
A date column with a time portion causes the same problem. 2024-03-05 14:30 does not equal 2024-03-05 00:00 in the date table, so no row matches. Convert the fact column to a pure date before loading it, or add a separate date-only column and relate on that.
Using the wrong date column
Time intelligence functions must receive 'Date'[Date] from the date table. Passing Sales[Order Date] makes them work on the fact table's column, which usually fails when the table is marked as a date table or breaks the shift. The same applies to the report. If a visual's axis uses Sales[Order Date] instead of a column from 'Date', the date table's filters never reach the measure, and [Sales PY] shows the same value as the current year or nothing.
Two more date-table problems to rule out:
- The table is not marked as the model's date table.
- Power BI's automatic date/time feature has created hidden date tables. Turn it off in the options so fields come only from your own table.
Several date columns on the fact table
Sales often has Order Date and Ship Date. Only one relationship to 'Date' can be active. A measure using the active relationship answers "sales by order date". For the other, use USERELATIONSHIP:
Sales Shipped YTD =
CALCULATE (
[Total Sales],
USERELATIONSHIP ( Sales[Ship Date], 'Date'[Date] ),
DATESYTD ( 'Date'[Date] )
)Pitfalls in the measures
Incomplete current periods
Suppose the data ends on 2025-03-12. The month of March 2025 holds 12 days of sales. [Sales PY] for March holds all 31 days of March 2024. [Sales YoY %] then shows a large fake decline. Year-to-date has a related problem: the value for April through December repeats the March total, so a line chart runs flat to the end of the year. The Sales YTD Capped measure from the earlier chapter fixes the flat line.
To make the comparison fair, shift only the dates that have data:
Sales PY Like for Like =
VAR LastSaleDate =
CALCULATE ( MAX ( Sales[Order Date] ), ALL ( 'Date' ) )
VAR CurrentDates =
FILTER ( VALUES ( 'Date'[Date] ), 'Date'[Date] <= LastSaleDate )
RETURN
CALCULATE ( [Total Sales], DATEADD ( CurrentDates, -1, YEAR ) )For March 2025 this compares 1 to 12 March 2025 with 1 to 12 March 2024.
Moving averages that average the wrong thing
AVERAGEX over a window of dates ignores days where [Total Sales] is blank. If the business does not sell on Sundays, "30 Day Avg" is the average over the selling days, not over 30 days. That may or may not be what you want. For a true per-calendar-day average, divide the window total by the number of days:
Sales 30 Day Avg per Calendar Day =
VAR Window =
DATESINPERIOD ( 'Date'[Date], MAX ( 'Date'[Date] ), -30, DAY )
RETURN
DIVIDE ( CALCULATE ( [Total Sales], Window ), COUNTROWS ( Window ) )Two edge cases remain. At the start of the data, the window reaches before the first sale, so the first 29 days are averages of fewer days. Decide whether to show them or blank them. At a month or year level in a matrix, MAX('Date'[Date]) is the last day of that period, so the value is the 30 days ending there, not an average of the period.
Leap days and month ends
SAMEPERIODLASTYEAR for 29 February 2024 returns 28 February 2023, and a year-over-year comparison of that single day compares unlike days. DATEADD by months from a month end can also land on a shorter month. At month or year level this rarely matters. On a daily grain, expect small differences around these dates.
Evaluating at the wrong level
DATEADD and its relatives work on the dates currently visible. If a slicer or visual filter leaves a non-contiguous set of dates (for example, only Mondays), the shift is computed from that set and DATEADD can return an error or odd numbers. Use filters that select whole periods.
A diagnosis flow
Use this order when a number looks wrong. Each step removes one class of cause.
Checking the date table with a query
Run this in DAX query view. It compares the number of rows with the number of days between the first and last date, and shows the fact table's range against the date table's range.
EVALUATE
ROW (
"Rows in Date table", COUNTROWS ( 'Date' ),
"Days expected", DATEDIFF ( MIN ( 'Date'[Date] ), MAX ( 'Date'[Date] ), DAY ) + 1,
"First date", MIN ( 'Date'[Date] ),
"Last date", MAX ( 'Date'[Date] ),
"First sale", MIN ( Sales[Order Date] ),
"Last sale", MAX ( Sales[Order Date] )
)The first two values must be equal. The date table's first date must be on or before the first sale, and the last date on or after the last sale. To count fact rows that have no matching date, add a measure:
Sales Rows Without Date =
COUNTROWS ( FILTER ( Sales, ISBLANK ( RELATED ( 'Date'[Date] ) ) ) )Anything above zero means some sales are outside the date table or have a time portion.
Checking each measure
Put a matrix on a page with 'Date'[Year] and 'Date'[Month Number] on rows and the measures you are testing as values. Then apply these checks.
| Measure | Check | Expected result |
|---|---|---|
| `[Sales YTD]` | December row of a complete year | Equals `[Total Sales]` for the whole year |
| `[Sales YTD]` | January row | Equals January's `[Total Sales]` |
| `[Sales PY]` | Any month in the second year | Equals `[Total Sales]` of the same month one year earlier |
| `[Sales PY]` | Months in the first year | Blank |
| `[Sales YoY %]` | First year | Blank, not zero and not an error |
| `[Sales 3 Month Avg]` | A middle month | Total of that month and the two before, divided by 3 |
| Sum of months | Total row vs. sum of rows | Equal for `[Total Sales]`; not equal for YTD or averages, which is normal |
Also compare against a source you trust. Pick one month, filter the raw data in Power Query, Excel or SQL, and calculate the total by hand. If your measure and the hand calculation disagree for one month, the cause is almost always one of the date table problems above.
Symptoms and likely causes
| Symptom | Likely cause | Fix |
|---|---|---|
| `[Sales PY]` is blank for every month | Visual uses `Sales[Order Date]`, or the relationship is missing | Use `'Date'` columns and check the relationship |
| `[Sales PY]` equals the current value | Same as above, or the measure uses a fact column | Pass `'Date'[Date]` to the function |
| Months do not add up to the year | Fact dates outside the date table, or times in the date column | Extend the table; remove the time portion |
| YTD line runs flat to year end | No cap for future dates | Use `Sales YTD Capped` |
| YoY % shows a big drop in the current month | Partial month vs. full prior month | Use a like-for-like prior year |
| `DATEADD` returns an error | Non-contiguous dates in the filter or gaps in the table | Select whole periods; rebuild the date table |
| Moving average is higher than expected | `AVERAGEX` skips blank days | Divide by `COUNTROWS(Window)` if you want calendar days |
Exercise
Sales data runs from 2022-02-15 to 2025-03-12. A colleague built the date table with CALENDAR(MIN(Sales[Order Date]), MAX(Sales[Order Date])) and reports that the 2022 year-to-date total is too low and that March 2025 shows -62% year-over-year growth. Give one cause for each problem and one fix for each.
Show answer
The 2022 YTD is too low because the date table starts on 2022-02-15, so January and early February 2022 do not exist and the year is not complete. Build the table from whole calendar years, for example CALENDAR(DATE(2022,1,1), DATE(2025,12,31)). The -62% is caused by comparing 12 days of March 2025 with all 31 days of March 2024. Compare like for like by shifting only the dates up to the last sale date, as in Sales PY Like for Like, or blank the current month until it is complete.
Exercise
A matrix shows Sales Rows Without Date = 480 and the months of 2023 add up to less than the 2023 grand total. List two possible causes and how you would tell them apart.
Show answer
One cause is that the fact table has dates the date table does not cover, such as 2026 orders when the table ends in 2025. The other is that Order Date has a time portion, so values like 2023-06-01 14:30 do not match a date-only table. Compare MIN and MAX of Sales[Order Date] with the date table's range to detect the first cause. Check whether any Order Date has a non-zero time portion, for example by comparing it with INT or the date-only conversion, to detect the second.