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

Find everything you need to get certified on Fabric—skills challenges, live sessions, exam prep, role guidance, and more. Get started

Reply
Dimitris_Kats
Helper V
Helper V

Best way to exclude values with dax

Hello,

 

I am new to dax and i would really like your advice.
I have a product table with products assigned to categories.
We decided to exclude some products of the category 1 and i needed a measure to calculate only the products that were not excluded and another measure for only the products we excluded.
I managed to do it, but i was thinking there is a better and more efficient way to write that measure.

This is my table:

PRODUCT IDPRODUCT CATEGORY 1PRODUCT CATEGORY 2PRODUCT CATEGORY 3PRODUCT CATEGORY 4PRODUCT CATEGORY 5
10001    
10011    
10021    
10031    
1004 1   
1005 1   
1006     
10071    
10081    
10091    
1010  1  
1011   1 
1012    1
10131    
10141    
1015 1   
1016 1   
1017 1   
1018 1   
1019  1  
1020   1 
10211    
1022    1
10231    
10241    

 

And these are my measures: 

Correct Product Category = CALCULATE(SUM(PRODUCTS[PRODUCT CATEGORY 1]), FILTER(PRODUCTS,PRODUCTS[PRODUCT ID] <> 1000), FILTER(PRODUCTS,PRODUCTS[PRODUCT ID] <> 1001), FILTER(PRODUCTS,PRODUCTS[PRODUCT ID] <> 1007) , FILTER(PRODUCTS,PRODUCTS[PRODUCT ID] <> 1014) , FILTER(PRODUCTS,PRODUCTS[PRODUCT ID] <> 1023))
 
Product Exceptions = CALCULATE(SUM(PRODUCTS[PRODUCT CATEGORY 1]), FILTER(PRODUCTS, PRODUCTS[PRODUCT ID] = 1000 ||PRODUCTS[PRODUCT ID] = 1001|| PRODUCTS[PRODUCT ID] = 1007||PRODUCTS[PRODUCT ID] = 1014 ||PRODUCTS[PRODUCT ID] = 1023))
 
I would really appreciate your suggestions.
 
Thank you in advance 

 

2 ACCEPTED SOLUTIONS
Payeras_BI
Super User
Super User

Hi @Dimitris_Kats,

An alternative to these two measures:

Correct Product Category ii = 
CALCULATE (
    SUM ( PRODUCTS[PRODUCT CATEGORY 1] ),
    KEEPFILTERS(NOT PRODUCTS[PRODUCT ID] IN {1000,1001,1007,1014,1023})
)
Product exceptions ii = 
CALCULATE (
    SUM ( PRODUCTS[PRODUCT CATEGORY 1] ),
    KEEPFILTERS(PRODUCTS[PRODUCT ID] IN {1000,1001,1007,1014,1023})
)

 

If this post answered your question, please mark it as a solution to help other users find useful content.
Kudos are another nice way to acknowledge those who tried to help you.

J. Payeras
Mallorca, Spain

View solution in original post

Payeras_BI
Super User
Super User

Hi again @Dimitris_Kats,

Thanks to you for your kind words. 😉

Yes, you are right, I thought the operators NOT and IN would help to simplify the code.

Regarding using the KEEPFILTERS modifier, it was because I assumed you could use the measures in a table / matrix visualization like the one below:

Payeras_BI_0-1633083027830.png

If so, you want to keep the outer filter on Product ID and intersect it with the filter provided in your Calculate statement.

Without removing KEEPFILTERS you would get this:

Payeras_BI_1-1633085601973.png

I hope this helps you.

 

 

 

If this post answered your question, please mark it as a solution to help other users find useful content.
Kudos are another nice way to acknowledge those who tried to help you.

J. Payeras
Mallorca, Spain

View solution in original post

5 REPLIES 5
Payeras_BI
Super User
Super User

Hi again @Dimitris_Kats,

Thanks to you for your kind words. 😉

Yes, you are right, I thought the operators NOT and IN would help to simplify the code.

Regarding using the KEEPFILTERS modifier, it was because I assumed you could use the measures in a table / matrix visualization like the one below:

Payeras_BI_0-1633083027830.png

If so, you want to keep the outer filter on Product ID and intersect it with the filter provided in your Calculate statement.

Without removing KEEPFILTERS you would get this:

Payeras_BI_1-1633085601973.png

I hope this helps you.

 

 

 

If this post answered your question, please mark it as a solution to help other users find useful content.
Kudos are another nice way to acknowledge those who tried to help you.

J. Payeras
Mallorca, Spain

That was really helpful, i totaly understand what you mean and why you used KEEPFILTERS.

Thank you very much again for the detailed explanation 

Your help is greatly appreciated 

Payeras_BI
Super User
Super User

Hi @Dimitris_Kats,

An alternative to these two measures:

Correct Product Category ii = 
CALCULATE (
    SUM ( PRODUCTS[PRODUCT CATEGORY 1] ),
    KEEPFILTERS(NOT PRODUCTS[PRODUCT ID] IN {1000,1001,1007,1014,1023})
)
Product exceptions ii = 
CALCULATE (
    SUM ( PRODUCTS[PRODUCT CATEGORY 1] ),
    KEEPFILTERS(PRODUCTS[PRODUCT ID] IN {1000,1001,1007,1014,1023})
)

 

If this post answered your question, please mark it as a solution to help other users find useful content.
Kudos are another nice way to acknowledge those who tried to help you.

J. Payeras
Mallorca, Spain

Thanx so much for this information

Thank you very much.

Both of them are working perfectly.
Is it too much to ask to explain me a little bit how your measures works?

You added keepfilters because you want to preserve the filters on products id right?
And you use NOT & IN to exclude the ID's. Is that correct??
Thank you very much again for replying to me so fast and of course for the amazing measures.

Helpful resources

Announcements
Sept PBI Carousel

Power BI Monthly Update - September 2024

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

September Hackathon Carousel

Microsoft Fabric & AI Learning Hackathon

Learn from experts, get hands-on experience, and win awesome prizes.

Sept NL Carousel

Fabric Community Update - September 2024

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