FormulaCraft

How to create a running total with QUERY in Excel and Google Sheets

Topic:QUERY (Google Sheets SQL)

Heads up: Excel and Google Sheets do this differently.

Excel
=SUMIF(A$2:A2,A2,B$2:B2)
Google Sheets
=ARRAYFORMULA(MMULT((ROW(B2:B7)>=TRANSPOSE(ROW(B2:B7)))*1,B2:B7))

Verified example

Computed by a real spreadsheet engine on the sample data below.

DateAmount
2024-01-01500
2024-01-02300
2024-01-03750
2024-01-04200
2024-01-05620

=SUMIF(A$2:A2,A2,B$2:B2)500

Try it with your data

Edit the grid or formula, then run it through a real spreadsheet engine — no signup.

Sample data — click any cell to edit

Runs server-side · free · no signup

Step by step

  1. 1Sort your data by the column you want to accumulate over — use QUERY itself for this: paste =QUERY(A1:B10,"SELECT A, B ORDER BY A",1) into D1:E10 to get a date-ordered copy.
  2. 2In column F (next to the QUERY output), enter the running total formula for the first data row: =SUMIF(D$2:D2,D2,E$2:E2) — the expanding lock ($2:D2 grows as you drag down).
  3. 3Drag the running total formula down to cover all output rows.
  4. 4For a fully array-based approach in Sheets without dragging, use ARRAYFORMULA with MMULT: =ARRAYFORMULA(MMULT((ROW(E2:E7)>=TRANSPOSE(ROW(E2:E7)))*1,E2:E7)) to get cumulative sums in one formula.
  5. 5Label the result column 'Running Total' by typing the label directly in F1, since QUERY's LABEL clause only applies inside the QUERY string itself.

Tips

Need it for your exact data?

Describe your columns in plain English and get the precise formula for your sheet, with the right Excel or Sheets syntax.

Frequently asked

Can QUERY itself compute a cumulative sum?

No. The Google Visualization API Query Language does not support window functions. You need a helper formula (SUMIF or MMULT) alongside the QUERY output to produce a running total.

What if I want the running total to reset each month?

Add a helper column with the year-month (TEXT(A2,"YYYY-MM")), then use SUMIFS with both the date column and the year-month column as criteria, locking only the top anchor: =SUMIFS(C$2:C2,D$2:D2,D2) where D holds the month key.

Is there a running total approach that stays in one cell?

Yes — the MMULT approach with ARRAYFORMULA spills all running total values from a single cell. It is computation-heavy for large ranges but works well for ≤ 200 rows.

More on QUERY (Google Sheets SQL)

See all →

Formulas used

Written and reviewed by FormulaCraft Team. Each formula on this page is run through our verification engine before publishing.

Last reviewed: