43

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?

user0
  • 27,169
  • 10
  • 55
  • 81
Jacob Jan
  • 23,322
  • 16
  • 106
  • 195

6 Answers6

39

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.

AdamL
  • 2,299
  • 17
  • 12
39

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.

Jacob Jan
  • 23,322
  • 16
  • 106
  • 195
Farfromunique
  • 511
  • 4
  • 4
6

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

user0
  • 27,169
  • 10
  • 55
  • 81
6

The accepted answers are far too complicated for running tallies. A much easier answer is:

=SUM(A$1:A1)

Substitute the A for the column you want. Substitute 1 for the row you want. Click the small box in the bottom right corner of the cell and drag it down to auto tally the cells you chose.

enter image description here

Note: I know when this was asked that my solution might not have been available but in 2020 the title 'Cumulative Sums without Scripts' makes this a leading google result for the query 'running totals in google sheets' and all the Google results had long complicated answers.

Altimus Prime
  • 161
  • 1
  • 5
2

Sumif

=INDEX(
    IF(
        A3:A = "", ""        ,SUMIF(
            ROW(
                A3:A
            ), "<=" &
            ROW(
                A3:A
            ) ,A3:A
        )
    )
)

Option suggested by Adam.

Drawbacks:

  • the last parameter of SUMIF needs to be always a "valid range"
  • not easy to add new cummulative sum by key, add complex conditions

Mmult

=INDEX(IF(A3:A="""","""" , 
  MMULT
  (--(TRANSPOSE(
      ROW(A3:A) <= TRANSPOSE(ROW(A3:A))))
   , -- A3:A)))

Option suggested by User0.

Drawbacks:

  • slows down your calculations

Manual formulas

  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)

Drawbacks:

  • will not refresh automatically, have to preserve space for the next entries.

Script

My try is to use a custom formula:

=runningTotal(A3:A)

Sample file

Source code

Code:

/**
 * Get running total for the array of numbers
 * by makhrov.max@gmail.com
 * 
 * @param {array} numbers The array of numbers
 * @param {number} total_types (1-dafault) sum, (2) avg, (3) min, (4) max, (5) count;
 *                  1-d array or number
 * @param {number} limit number of last values to count next time. 
 *                 Set to 0 (defualt) to take all values
 * @param {array} keys (optional) array of keys. Function will group result by keys
 * @return The hex-code of cell background & font color
 * @customfunction
 */
function runningTotal(numbers, total_types, limit, keys) {

// possible types to return var oTypes = { '1': 'sum', '2': 'avg', '3': 'min', '4': 'max', '5': 'count' } // checks and defaults var errPre = ' '; if( typeof numbers != "object" ) { numbers = [ [numbers] ]; } total_types = total_types || [1]; if( typeof total_types != "object" ) { total_types = [ total_types ]; } if( keys && typeof keys != "object" ) { keys = [ [keys] ]; } if (keys) { if (numbers.length !== keys.length) { throw errPre + 'Numbers(' + numbers.length + ') and keys(' + keys.length + ') are of different length'; } } // assign types var types = [], type, k; for (var i = 0; i < total_types.length; i++) { k = '' + total_types[i]; type = oTypes[k]; if (!type) { throw errPre + 'Unknown total_type = ' + k; } types.push(type); } limit = limit || 0; if (isNaN(limit)) { throw errPre + 'limit is not a Number!'; } limit = parseInt(limit);

// calculating running totals var result = [], subres = [], nodes = {}, key = '-', val; var defaultNode_ = { values: [], count: 0, sum: 0, max: null, min: null, avg: null, maxA: Number.MIN_VALUE, maxB: Number.MIN_VALUE, maxC: Number.MIN_VALUE, minA: Number.MAX_VALUE, minB: Number.MAX_VALUE, minC: Number.MAX_VALUE }; for (var i = 0; i < numbers.length; i++) { val = numbers[i][0]; // find correct node if (keys) { key = keys[i][0]; } node = nodes[key] || JSON.parse(JSON.stringify(defaultNode_)); /** * For findig running Max/Min * sourse of algorithm * https://www.geeksforgeeks.org * /sliding-window-maximum-maximum-of-all-subarrays-of-size-k/ */ // max //reset first second and third largest elements //in response to new incoming elements if (node.maxA<val) { node.maxC = node.maxB; node.maxB = node.maxA; node.maxA = val; } else if (node.maxB<val) { node.maxC = node.maxB; node.maxB = val; } else if (node.maxC<val) { node.maxC = val; } // min if (node.minA>val) { node.minC = node.minB; node.minB = node.minA; node.minA = val; } else if (node.minB>val) { node.minC = node.minB; node.minB = val; } else if (node.minC>val) { node.minC = val; }

// if limit exceeds
if (limit !== 0 &amp;&amp; node.count === limit) {
  //if the first biggest we earlier found
  //is matching from the element that
  //needs to be removed from the subarray
  if(node.values[0]==node.maxA) {
    //reset first biggest to second and second to third
    node.maxA = node.maxB;
    node.maxB = node.maxC;
    node.maxC = Number.MIN_VALUE;
    if (val &lt;= node.maxB) {
      node.maxC = val;
    }
  } else if (node.values[0]==node.maxB) {
    node.maxB = node.maxC;
    node.maxC = Number.MIN_VALUE;
    if (val &lt;= node.maxB) {
      node.maxC = val;
    }
  } else if (node.values[0]==node.maxC) {
    node.maxC = Number.MIN_VALUE;
    if (val &lt;= node.maxB) {
      node.maxC = val;
    }
  } else if(node.values[0]==node.minA) {
    //reset first smallest to second and second to third
    node.minA = node.minB;
    node.minB = node.minC;
    node.minC = Number.MAX_VALUE;
    if (val &gt; node.minB) {
      node.minC = val;
    }
  }
  if (node.values[0]==node.minB) {
    node.minB = node.minC;
    node.minC = Number.MAX_VALUE;
    if (val &gt; node.minB) {
      node.minC = val;
    }
  } 
  if (node.values[0]==node.minC) {
    node.minC = Number.MAX_VALUE;
    if (val &gt; node.minB) {
      node.minC = val;
    }
  }
  // sum
  node.sum -= node.values[0];
  // delete first value
  node.values.shift();
  // start new counter
  node.count = limit-1; 
}
// add new values
node.count++;
node.values.push(val);
node.sum += val;
node.avg = node.sum/node.count;
node.max = node.maxA;
node.min = node.minA;
// remember entered values for the next loop
nodes[key] = node;

// get the result depending on 
// selected total_types
subres = [];
for (var t = 0; t &lt; types.length; t++) {
  subres.push(node[types[t]]);
}
result.push(subres);

} // console.log(JSON.stringify(nodes, null, 4)); return result; }

More sample usages:

  • Running sum with group by column A: =runningTotal(B3:B,1,,A3:A)
  • Running max =runningTotal(A3:A,4)
Max Makhrov
  • 475
  • 8
  • 16
2

(2022 update: With respect to other solutions and their authors, the other solutions are now outdated, and should no longer be used due to simplicity or performance reasons, unless backwards-compatibility is required)

This is now the best way to do so.:

=SCAN(0, A3:A, LAMBDA(acc,x, acc+x))

This works without any 'fuss' (no copy-pasting formulas when you edit things; this "auto-expands" like an ARRAYFORMULA would). It is a single cell that always just works. Demonstration:

enter image description here

You can also crop the cumulative sum after a certain point: For example, FILTER is a function that works like ARRAYFORMULA that you can use to filter out the blank data (otherwise the cumulative sum will think "number+blank=number", which is actually a very reasonable thing to do).

You could have just specified A3:A9 in the example above to avoid having to filter out blanks.

Of course, maybe you want the cumulative sum to continue forever; that is also reasonable.

Furthermore this is performant; solutions using ARRAYFORMULA and SUMIF will slow down on large datasets because they do quadraticly-scaling amounts of unnecessary work.

ninjagecko
  • 443
  • 4
  • 10