Forum Discussion

markus_zhang's avatar
markus_zhang
Icon for Advocate III rankAdvocate III
7 years ago
Solved

Can anyone explain why this works?

Hi experts, I'm reading the tutorial here: https://www.daxpatterns.com/new-and-returning-customers/   In the first meausre it presents:     =COUNTROWS ( FILTER ( ADDCOLUMNS ( ...
  • AlB's avatar
    AlB
    7 years ago

    markus_zhang 

    I see your point. I was similarly bewildered when first presented with this type of formulae.

     

    There are two tricky subjects here, by order of importance:

    1. What 'Date'[Full Date] actually refers to.

    2. The nuances of how ALL( ) actually works     

     

    The behaviour of ALL depends on where it is used. It should probably have two different names. Since you're already familiar with the Italian gurus, check out this article for details. ALL can work either by removing filters or by ignoring filters. It sounds like the same but it sure ain't.

     

    Let us walk through your example:

    The filter context

    We have 'Date'[Full Date] in the rows of our pivot table. That determines the filter context. 

     

    Inside FILTER( )

    FILTER( ) first computes the table it will operate on. In this case, ALL('Date'). That is the full 'Date' table in your data model. Let's call it Table=ALL('Date').

    Nota bene: ALL( ) here ignores filter context but it does NOT remove it, so filter context will be effective everywhere else.

     

    Table has a 'Date'[Full Date] column as well, an instance different from the one in the pivot that we marked in red above. We'll use purple for Table's : 'Date'[Full Date]. So we have one name referring to two different instances. We'll have to see how we tell them apart.

     

    FILTER( ) and filter context interaction

    Once it has Table, FILTER( ) starts scanning it to check whether each row complies with the condition:

    'Date'[FullDate] < MIN ( 'Date'[FullDate] )

     

    'Date'[FullDate] is the value in the current row of Table. MIN('Date'[FullDate]), however, is the minimum of the values of 'Date'[Full Date] coming from the pivot table (filter context). This is the crux.

     

    Conclusions

    You do have a valid point in that MIN('Date'[FullDate]) could actually be referring to MIN('Date'[FullDate]), in which case every row would certainly be filtered out.

    How does DAX discriminate between 'Date'[FullDate] and  'Date'[FullDate]?

    Basically, when you use the "naked" column, i.e. the column on its own, it's 'Date'[FullDate]. In any other case you'll be referring to 'Date'[Full Date]. It's been built that way so that you can refer to both instances and build powerful code like the one you've shown.

    As a final point, bear in mind that filter context affects both arguments in

         FILTER(<table>; <filter expression>)

    In this case the filter context is ignored in the first argument because we are using ALL( ). It'd be another story if we were using VALUES( ).

     

    Does that help? (I do hope so, as it took some time to put together :smileywink:)