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
Moatasem
Frequent Visitor

A Question in Evaluation Context for ALL Function

Hello everyone,

 

I was trying to build a table that displays the top 5 subcategories by sales, I build the dax formula and it worked fine

 

Top 5 Subcategories 01 = 
var _top_subcategries = TOPN( 5, ALL( 'Product'[ProductSubcategoryName] ), [Offline Sales], DESC )
return 
CALCULATE( [Offline Sales], _top_subcategries, VALUES( 'Product'[ProductSubcategoryName] ) )

 

evaluation context question 01.PNG

After adding the category name to the table, instead of getting the same top 5 subcategories, I got the full list of subcategories.
evaluation context question 02.PNG

 

after a lot of try and error, I updated the formula as below, and the formula gave the expected output.

 

Top 5 Subcategories 02 = 
var _top_subcategries = TOPN( 5, ALL( 'Product'[ProductSubcategoryName], 'Product'[ProductCategoryName] ), [Offline Sales], DESC )
return 
CALCULATE( [Offline Sales], _top_subcategries, VALUES( 'Product'[ProductSubcategoryName] ) )

 

 

My questions

  1. Why adding ProductCategoryName to the table led to this issue? 
    here I discovered that, after adding the category to the table, the result of the TOPN function was all the subcategories.
  2. Why adding ProductCategoryName to the ALL function solves it?
1 ACCEPTED SOLUTION
CNENFRNL
Community Champion
Community Champion

Hi, @Moatasem , I think this seemingly simple change, in fact, involves at least 3 profound concepts in DAX calculation, namely auto-existdata lineage and evaluation context😂 You might want to study those linked articles I placed for wrapping your head around some sophisticate mechanisms of DAX under the hood.

 

Let's get back to your measure and break it down,

First

 

 

ALL( 'Product'[ProductSubcategoryName], 'Product'[ProductCategoryName] )

 

 

materialises a table with all possible combinations in 'Product' due to auto-exist (let's name it "prod").

 

Secondly,

TOPN filters 'prod' with data lineage being kept, which means 'prod'[ProductCategoryName] and 'prod'[ProductSubCategoryName] reference original 'Product'[ProductCategoryName] and 'Product'[ProductSubCategoryName] respectively with filtered values by TOPN.

 

Last but not the least,

 

 

CALCULATE( [Offline Sales], _top_subcategries, VALUES( 'Product'[ProductSubcategoryName] ) )

 

 

2 filter modifiers, namely _top_subcategories and VALUES(...) override initial filter context from the table viz.

 

Btw, I authored a more flexible measure which you might want to test, if you'd like to add to your report more slicers such as date, region etc.

 

 

 

Top 5 Subcategories 03 =
VAR _top_subcategries =
    TOPN (
        5,
        ALL ( 'Product'[ProductSubcategoryName] ),
        CALCULATE (
            [Offline Sales],
            ALL ( 'Product'[ProductCategoryName] )
        ), DESC
    )
RETURN
    CALCULATE ( [Offline Sales], KEEPFILTERS ( _top_subcategries ) )

 

 

 

Pls update me with the result of performance if you test it.

 

Enjoy DAX!


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!

View solution in original post

3 REPLIES 3
Moatasem
Frequent Visitor

@Nathaniel_C Thanks for your promt reply. it helped me to figure out the missing part while trying to understand the evalulation context "your measure is not filtering that column and so in the visual you get every category".

 

@CNENFRNL Thanks for the detailed reply, and the links for these concepts.

I will check them, and try to understand how they affect the final filter context for the formulas that I wrote.

 

The formula that you provided is working as expected, Thanks again.

CNENFRNL
Community Champion
Community Champion

Hi, @Moatasem , I think this seemingly simple change, in fact, involves at least 3 profound concepts in DAX calculation, namely auto-existdata lineage and evaluation context😂 You might want to study those linked articles I placed for wrapping your head around some sophisticate mechanisms of DAX under the hood.

 

Let's get back to your measure and break it down,

First

 

 

ALL( 'Product'[ProductSubcategoryName], 'Product'[ProductCategoryName] )

 

 

materialises a table with all possible combinations in 'Product' due to auto-exist (let's name it "prod").

 

Secondly,

TOPN filters 'prod' with data lineage being kept, which means 'prod'[ProductCategoryName] and 'prod'[ProductSubCategoryName] reference original 'Product'[ProductCategoryName] and 'Product'[ProductSubCategoryName] respectively with filtered values by TOPN.

 

Last but not the least,

 

 

CALCULATE( [Offline Sales], _top_subcategries, VALUES( 'Product'[ProductSubcategoryName] ) )

 

 

2 filter modifiers, namely _top_subcategories and VALUES(...) override initial filter context from the table viz.

 

Btw, I authored a more flexible measure which you might want to test, if you'd like to add to your report more slicers such as date, region etc.

 

 

 

Top 5 Subcategories 03 =
VAR _top_subcategries =
    TOPN (
        5,
        ALL ( 'Product'[ProductSubcategoryName] ),
        CALCULATE (
            [Offline Sales],
            ALL ( 'Product'[ProductCategoryName] )
        ), DESC
    )
RETURN
    CALCULATE ( [Offline Sales], KEEPFILTERS ( _top_subcategries ) )

 

 

 

Pls update me with the result of performance if you test it.

 

Enjoy DAX!


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!

Nathaniel_C
Super User
Super User

Hi @Moatasem ,
When you add the category column, your measure is not filtering that column and so in the visual you get every category. When you add both columns to the ALL() then it returns a table with the whole table that TOPN filters for the top n values, but now the Category column is filtered as well.

Let me know if you have any questions.

If this solves your issues, please mark it as the solution, so that others can find it easily. Kudos 👍are nice too.
Nathaniel





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

Proud to be a Super User!




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