Forum Discussion
Need Help with DAX Formula to combine rows
- 9 months ago
Apart from DataNinja777 solution, Alternatively, you can use below tiny DAX code
Value Measure = VAR _Rank = RANKX( ALL( 'Table'[Segment]),CALCULATE( SUM( 'Table'[ValueMeasure])),,DESC) VAR _Result = IF( _Rank =1, CALCULATE( SUM( 'Table'[ValueMeasure]), ALL( ) ) ,BLANK() ) RETURN _ResultBelow screenshots
when you select Segment A
Hopt it helps
Regards
sanalytics
- 9 months ago
Hello rizkus
Thanks for your question!
I have recreated your scenario and here’s the full explanation.DataTable:
ID Segment Value Measure123 A 100 123 B 200 123 B 300 RankTable:
Segment RankB 1 A 2 TopSegmentTotal := IF ( ISINSCOPE ( 'DataTable'[ID] ), VAR CurrentID = SELECTEDVALUE ( 'DataTable'[ID] ) VAR TopRankForID = CALCULATE ( MIN ( 'RankTable'[Rank] ), FILTER ( ALL ( 'DataTable' ), 'DataTable'[ID] = CurrentID ) ) VAR CurrentSegmentRank = MIN ( 'RankTable'[Rank] ) VAR TotalForID = CALCULATE ( SUM ( 'DataTable'[Value Measure] ), FILTER ( ALL ( 'DataTable' ), 'DataTable'[ID] = CurrentID ) ) RETURN IF ( ISFILTERED ( 'DataTable'[Segment] ) && SELECTEDVALUE ( 'DataTable'[Segment] ) = "A", TotalForID, IF ( CurrentSegmentRank = TopRankForID, TotalForID ) ) )
Why the DifferenceIn your screenshot, Segment B had only one row (300), so the total was 300.
In my dataset, Segment B has two rows (200 + 300), so the total is 600.
The measure is correct, it always sums all values for the same ID and shows the total only on the top‑ranked segment.
Filtering Behavior
If you filter Segment B, the measure shows the total (600).
If you filter Segment A, the measure shows blank (unless you allow it to return the total for A).
If you want to show the raw row (300) instead of the total, use Value Measure directly and filter to that row.
Conclusion
Use Value Measure if you want to show raw rows (200 and 300 separately, or just 300).
Use TopSegmentTotal if you want to show the combined total (600) for the top segment.
Both are correct , it depends on whether you want to display raw values or the calculated total.
Slicers make it easy to toggle between segments and see how the measure behaves under filter
Slicer showing TopSegment TotalValue Measure for Segment B only