Forum Discussion
Ranking issue
- Anonymous3 years ago
Hi smpa01 ,
I updated your sample pbix file(see the attachment), please check if that is what you want. You can create a measure as below to get the rank...
Measure = VAR tab = FILTER ( ALLSELECTED ( 'wc' ), 'wc'[Win] <> 0 && 'wc'[Region] = SELECTEDVALUE ( 'wc'[Region] ) ) VAR result = SUBSTITUTEWITHINDEX ( 'wc', "rank", SUMMARIZE ( tab, 'wc'[Win] ), 'wc'[Win], DESC ) VAR _rank = MAXX ( result, [rank] ) RETURN IF ( ISBLANK ( _rank ), BLANK (), _rank + 1 )In addition, you can achieve the same requirement using RANKX function. Please create a measure as below to get it, it is easier...
Rank = RANKX ( FILTER ( ALLSELECTED ( 'wc' ), 'wc'[Region] = SELECTEDVALUE ( 'wc'[Region] ) ), CALCULATE ( SUM ( 'wc'[Win] ) ), , DESC, DENSE )Best Regards
- 3 years ago
This feels a bit for efficient since it's iterating over regions instead of every row:
Table 3 = SELECTCOLUMNS ( GENERATE ( SUMMARIZE ( wc, wc[Region] ), VAR _Region = wc[Region] VAR _Partition_ = SELECTCOLUMNS ( FILTER ( wc, wc[Region] = _Region ), wc[Country], wc[Win] ) VAR _AddCol_ = ADDCOLUMNS ( _Partition_, "@win", wc[Win] ) VAR _SubIndex_ = SUBSTITUTEWITHINDEX ( _AddCol_, "Rank", SUMMARIZE ( _AddCol_, [@win] ), [@win], DESC ) RETURN _SubIndex_ ), "Region", wc[Region], "Country", wc[Country], "Win", wc[Win], "Rank", [Rank] + 1 )
Hi smpa01 ,
I updated your sample pbix file(see the attachment), please check if that is what you want. You can create a measure as below to get the rank...
Measure =
VAR tab =
FILTER (
ALLSELECTED ( 'wc' ),
'wc'[Win] <> 0
&& 'wc'[Region] = SELECTEDVALUE ( 'wc'[Region] )
)
VAR result =
SUBSTITUTEWITHINDEX (
'wc',
"rank", SUMMARIZE ( tab, 'wc'[Win] ),
'wc'[Win], DESC
)
VAR _rank =
MAXX ( result, [rank] )
RETURN
IF ( ISBLANK ( _rank ), BLANK (), _rank + 1 )
In addition, you can achieve the same requirement using RANKX function. Please create a measure as below to get it, it is easier...
Rank =
RANKX (
FILTER ( ALLSELECTED ( 'wc' ), 'wc'[Region] = SELECTEDVALUE ( 'wc'[Region] ) ),
CALCULATE ( SUM ( 'wc'[Win] ) ),
,
DESC,
DENSE
)
Best Regards
Anonymous follow up question.
In a pure table expression, how can I return a rank by partition.
For example, I can achieve this with RANKX as following
Table 4 =
ADDCOLUMNS (
FILTER ( wc, wc[Win] <> 0 ),
"rank", RANKX ( FILTER ( wc, wc[Region] = EARLIER ( wc[Region] ) ), wc[Win],, DESC )
)
How can I do the same with SUBSTITUTEWITHINDEX in a pure table expression
I tried this
Table 3 =
VAR partitionFirst =
CALCULATE ( MAX ( wc[Region] ) )
VAR base =
FILTER ( ALL ( wc ), wc[Win] <> 0 && wc[Region] = partitionFirst )
VAR rankedTbl =
SUBSTITUTEWITHINDEX (
ADDCOLUMNS ( wc, "@win", wc[Win] ),
"rank", SUMMARIZE ( base, [Win] ),
wc[Win], DESC
)
RETURN
rankedTbl
but it did not work
I know I can do this. But I am not looking for this.
- AlexisOlson3 years agoSuper User
smpa01 Here's how I'd modify your attempt:
Table 3 = VAR _NonZero_ = FILTER ( wc, wc[Win] <> 0 ) VAR _Ranked_ = ADDCOLUMNS ( _NonZero_, "Rank", VAR _Region = wc[Region] VAR _Country = wc[Country] VAR _Partition_ = FILTER ( _NonZero_, wc[Region] = _Region ) VAR _AddCol_ = ADDCOLUMNS ( _Partition_, "@win", wc[Win] ) VAR _SubIndex_ = SUBSTITUTEWITHINDEX ( _AddCol_, "@Rank", SUMMARIZE ( _AddCol_, [@win] ), [@win], DESC ) VAR _Row_ = FILTER ( _SubIndex_, wc[Country] = _Country ) VAR _Rank = MAXX ( _Row_, [@Rank] ) + 1 RETURN _Rank ) RETURN _Ranked_- AlexisOlson3 years agoSuper User
This feels a bit for efficient since it's iterating over regions instead of every row:
Table 3 = SELECTCOLUMNS ( GENERATE ( SUMMARIZE ( wc, wc[Region] ), VAR _Region = wc[Region] VAR _Partition_ = SELECTCOLUMNS ( FILTER ( wc, wc[Region] = _Region ), wc[Country], wc[Win] ) VAR _AddCol_ = ADDCOLUMNS ( _Partition_, "@win", wc[Win] ) VAR _SubIndex_ = SUBSTITUTEWITHINDEX ( _AddCol_, "Rank", SUMMARIZE ( _AddCol_, [@win] ), [@win], DESC ) RETURN _SubIndex_ ), "Region", wc[Region], "Country", wc[Country], "Win", wc[Win], "Rank", [Rank] + 1 )- smpa013 years agoCommunity Champion
AlexisOlson this is simply Brilliant !!! Thank you again.
I rewrote the core query as below (for me to understand)
Happy new year !!! AlexisOlson CNENFRNL