D4 · DAX · Filter context · about 20 min
Share of Team Runs
mediumA coach's report shows each player's runs as a share of the team total. It works until the coach picks three players in the slicer: the shares of those three players now add up to a few percent, when they should add up to 100% of the group on screen.
Your task
Create a measure named [Run Share]: a player's runs divided by the runs of all players currently selected (the denominator respects the Season, Team and Player slicers). Shares of the visible players must add up to 100%.
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.
- T1Players on one team
One team selected, one row per player. - T2Season slicer: 2024
Season slicer set to 2024, one row per player. - T3Player slicer: 3 players picked
Three players picked in a slicer. Their shares must add up to 100%. - T4Grand total
No grouping, no slicers: the total of the visual must be 100%.
Stuck?
Open one hint at a time.
Hint 1
A share needs a denominator that is the total of the group on screen. In a table by player, what filter context does each row have, and what do you need to undo to get the group total?
Hint 2
CALCULATE can change the filter context for the denominator. You want to remove the filter that makes each row its own player, but keep the filters the user chose in slicers. There are several functions that remove filters; they differ in what they keep.
Hint 3 (near-answer)
Use ALLSELECTED( Players ) as the filter inside CALCULATE( SUM( Batting[Runs] ), ... ). ALL( Players ) removes the slicer too.
Show the editorial
Solve it first if you can: the editorial gives the answer away.
The trap
ALL( Players ) in the denominator removes every filter on Players, including the player slicer the user chose. With three players picked, each share is divided by the runs of every player in the context, so the three shares add up to a few percent instead of 100%. It looks right on a page where nobody uses the Player slicer, which is why it ships.
The fix
DIVIDE( SUM( Batting[Runs] ), CALCULATE( SUM( Batting[Runs] ), ALLSELECTED( Players ) ) ).
Why it works
ALL clears filters, whatever their source. ALLSELECTED clears only the filters that come from the row or column of the visual itself and restores the filters from outside, such as slicers. The denominator becomes the total of the players the user selected, so the visible shares add up to 100%.
Ask what the denominator should respect. All data: ALL. What the user has selected: ALLSELECTED.