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

Be one of the first to start using Fabric Databases. View on-demand sessions with database experts and the Microsoft product team to learn just how easy it is to get started. Watch now

Reply
Ang0zyx
Frequent Visitor

DAX code to filter topn

Hi everyone,

Could you please help me to convert the below query to dax.the idea is to filter the overall top3 products and show for all countries.

 

SQL:
Select country,product,sales from SalesTable
where product in
(SELECT distinct product as top3product FROM (SELECT product,sales FROM SalesTable ORDER BY sales)

WHERE ROWNUM<=3)

 

I tried with below dax but is not working.when I add product and sales to a visual,it's properly showing 3 top products but on adding country, it shows all rows.

Calculate(sum(sales),keepfilters(topn(3,allselected(SalesTable[product]),sum(sales),desc)))

7 REPLIES 7
Ang0zyx
Frequent Visitor

Could anyone please help on this issue.

Ang0zyx
Frequent Visitor

Hi @Jihwan_Kim ,

Initially it worked but later on it failed with a single table. Please find the data,Measure & the issue below.

Total sales measure: =
SUM(Sales[Sales])

Top3 product sales measure: =

VAR topthreeproducts =
TOPN ( 10, ALL ( 'Sales'[Product] ), [Total sales measure:], DESC )
RETURN CALCULATE ([Total sales measure:],KEEPFILTERS (TOPN ( 10, ALL ( 'Sales'[Product] ), [Total sales measure:], DESC ) ) )
top3 product sales measure fix: =IF (CALCULATE ( [Top3 product sales measure:], REMOVEFILTERS ( Sales[Source] ) )
= BLANK (),BLANK (),[Top3 product sales measure:])


Issue: of the rows are missing when it is impleted through dax.

Ang0zyx_0-1656577801210.png

 


ProductSourceSales

A66S156
A77S129
A169S129
A223S118
A230S11
A231S11
A232S13
A247S129
A301S22
A301S184
A303S35
A303S24
A303S125
A712S321
A712S24
A712S128
A718S21
A729S318
A729S111
A767S133
A769S41
A783S315
A783S134
A794S327
A794S17

Hi,

I am not quite sure if I fully understand what your question is, but please check the below picture and the attached pbix file.

 

Untitled.png

Sales total: = 
SUM( Data[Sales] )
Sales by product: = 
 CALCULATE( [Sales total:], ALLEXCEPT(Data,Data[Product]))

 

Sales by product top10: = 
IF (
    [Sales by product:]
        = CALCULATE (
            [Sales by product:],
            KEEPFILTERS ( TOPN ( 10, ALL ( Data[Product] ), [Sales by product:], DESC ) )
        ),
    [Sales total:],
    BLANK ()
)

 

If this post helps, then please consider accepting it as the solution to help other members find it faster, and give a big thumbs up.


Visit my LinkedIn page by clicking here.


Schedule a meeting with me to discuss further by clicking here.

Ang0zyx
Frequent Visitor

Hi @Jihwan_Kim , Thanks a lot . It worked perfectly 😀

Ang0zyx
Frequent Visitor

Hi @Jihwan_Kim , It is not working when i add both the dimension in the Visual. Please find the screenshot below.

Top 3 Product Sales =
CALCULATE (
SUM ( SalesTable[Sales] ),
KEEPFILTERS (
TOPN (
3,
ALLSELECTED ( SalesTable[Product] ),
CALCULATE ( SUM ( SalesTable[Sales] ) ), DESC
)
)
)

Ang0zyx_0-1656352136487.png

 

Note:

1. I cannnot use visual filter TopN for some certain reason.
2. the data is in a single table like the Screenshot.

 

Hi,

Thank you for your feedback.

Please check the below picture and the attached pbix file.

All measures are in the attached pbix file.

 

Untitled.png

 

Total sales measure: = 
SUM(Sales[Total Sales])

 

Top3 product sales measure: = 
VAR topthreeproducts =
    TOPN ( 3, ALL ( 'Product'[Product] ), [Total sales measure:], DESC )
RETURN
    CALCULATE (
        [Total sales measure:],
        KEEPFILTERS (
            TOPN ( 3, ALL ( 'Product'[Product] ), [Total sales measure:], DESC )
        )
    )

 

top3 product sales measure fix: = 
IF (
    CALCULATE ( [Top3 product sales measure:], REMOVEFILTERS ( Country[Country] ) )
        = BLANK (),
    BLANK (),
    [Top3 product sales measure:]
)

If this post helps, then please consider accepting it as the solution to help other members find it faster, and give a big thumbs up.


Visit my LinkedIn page by clicking here.


Schedule a meeting with me to discuss further by clicking here.

Jihwan_Kim
Super User
Super User

Hi,

I am not sure how your data model looks like or how your desired outcome looks like, but please try the below.

 

 

new measure: =
CALCULATE (
    SUM ( SalesTable[sales] ),
    KEEPFILTERS (
        TOPN (
            3,
            ALLSELECTED ( SalesTable[product] ),
            CALCULATE ( SUM ( SalesTable[sales] ) ), DESC
        )
    )
)

 

If this post helps, then please consider accepting it as the solution to help other members find it faster, and give a big thumbs up.


Visit my LinkedIn page by clicking here.


Schedule a meeting with me to discuss further by clicking here.

Helpful resources

Announcements
Las Vegas 2025

Join us at the Microsoft Fabric Community Conference

March 31 - April 2, 2025, in Las Vegas, Nevada. Use code MSCUST for a $150 discount!

Dec Fabric Community Survey

We want your feedback!

Your insights matter. That’s why we created a quick survey to learn about your experience finding answers to technical questions.

ArunFabCon

Microsoft Fabric Community Conference 2025

Arun Ulag shares exciting details about the Microsoft Fabric Conference 2025, which will be held in Las Vegas, NV.

December 2024

A Year in Review - December 2024

Find out what content was popular in the Fabric community during 2024.