Forum Discussion

Mackie's avatar
Mackie
Icon for Helper I rankHelper I
6 years ago
Solved

How to Cumulative Sum?

How can I calculate column "C" from "A" & "B" columns? Thank you.

 

abc
077
31017
6219
9423
12932
151850

 

 

 

  • Well, you cannot unless you have an Index column or the value of A for each row of increases in a sorted fashion. If the latter is indeed the case then you could do this:

     

    c =
      SUMX(FILTER('Table',[a] <= EARLIER([a])),[b])

3 Replies

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

    Well, you cannot unless you have an Index column or the value of A for each row of increases in a sorted fashion. If the latter is indeed the case then you could do this:

     

    c =
      SUMX(FILTER('Table',[a] <= EARLIER([a])),[b])
    • Mackie's avatar
      Mackie
      Icon for Helper I rankHelper I

      Thank you, Greg_Deckler . Your formulate works for me.

      If you know any page which explains "EARLIER" function, please let me know. 

      • Greg_Deckler's avatar
        Greg_Deckler
        Icon for Community Champion rankCommunity Champion
        Sure, think of EARLIER as the "current" value for a column in a row. The technical explanation is that it refers to an "earlier" evaluation context. So, in the formula above, the evaluation context that is previous to the evaluation context created by the FILTER statement. You could do the same thing with

        c =
        VAR __a = [a]
        RETURN
        SUMX(FILTER('Table',[a] <= __a),[b])

        But, it's more typing and I do enough of that so EARLIER is a nice shorthand. Plus, in more complex DAX calculations it avoids nested VAR RETURN statements. Pick your poison I guess.