Forum Discussion

Anonymous's avatar
Anonymous
Not applicable
5 years ago
Solved

Theory Question on using DATESMTD with CALCULATE

Hi All,

MS documentation provides this example below to explain DATESMTD function:

CALCULATE(SUM(InternetSales_USD[SalesAmount_USD]), DATESMTD(DateTime[DateKey]))

 

My question is that per the syntax of CALCULATE function, DATESMTD seems like a Filter.

But, here it is not filtering anything. It is just changing the context of calculation to be done per month-to-date.

Then, why is DATESMTD function in the example above considered and placed as "FILTER" in CALCULATE formula?

Is FILTER not equivalent to removing items?

  • Anonymous 

     

    CALCULATE function accepts the following type of parameters after the Expression.

    • Boolean filter expressions
    • Table filter expressions
    • Filter modification functions

    In your MTD example it is a Table Filter expression that is applied here. CALCULATE filters the DateTime[DateKey] column with a range of dates from 1st of the month to the current date in the background when you apply DATESMTD function.

    Hope this clear your question?

    Articles: 
    https://dax.guide/calculate/
    https://docs.microsoft.com/en-us/dax/calculate-function-dax


  • Hi Anonymous 

    It's a valid point. It all comes down to terminology. Here you should read FILTER not as the usual selection of rows operation but as filter context.  The whole point of  CALCULATE is evaluating the expression in a modified filter context.  Each of those "FILTER" arguments described in the documentation is used for modifying the filter context   and can be either a:

    A) filter removal operation (such as ALL, ALLEXCEPT, ALLNOBLANKROW)

    B) filter restore operation (ALLSELECTED)

    C) table expression, returning a list of values for one or more columns or for an entire expanded table

     

    Point C) would be the one at play in your DATESMTD example 

    Have a look at https://dax.guide/calculate/  for a more comprehensive overview

     

    Please accept the solution when done and consider giving a thumbs up if posts are helpful. 

    Contact me privately for support with any larger-scale BI needs, tutoring, etc.

     

4 Replies

  • Anonymous 

     

    CALCULATE function accepts the following type of parameters after the Expression.

    • Boolean filter expressions
    • Table filter expressions
    • Filter modification functions

    In your MTD example it is a Table Filter expression that is applied here. CALCULATE filters the DateTime[DateKey] column with a range of dates from 1st of the month to the current date in the background when you apply DATESMTD function.

    Hope this clear your question?

    Articles: 
    https://dax.guide/calculate/
    https://docs.microsoft.com/en-us/dax/calculate-function-dax


    • Anonymous's avatar
      Anonymous
      Not applicable

      Thanks Fowmy that explanation helped. Just a small follow-up query:

      Does my explanation below make any sense? I mean does it support, counter your explanation or is it incorrect totally?

      DATESMTD (DateTable[Date]) which according to CALCULATE function's syntax appears as being a filter is actually just a context. And when we hover over that FILTER part in the formula- PBI's intelligence suggests that [Filter1] "evaluates an expression in a context modified by filters". Which seem to suggest that even though if it doesn't remove (filter) items it still does calculate the expression per the context provided by DATESMTD

      • Fowmy's avatar
        Fowmy
        Icon for Super User rankSuper User

        Anonymous 

        You are right. One point on removing filters in addition to adding filters is that CALCULATE also accepts filter modifiers.


  • AlB's avatar
    AlB
    Icon for Community Champion rankCommunity Champion

    Hi Anonymous 

    It's a valid point. It all comes down to terminology. Here you should read FILTER not as the usual selection of rows operation but as filter context.  The whole point of  CALCULATE is evaluating the expression in a modified filter context.  Each of those "FILTER" arguments described in the documentation is used for modifying the filter context   and can be either a:

    A) filter removal operation (such as ALL, ALLEXCEPT, ALLNOBLANKROW)

    B) filter restore operation (ALLSELECTED)

    C) table expression, returning a list of values for one or more columns or for an entire expanded table

     

    Point C) would be the one at play in your DATESMTD example 

    Have a look at https://dax.guide/calculate/  for a more comprehensive overview

     

    Please accept the solution when done and consider giving a thumbs up if posts are helpful. 

    Contact me privately for support with any larger-scale BI needs, tutoring, etc.