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

Register now to learn Fabric in free live sessions led by the best Microsoft experts. From Apr 16 to May 9, in English and Spanish.

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.


Go to My LinkedIn Page


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.


Go to My LinkedIn Page


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.


Go to My LinkedIn Page


Helpful resources

Announcements
Microsoft Fabric Learn Together

Microsoft Fabric Learn Together

Covering the world! 9:00-10:30 AM Sydney, 4:00-5:30 PM CET (Paris/Berlin), 7:00-8:30 PM Mexico City

PBI_APRIL_CAROUSEL1

Power BI Monthly Update - April 2024

Check out the April 2024 Power BI update to learn about new features.

April Fabric Community Update

Fabric Community Update - April 2024

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

Top Solution Authors