Forum Discussion
Ranking with multiple rows and drilldown
- 2 years ago
Hi again VBLOT
Thanks so much for the file! That made it a lot easier to debug.
It seems that the problem actually relates to how the visual level filter "Sum of Nb samples collected > 0" interacts with the ALLSELECTED () modifier used to produce the table for ranking.
A safer way to write the measure is below (see attached PBIX). Actually in their Whitepaper, SQLBI recommend this general approach of adding column(s) to the "relation" argument of window functions.
I might have to get back to you with an explanation of why it works when I've analyzed further.
Rank FIX = VAR IsLineInScope = ISINSCOPE ( 'Invoices List'[Line Designation] ) VAR IsREFInScope = ISINSCOPE ( 'Invoices List'[REF] ) VAR Result = SWITCH ( TRUE ( ), IsLineInScope && IsREFInScope, RANK ( DENSE, CALCULATETABLE ( ADDCOLUMNS ( SUMMARIZE ( 'Invoices List', 'Invoices List'[Line Designation], 'Invoices List'[REF] ), "@TotalSamples", [Total Samples collected] ), ALLSELECTED () ), ORDERBY ( [@TotalSamples], DESC ), , PARTITIONBY('Invoices List'[REF]) ), NOT IsLineInScope && IsREFInScope, RANK ( DENSE, CALCULATETABLE ( ADDCOLUMNS ( SUMMARIZE ( 'Invoices List', 'Invoices List'[REF] ), "@TotalSamples", [Total Samples collected] ), ALLSELECTED ( ) ), ORDERBY ( [@TotalSamples], DESC ) ) ) RETURN ResultRegards
Hi VBLOT
Your logic is essentially correct!
In fact your measure works fine in a test PBIX I created, as long as [Total Samples Collected] always has positive values (example in attached PBIX).
However, since it wasn't working correctly for you, it's possible that there is a complication in your model. Are there any sort-by columns defined by any chance?
A more robust way I would suggest writing this measure is using the RANK function, since we can specify partitioning.
Rank =
VAR IsLineInScope =
ISINSCOPE ( 'Invoices List'[Line Designation] )
VAR IsREFInScope = ISINSCOPE ( 'Invoices List'[REF] )
VAR Result =
SWITCH (
TRUE ( ),
IsLineInScope && IsREFInScope,
RANK (
DENSE,
CALCULATETABLE (
SUMMARIZE (
'Invoices List',
'Invoices List'[Line Designation],
'Invoices List'[REF]
),
ALLSELECTED ( )
),
ORDERBY ( [Total Samples Collected], DESC ),
PARTITIONBY ( 'Invoices List'[REF] )
),
NOT IsLineInScope && IsREFInScope,
RANK (
DENSE,
CALCULATETABLE (
SUMMARIZE ( 'Invoices List', 'Invoices List'[REF] ),
ALLSELECTED ( )
),
ORDERBY ( [Total Samples Collected], DESC )
)
)
RETURN
Result
PBIX attached for reference
- Rank Original is your posted measure.
- Rank is updated measures.
Regards
- VBLOT2 years ago
Helper I
Hello OwenAuger !
Thanks for your answer ! It is indeed working in the PBI you linked but sadly it doesn't work on mine.
I've checked and I don't have any sort-by columns in Invoices List, the code in entering conditions as expected but somehow, the rank part in the REF scope isn't working and I still have 1 everywhere...
I also know that I have blanks in my Nb samples collected. But it shouldn't have any impacts as I filtered the result to have only those greater than 0 and in my Total Samples collected measure, I make sure that I don't consider BLANK().Total Samples collected = CALCULATE( SUM('Invoices List'[Nb samples collected]), FILTER('Invoices List','Invoices List'[Nb samples collected] <> BLANK()) )Do you have another idea ?