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

Get Fabric Certified for FREE during Fabric Data Days. Don't miss your chance! Request now

Reply
smpa01
Super User
Super User

DAX to filter table based on TOPN aggregation

I have a table like this

 

| name | sale |
|------|------|
| a    | 1000 |
| a    | 1500 |
| b    | 2000 |
| b    | 1500 |
| c    | 2000 |
| c    | 2000 |
| d    | 5000 |

 

and I am trying to write a measure to utilize TOPN that would return the filter table based on TOP 2 sale by name.

I want to end up with this

 

| name | sale |
|------|------|
| d    | 5000 |
| c    | 4000 |

 

The equivalent SQL is following

 

declare @t1 as table (name varchar(1),sale int)
insert into @t1
select * from
(values('a',1000),('a',1500),('b',2000),('b',1500),('c',2000),('c',2000),('d',5000)) t(a,b)

;with cte1 as(
select name, SUM(sale) as sale
from @t1
group by name
)
select TOP 2 * from cte1
order by sale desc

 

 

smpa01_0-1639871964020.png

 

But in DAX if I try the following

 

Measure =
CALCULATE (
    SUM ( 'Table'[sale] ),
    CALCULATETABLE (
        'Table',
        TOPN ( 2, VALUES ( 'Table'[name] ), CALCULATE ( SUM ( 'Table'[sale] ) ), DESC )
    )
)

 

it does not do anything

smpa01_1-1639872198133.png

 

However, I can get to where I want to by using the following set up

smpa01_2-1639872353813.png

 

But I am simply trying to undertand, if it is possible to filter the table using TOPN.

 

@AlexisOlson  @CNENFRNL 

Did I answer your question? Mark my post as a solution!
Proud to be a Super User!
My custom visualization projects
Plotting Live Sound: Viz1
Beautiful News:Viz1, Viz2, Viz3
Visual Capitalist: Working Hrs
1 ACCEPTED SOLUTION
parry2k
Super User
Super User

@smpa01 here it is, tweak it as you see fit

 

Top Name = 
CALCULATE ( 
    [Sum Sales], 
    TOPN ( 
        2, 
        ALLSELECTED ('Top'[name] ), 
        [Sum Sales], 
        DESC 
    ), 
    VALUES ('Top'[name] ) 
) 

 

Follow us on LinkedIn

 

Learn about conditional formatting at Microsoft Reactor

My latest blog post The Power of Using Calculation Groups with Inactive Relationships (Part 1) (perytus.com) I would  Kudos if my solution helped. 👉 If you can spend time posting the question, you can also make efforts to give Kudos to whoever helped to solve your problem. It is a token of appreciation!

 

Visit us at https://perytus.com, your one-stop-shop for Power BI-related projects/training/consultancy.



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.

View solution in original post

5 REPLIES 5
CNENFRNL
Community Champion
Community Champion

This measure functions equally instead of visual level filter

Sales = SUMX(FILTER(VALUES('Table'[name]), [_rank]<3),[_sum])

Thanks to the great efforts by MS engineers to simplify syntax of DAX! Most beginners are SUCCESSFULLY MISLED to think that they could easily master DAX; but it turns out that the intricacy of the most frequently used RANKX() is still way beyond their comprehension!

DAX is simple, but NOT EASY!

parry2k
Super User
Super User

@smpa01 you can also do this:

 

Top Name = 
CALCULATE ( 
    [Sum Sales], 
    KEEPFILTERS (
        TOPN ( 
            2, 
            ALLSELECTED ('Top'[name] ), 
            [Sum Sales], 
            DESC 
        )
    )
    //VALUES ('Top'[name] ) 
) 


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.

@smpa01 ^^ This is exactly what I would have suggested.

parry2k
Super User
Super User

@smpa01 here it is, tweak it as you see fit

 

Top Name = 
CALCULATE ( 
    [Sum Sales], 
    TOPN ( 
        2, 
        ALLSELECTED ('Top'[name] ), 
        [Sum Sales], 
        DESC 
    ), 
    VALUES ('Top'[name] ) 
) 

 

Follow us on LinkedIn

 

Learn about conditional formatting at Microsoft Reactor

My latest blog post The Power of Using Calculation Groups with Inactive Relationships (Part 1) (perytus.com) I would  Kudos if my solution helped. 👉 If you can spend time posting the question, you can also make efforts to give Kudos to whoever helped to solve your problem. It is a token of appreciation!

 

Visit us at https://perytus.com, your one-stop-shop for Power BI-related projects/training/consultancy.



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  this is extremely elegant and smooth !!! Thanks for this one.

Did I answer your question? Mark my post as a solution!
Proud to be a Super User!
My custom visualization projects
Plotting Live Sound: Viz1
Beautiful News:Viz1, Viz2, Viz3
Visual Capitalist: Working Hrs

Helpful resources

Announcements
November Power BI Update Carousel

Power BI Monthly Update - November 2025

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

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!

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.

Top Solution Authors