Forum Discussion

Anonymous's avatar
Anonymous
Not applicable
5 years ago
Solved

FILTER vs CALCULATETABLE based on Boolean Expression

Hi Experts,

I am looking to understand why is that for FILTER to work in my code, I have to used DATESBETWEEN as a true/ false expression whereas for CALCULATETABLE to work similarly, it needs to be used as a table filter expression not Boolean
1) With FILTER, using DATESBETWEEN as

This wouldn't work if I change the highlighted portion to just this

DATESBETWEEN('Calendar'[Transaction_Date], DATE(2018,11,18), DATE(2018,11,24))
 
2) On the other hand, CALCULATETABLE works the other way around
It works with the code in blue above and not with how FILTER worked as highlighted by green line
If I use it the way FILTER worked, i see this error

Please suggest!

  • Anonymous's avatar
    Anonymous
    5 years ago

    That's because the second argument to FILTER must ALWAYS be a logical condition (or castable to it) that operates for each row of the table in the first argument (FILTER is an iterator) and all CALCULATETABLE's arguments must ALWAYS be tables. DATESBETWEEN returns a table.

     

    Even the seemingly logical condition in:

     

    calculatetable( A, T[Col] = Value )

     

    is internally translated into:

     

    calculatetable( A, Filter( All(T[Col]), T[Col] = Value ).

     

    So the "logicals" under CALCULATETABLE are just syntactic sugar.

  • Anonymous's avatar
    Anonymous
    5 years ago

    If you read the ultimate source of knowledge on DAX, which is The Book, you'll find the explanation of the use of the seemingly "logical expressions" under CALCULATETABLE that I gave you. No, what the docs mention is syntactic sugar. Only.

     

    Check this out: How CALCULATE works in DAX - SQLBI

     

    Well, Microsoft is just not telling you the whole truth but The Italians do.

5 Replies

  • Anonymous's avatar
    Anonymous
    Not applicable

    That's because the second argument to FILTER must ALWAYS be a logical condition (or castable to it) that operates for each row of the table in the first argument (FILTER is an iterator) and all CALCULATETABLE's arguments must ALWAYS be tables. DATESBETWEEN returns a table.

     

    Even the seemingly logical condition in:

     

    calculatetable( A, T[Col] = Value )

     

    is internally translated into:

     

    calculatetable( A, Filter( All(T[Col]), T[Col] = Value ).

     

    So the "logicals" under CALCULATETABLE are just syntactic sugar.

  • Anonymous's avatar
    Anonymous
    Not applicable

    Anonymous You mentioned that "CALCULATETABLE's arguments must ALWAYS be tables"
    Before posting this question, I checked the MS docs, and what I noticed there was the following:-
    1) That CALCULATETABLE does allow boolean expressions- it can be either boolean or table expression
    2) Boolean expressions must not use a function that returns a table

    I am highlighting these points in green

     

    Now, coming back to my question. Because of these 2 points above (which seem cotradictory to your point that they should always be tables, pardon my ignore) I wanted to try and use the boolean expression using IN as used in FILTER in CALCULATETABLE too

  • Anonymous's avatar
    Anonymous
    Not applicable

    Anonymous This just sealed the deal for me-