35

For this question, I've created a Google Apps Script function, that calculates the cumulative sum of a range. Martin Hawksey on Google+ made a comment about a more efficient way of calculating the cumulative sum:

function cumulativeSum(array){
  var output = [];
  for (i in array){
    if(i==0) {
      if (array[i].length > 1) throw ("Only single column of data");
      output.push([array[i][0]]);
    } else {
      output.push([output[i-1][0] + array[i][0]]);
    }
  }
  return output;
}

My question is: can this be achieved with the usage of formula's?

  • Apologies for a very late answer, I just saw he thread in "Related" on the right. – AdamL Jun 30 '13 at 10:22
30
+100

Yes:

=ArrayFormula(SUMIF(ROW(A1:A10);"<="&ROW(A1:A10);A1:A10))

Example of Cumulative Sum

The numbers in the green circle are the ones being summed, the ArrayFormula is populating all the others based on the cell it was pasted into, in this example I pasted in A11, B1, and C1 to show its effect.

This spreadsheet that I made a while ago attempts to explain how MMULT can be used for these sort of problems, and it also demonstrates the SUMIF solution on one of the sheets, which can (and probably should) be used for special cases like the one in this question.

  • I took this formula once (I think it was yours) on the Google Forum: SUMIF(ROW(A:A);"<"&ROW(A:A)), that didn't work. This one does. – Jacob Jan Tuinstra Jun 30 '13 at 11:56
  • The image in the answer was edited in by another member: +Xavier Casto – AdamL Jul 4 '13 at 0:10
  • 1
    @Fogest, I used a product called "Snagit" find out more about at techsmith.com/download/snagit – Xavier Casto Jul 5 '13 at 15:49
  • @XavierCasto Oh, I've used this a LONG time ago, didn't remember it having this, though it was a long time ago. Thanks for the reply though, I like products by techsmith. – ComputerLocus Jul 5 '13 at 15:50
31

Assuming you want:

  Row | A | B | B's formula
  --------------------------------
   1  | 1 | 1 | =1     (or =A1)
   2  | 2 | 3 | =1+2   (or =A2+B1)
   3  | 3 | 6 | =1+2+3 (or =A3+B2)

etc...

Then you can use:=sum($A$1:A1) where A1 is the start of numbers to sum. Put that formula in B1 (or where ever you want) and fill it down, and it will sum all cells up to and including the row you're on. Alternately, you can use the =Ax+Bx-1 (x = current row, data in A, cumulative sum in B) format from the "B's formula" section above.

  • 7
    This seems to work and is much simpler than the accepted answer – K Raphael Mar 15 '17 at 6:18
  • 1
    agreed, this is simpler and it works seamless! – loretoparisi May 29 '18 at 21:40
1

While the accepted answer is on spot it has a major disadvantage due to the fact that the last parameter of SUMIF needs to be always a "valid range" so when it comes to some more advanced running total calculation MMULT is the better option:

=ARRAYFORMULA(IF(LEN(A1:A), 
 MMULT(TRANSPOSE((ROW(A1:A)<=TRANSPOSE(ROW(A1:A)))*A1:A), SIGN(A1:A)^2), ))

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.