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

Enhance your career with this limited time 50% discount on Fabric and Power BI exams. Ends August 31st. Request your voucher.

Reply
Anonymous
Not applicable

Keep only n first rows of a visual table based on a metric value ?

Hi !

 

I would like to visualy keep in my table (table as a visualization, not in terms of data source) only n first rows based on the value of a metric column for a category.

 

Exemple : in the below table, I would like to display only the 2 most sold product id in the category A and the 2 most sold in the category B. So I will visualy have only 4 rows in my table.

 

How would you do that ?

 

Thanks 😉

 

product idcategory 1category 2sales

FDFSDF

A

X543554
HFGHFGHBX34234
YRTYYAY7567
ERERZRAX234
gDFGDGBY23434
GFDGDFGAY5345345
JHGFAY4324
qFGDBY543354
gdFGGBX5345345
1 ACCEPTED SOLUTION
mahoneypat
Microsoft Employee
Microsoft Employee

Here is a measure that seems to work. Just replace Top2 with your actual table name.

 

mahoneypat_0-1643288969567.png

Top 2 AB =
VAR top2x =
    TOPN (
        2,
        ADDCOLUMNS (
            ALLSELECTED ( Top2[product id] ),
            "cSales"CALCULATE ( SUM ( Top2[sales] )ALL ( Top2[category 1] ) )
        ),
        [cSales], DESC
    )
VAR result =
    CALCULATE ( SUM ( Top2[sales] )KEEPFILTERS ( top2x ) )
RETURN
    result

 

Pat





Did I answer your question? Mark my post as a solution! Kudos are also appreciated!

To learn more about Power BI, follow me on Twitter or subscribe on YouTube.


@mahoneypa HoosierBI on YouTube


View solution in original post

3 REPLIES 3
mahoneypat
Microsoft Employee
Microsoft Employee

Here is a measure that seems to work. Just replace Top2 with your actual table name.

 

mahoneypat_0-1643288969567.png

Top 2 AB =
VAR top2x =
    TOPN (
        2,
        ADDCOLUMNS (
            ALLSELECTED ( Top2[product id] ),
            "cSales"CALCULATE ( SUM ( Top2[sales] )ALL ( Top2[category 1] ) )
        ),
        [cSales], DESC
    )
VAR result =
    CALCULATE ( SUM ( Top2[sales] )KEEPFILTERS ( top2x ) )
RETURN
    result

 

Pat





Did I answer your question? Mark my post as a solution! Kudos are also appreciated!

To learn more about Power BI, follow me on Twitter or subscribe on YouTube.


@mahoneypa HoosierBI on YouTube


Anonymous
Not applicable

Hi,

 

Thanks for your answer @ValtteriN ! The problem is that I have 150 categories so it will be very long for me to create one formula for each category.

ValtteriN
Super User
Super User

Hi,

You can create a filter measure like this:

Filter top 2 of categories =

Var catA = RANKX(all('Table (7)'),calculate(SUM('Table (7)'[sales]),'Table (7)'[Category 1]="A"),,DESC)

Var catB = RANKX(all('Table (7)'),calculate(SUM('Table (7)'[sales]),'Table (7)'[Category 1]="B"),,DESC)

Return

IF(or(catA<=2,catB<=2),1,0)
 
Then apply it like this:
ValtteriN_0-1643200834295.png

I hope this post helps to solve your issue and if it does consider accepting it as a solution and giving the post a thumbs up!





Did I answer your question? Mark my post as a solution!

Proud to be a Super User!




Helpful resources

Announcements
July 2025 community update carousel

Fabric Community Update - July 2025

Find out what's new and trending in the Fabric community.

July PBI25 Carousel

Power BI Monthly Update - July 2025

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