Forum Discussion

Anonymous's avatar
Anonymous
Not applicable
7 years ago
Solved

Cumulative total query

Hi,   I know the following formula works to find the cumulative total but I don't fully understand how it works:   [CumulativeProducts] := CALCULATE ( SUM ( Sales[Quantity] ), FILTER ( ...
  • Cmcmahan's avatar
    Cmcmahan
    7 years ago

    I actually went through on some of my own data to create a cumulative total in this manner (since the example given is not exactly the way I would do it) and I think the example might be wrong.  Have you actually implemented this cumulative total on the sample data they provide? I would be interested if it does work.  I'm sure there's some way to do it the way they say using RELATED, but when I started from scratch, I ended up with this:

    Cumulative Amt = CALCULATE(SUM('Sales'[Quantity]), FILTER(ALL('Sales'), 'Sales'[Date] <= MAX('Sales'[Date]) ))


    The big difference is that I'm not filtering the Date table, I'm filtering the Sales table. However, I think this example is still an example of what you find confusing.  There's nothing special about the MAX function. It could be FIRSTNONBLANK, SELECTEDVALUE, MIN, or any other expression that defines what the "previous" values for the cumulative total are.  What is special is the FILTER function.  

     

    The filter expression is actually iterated against every row in the table (in this case ALL('Sales')) to see if it evaluates to true.  It's checking that for each row the 'Sales'[Date] is less than or equal to the value of "MAX('Sales'[Date])".  It's my understanding that because your MAX expression doesn't have a new context explicitly defined, it evaluates once at a level out, essentially evaluating against the data before the ALL is applied.  

    I've looked over documentation to see if there's anywhere that the context switch is explicitly called out, or articles that explain it, but I can't seem to find any.  I've never questioned it, because how else would you be able to filter a table based on a current value? The automatic context switching is incredibly useful and feels natural to me, so I feel like I can't explain further.  Maybe somebody like marcorusso or Greg_Deckler might be able to give a better explanation on how this works.