D9 · DAX · Filter context · about 25 min
The Total That Doesn't Add Up
hardThe front office counts "big games": games where the team scored 5 runs or more. In a table of games every row is right. The team total says 120, which is every game the team played.
Your task
Create a measure named [Big Games]: the number of games in the current filter in which the team scored 5 or more runs. It must be right per game, per month, per team and at the grand total, and stay blank where there are none.
What the grader checks
You see the situations, not the expected numbers. The expected values are computed live from your file and the source data.
- T1One row per game
Harbor Hawks, April 2025, one row per game. - T2By team
One row per team, all seasons. - T3By month for one team
Riverton Rams in 2024, one row per month. - T4Grand total
The total row of the whole league.
Stuck?
Open one hint at a time.
Hint 1
Your measure is right on every game row and wrong on the total row. What is SUM( Batting[Runs] ) on the total row of a whole team?
Hint 2
The test "5 or more runs" belongs to each game, but a measure is evaluated once per cell. On a total cell you have to look at the games one by one and count the ones that pass.
Hint 3 (near-answer)
COUNTROWS( FILTER( VALUES( Batting[GameID] ), CALCULATE( SUM( Batting[Runs] ) ) >= 5 ) ). FILTER walks the games in the cell, CALCULATE gets each game's runs.
Show the editorial
Solve it first if you can: the editorial gives the answer away.
The trap
IF( SUM( Batting[Runs] ) >= 5, ... ) checks the condition once for the whole cell. On a game row the cell is one game, so the answer is right and the table looks perfect. On the team total the cell is 120 games with hundreds of runs, the condition is true, and the measure counts every game. Totals are not the sum of the rows above them; they are the same formula run on a bigger filter.
The fix
COUNTROWS( FILTER( VALUES( Batting[GameID] ), CALCULATE( SUM( Batting[Runs] ) ) >= 5 ) ).
Why it works
The condition has a grain: it is about one game. To use it on any bigger cell you must go down to that grain and come back up: VALUES lists the games in the cell, CALCULATE evaluates the runs of each game, FILTER keeps the big ones, COUNTROWS counts them. The same formula then works on a game row, a month, a team and the grand total.
If a condition is about one row of something (a game, an order), iterate those rows. Never test it on the aggregate.