Forum Discussion
Remove row context from measure with calculated table
I want to create a measure to show values only for TOP 5 and Bottom 5 performers together in one table.
I know how to do this with TOPN, but I need to repeat it with RANKX and compare performance as the amount of data is big.
The calculated table in the formula works as expected when I check with a physical table, but I can't make it work when I'm trying to use calculation in a measure.
Here is the formula:
link
If you insist,
Top and Bottom VirtualTable = VAR __t = ADDCOLUMNS( ALLSELECTED( DIM_Product[Product CD] ), "@sales", [Sum Sales] ) RETURN CALCULATE( [Sum Sales], KEEPFILTERS( UNION( TOPN( 5, __t, [@sales] ), TOPN( 5, __t, [@sales], ASC ) ) ) )But I didn't see any advantage over TOPN() or WINDOW().
8 Replies
- parry2k
Super User
Kazanskyi I love new window functions, and tried that to solve it:
Measure = CALCULATE ( [Total Sales], KEEPFILTERS ( UNION ( WINDOW ( 1, ABS, 5, ABS, ALLSELECTED ( DIM_Product ), ORDERBY ( [Total Sales], DESC ) ), WINDOW ( 1, ABS, 5, ABS, ALLSELECTED ( DIM_Product ), ORDERBY ( [Total Sales], ASC ) ) ) ) )You can apply further filters if you want, like checking the BLANK product etc.
If you are interested in learning these new functions, check the playlist on my YT channel: https://www.youtube.com/playlist?list=PLiYSIjh4cEx0BDzmo48YIPzw_dIC0Kd95
- parry2k
Super User
- Kazanskyi
Helper I
Thank you for your idea, parry2k
I did something similar with TOPN, but the problem with these solutions is that the table is recalculated 2 times to obtain the output.
With RANKX it should be only 1 iteration that in theory should be faster.
If you have an approach how to do this with one iteration, that would be amazing!
- ThxAlot
Super User
If you insist,
Top and Bottom VirtualTable = VAR __t = ADDCOLUMNS( ALLSELECTED( DIM_Product[Product CD] ), "@sales", [Sum Sales] ) RETURN CALCULATE( [Sum Sales], KEEPFILTERS( UNION( TOPN( 5, __t, [@sales] ), TOPN( 5, __t, [@sales], ASC ) ) ) )But I didn't see any advantage over TOPN() or WINDOW().
- parry2k
Super User
Regardless you have to calculate it twice. 🙂
- Kazanskyi
Helper I