Skip to main content
cancel
Showing results for 
Search instead for 
Did you mean: 

Join us at FabCon Atlanta from March 16 - 20, 2026, for the ultimate Fabric, Power BI, AI and SQL community-led event. Save $200 with code FABCOMM. Register now.

Reply
Kazanskyi
Helper I
Helper I

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:

Top and Bottom =
VAR _table = FILTER(
                        VALUES(DIM_Product[Product CD]),
                        DIM_Product[Product CD] <> BLANK())
VAR _ranked_table =
                ADDCOLUMNS(
                        _table
                        ,"_rank",RANKX(_table,[Total Sales],,DESC,Skip))
VAR max_rank = MAXX(_ranked_table,[_rank])
VAR _top = filter(_ranked_table,[_rank]<=5)
VAR _bottom = filter(_ranked_table,[_rank]>max_rank-5)
RETURN
CALCULATE (
        [Total Sales],
        UNION(_top,_bottom),
        ALLSELECTED(DIM_Product[Product CD])
)
 
Here is a link to the sample dataset 
link

 

1 ACCEPTED SOLUTION

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 ) )
        )
    )

ThxAlot_0-1694552001664.png

 

But I didn't see any advantage over TOPN() or WINDOW().



Expertise = List.Accumulate(


        {Days as from Today},


        {Skills and Knowledge},


        (Current, Everyday) => Current & Day.LearnAndPractise(Everyday)


)



View solution in original post

8 REPLIES 8
Kazanskyi
Helper I
Helper I

Thank you for your answers @parry2k  and @ThxAlot !

parry2k
Super User
Super User

@ThxAlot I liked this one. I will use this one, much cleaner. 



Subscribe to the @PowerBIHowTo YT channel for an upcoming video on List and Record functions in Power Query!!

Learn Power BI and Fabric - subscribe to our YT channel - Click here: @PowerBIHowTo

If my solution proved useful, I'd be delighted to receive Kudos. When you put effort into asking a question, it's equally thoughtful to acknowledge and give Kudos to the individual who helped you solve the problem. It's a small gesture that shows appreciation and encouragement! ❤


Did I answer your question? Mark my post as a solution. Proud to be a Super User! Appreciate your Kudos 🙂
Feel free to email me with any of your BI needs.

parry2k
Super User
Super User

Regardless you have to calculate it twice. 🙂



Subscribe to the @PowerBIHowTo YT channel for an upcoming video on List and Record functions in Power Query!!

Learn Power BI and Fabric - subscribe to our YT channel - Click here: @PowerBIHowTo

If my solution proved useful, I'd be delighted to receive Kudos. When you put effort into asking a question, it's equally thoughtful to acknowledge and give Kudos to the individual who helped you solve the problem. It's a small gesture that shows appreciation and encouragement! ❤


Did I answer your question? Mark my post as a solution. Proud to be a Super User! Appreciate your Kudos 🙂
Feel free to email me with any of your BI needs.

parry2k
Super User
Super User

@Kazanskyi I think you shouldn't worry about if it is getting calculated twice or not, check the performance using the performance analyzer and then decide.



Subscribe to the @PowerBIHowTo YT channel for an upcoming video on List and Record functions in Power Query!!

Learn Power BI and Fabric - subscribe to our YT channel - Click here: @PowerBIHowTo

If my solution proved useful, I'd be delighted to receive Kudos. When you put effort into asking a question, it's equally thoughtful to acknowledge and give Kudos to the individual who helped you solve the problem. It's a small gesture that shows appreciation and encouragement! ❤


Did I answer your question? Mark my post as a solution. Proud to be a Super User! Appreciate your Kudos 🙂
Feel free to email me with any of your BI needs.

parry2k
Super User
Super User

@Kazanskyi and here is the output:

 

parry2k_0-1694549693409.png

 



Subscribe to the @PowerBIHowTo YT channel for an upcoming video on List and Record functions in Power Query!!

Learn Power BI and Fabric - subscribe to our YT channel - Click here: @PowerBIHowTo

If my solution proved useful, I'd be delighted to receive Kudos. When you put effort into asking a question, it's equally thoughtful to acknowledge and give Kudos to the individual who helped you solve the problem. It's a small gesture that shows appreciation and encouragement! ❤


Did I answer your question? Mark my post as a solution. Proud to be a Super User! Appreciate your Kudos 🙂
Feel free to email me with any of your BI needs.

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!

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 ) )
        )
    )

ThxAlot_0-1694552001664.png

 

But I didn't see any advantage over TOPN() or WINDOW().



Expertise = List.Accumulate(


        {Days as from Today},


        {Skills and Knowledge},


        (Current, Everyday) => Current & Day.LearnAndPractise(Everyday)


)



parry2k
Super User
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

 

 



Subscribe to the @PowerBIHowTo YT channel for an upcoming video on List and Record functions in Power Query!!

Learn Power BI and Fabric - subscribe to our YT channel - Click here: @PowerBIHowTo

If my solution proved useful, I'd be delighted to receive Kudos. When you put effort into asking a question, it's equally thoughtful to acknowledge and give Kudos to the individual who helped you solve the problem. It's a small gesture that shows appreciation and encouragement! ❤


Did I answer your question? Mark my post as a solution. Proud to be a Super User! Appreciate your Kudos 🙂
Feel free to email me with any of your BI needs.

Helpful resources

Announcements
Fabric Data Days Carousel

Fabric Data Days

Advance your Data & AI career with 50 days of live learning, contests, hands-on challenges, study groups & certifications and more!

October Power BI Update Carousel

Power BI Monthly Update - October 2025

Check out the October 2025 Power BI update to learn about new features.

FabCon Atlanta 2026 carousel

FabCon Atlanta 2026

Join us at FabCon Atlanta, March 16-20, for the ultimate Fabric, Power BI, AI and SQL community-led event. Save $200 with code FABCOMM.