Skip to main content

How to Reset Running Totals on Multiple Conditions in Google Sheets (SCAN + LAMBDA) (Step-by-Step)

How to Calculate Running Totals with Multiple Reset Conditions Using SCAN and LAMBDA in Google Sheets

When a running total needs to reset based on more than one condition, a normal cumulative SUM formula quickly becomes difficult to manage. I use SCAN and LAMBDA for this because they let you process each row in sequence and decide exactly when the total should restart.

30-SECOND SUMMARY

The Quick Answer

Suppose your data has:

  • Column A: Employee
  • Column B: Month
  • Column C: Sales amount

You want a running total that resets whenever either the employee or month changes. A basic SCAN pattern looks like this:

=SCAN(0,A2:C10,LAMBDA(acc,row, IF( INDEX(row,1,1)&INDEX(row,1,2)<>previous_condition, INDEX(row,1,3), acc+INDEX(row,1,3) ) ))

In real spreadsheet work, the most reliable approach is to create a reset flag by comparing the current row with the previous row, then use that flag inside SCAN.

For example, if your reset condition is stored in column D as TRUE or FALSE:

=SCAN(0,C2:C10,LAMBDA(total,value, IF(INDEX(D2:D10,ROW(value)-ROW(C2)+1), value, total+value )))

However, because SCAN processes values sequentially, a cleaner practical solution is often to pass the amount and reset flag together as an array:

=SCAN(0,HSTACK(C2:C10,D2:D10), LAMBDA(total,row, IF(INDEX(row,1,2), INDEX(row,1,1), total+INDEX(row,1,1) )))

Important: Your data must be sorted in the order you want the running total calculated.

What Are We Trying to Solve?

Imagine you maintain a sales sheet for employees. You want to calculate a cumulative sales total, but the total must restart when:

  • The employee changes, or
  • The month changes.

Here is a realistic example:

Employee Month Sales Reset? Running Total
XYZJanuary120Yes120
XYZJanuary80No200
XYZJanuary150No350
XYZFebruary100Yes100
XYZFebruary90No190
ABCFebruary200Yes200
ABCFebruary50No250

The reset happens on row 1 because there is no previous record. It happens again when January becomes February, and again when XYZ becomes ABC.

The Anatomy of SCAN and LAMBDA

Before building the final formula, let me break down the two functions.

SCAN

=SCAN(initial_value,array,LAMBDA(accumulator,current_value,calculation))
Part What It Does
initial_valueThe starting value for the calculation, such as 0.
arrayThe data SCAN processes one item or row at a time.
LAMBDAYour custom calculation rule.
accumulatorThe running result from the previous step.
current_valueThe current item being processed.

A very simple running total is:

=SCAN(0,C2:C10,LAMBDA(total,value,total+value))

If column C contains 120, 80, and 150, the result is:

120 → 200 → 350

That is useful, but it never resets. So we need to add reset logic.

Why Use LAMBDA?

LAMBDA lets you name the values used during each SCAN step. In this part:

LAMBDA(total,row, ... )
  • total is the accumulated result so far.
  • row represents the current row data.

You can then write a normal IF rule:

=IF(reset_condition,current_amount,total+current_amount)

That is the main idea behind a running total with resets.

Step-by-Step Practical Walkthrough

Let's build this using a sample sheet. Assume your columns are arranged like this:

A B C D E
EmployeeMonthSalesReset FlagRunning Total
XYZJanuary120

XYZJanuary80

XYZJanuary150

XYZFebruary100

XYZFebruary90

ABCFebruary200

ABCFebruary50

Step 1: Create a Reset Flag

First, we need to identify when the current row belongs to a new group.

In D2, enter:

=TRUE

The first data row should always reset because there is no previous row.

Then, in D3, use:

=OR(A3<>A2,B3<>B2)

Copy the formula downward.

This means:

  • If the employee changed, return TRUE.
  • If the month changed, return TRUE.
  • If neither changed, return FALSE.
Pro Tip: The operator <> means "not equal to." It is one of the simplest ways to detect a change between the current and previous row.

Step 2: Use SCAN to Build the Running Total

Now that column D tells us exactly when to reset, the SCAN formula becomes much easier to understand.

In E2, use:

=SCAN(0,HSTACK(C2:C8,D2:D8), LAMBDA(total,row, IF( INDEX(row,1,2)=TRUE, INDEX(row,1,1), total+INDEX(row,1,1) ) ))

Let's break down what happens on each row:

Current Row Reset Flag Calculation Result
120TRUEStart with 120120
80FALSE120 + 80200
150FALSE200 + 150350
100TRUEReset to 100100
90FALSE100 + 90190
200TRUEReset to 200200

Step 3: Understand the HSTACK Part

This part often confuses beginners:

HSTACK(C2:C8,D2:D8)

It temporarily combines your Sales column and Reset Flag column into one two-column array.

Each SCAN step receives a row containing:

Current Amount | Reset Flag

For example:

120 | TRUE 80 | FALSE 150 | FALSE

Inside the LAMBDA:

  • INDEX(row,1,1) gets the sales amount.
  • INDEX(row,1,2) gets the reset flag.

A More Compact Approach Using Combined Conditions

You can also create a helper group key. This is useful when your reset depends on several columns.

For example, combine Employee and Month in column D:

=A2&"|"&B2

This creates values such as:

XYZ|January XYZ|January XYZ|February ABC|February

Then your reset formula can compare the current key with the previous key:

=D3<>D2
Pro Tip: Using a separator such as | helps avoid accidental combinations. For example, combining text without a separator can sometimes create ambiguous values.

What If You Have Three or More Reset Conditions?

The same method works. Suppose the running total should reset when any of these changes:

  • Employee
  • Month
  • Project

Your reset flag can be:

=OR(A3<>A2,B3<>B2,C3<>C2)

Or, if you have many conditions, create a combined key:

=A2&"|"&B2&"|"&C2

Then compare each key with the previous one.

This is one of the reasons I prefer the helper-column approach for beginners. The SCAN formula stays simple even when the reset logic becomes more complex.

Excel vs. Google Sheets

The core idea is similar in both spreadsheet applications, but function availability and implementation can differ depending on your version.

Feature Google Sheets Excel
SCANAvailable in supported modern versionsAvailable in modern versions with dynamic array functions
LAMBDAUsed directly inside array formulasAlso supports named custom LAMBDA functions
Dynamic arraysResults can spill automaticallyResults can spill automatically
HSTACKUseful for combining arrays horizontallyAvailable in modern versions
Older spreadsheet versionsMay require traditional formulasMay require helper columns or older techniques

The biggest practical difference is often the spreadsheet version you are using. If SCAN or LAMBDA is not recognized, check whether your version supports those functions.

3 Common Mistakes and How to Fix Them

Mistake 1: Your Data Is Not Sorted

Problem: The employee or month appears in different places throughout the sheet, so the formula resets unexpectedly.

For example:

EmployeeMonth
XYZJanuary
ABCJanuary
XYZJanuary

The formula sees the second XYZ record as a new group because ABC appeared in between.

Fix: Sort the data by the columns that define your groups before calculating the running total.

Mistake 2: Hidden Extra Spaces Cause Unexpected Resets

Problem: One row contains XYZ, while another contains XYZ with an extra space.

They look identical, but the spreadsheet treats them as different text values.

Fix: Clean the comparison values with TRIM:

=OR(TRIM(A3)<>TRIM(A2),TRIM(B3)<>TRIM(B2))

If your source data is imported or copied from another system, this small cleanup step can save a lot of debugging time.

Mistake 3: Range Sizes Do Not Match

Problem: Your HSTACK formula combines ranges with different numbers of rows.

For example:

=HSTACK(C2:C10,D2:D9)

These ranges have different sizes, which can produce an error or unexpected results.

Fix: Always make sure every range inside HSTACK starts and ends on the same rows.

=HSTACK(C2:C10,D2:D10)

Another Beginner Mistake: Using a Circular Reference

A circular reference happens when your running-total formula directly or indirectly refers to its own output range.

For example, trying to make the formula in column E depend on the previous calculated value in column E can become difficult when you also want one spilled array formula.

Why SCAN helps: SCAN maintains the previous accumulated value internally through the total parameter. You do not need to manually reference the previous output cell.

Pro Tip: If you find yourself writing a formula that refers to the cell immediately above, stop and ask whether SCAN can manage that running state for you.

How to Make the Formula Dynamic for New Rows

In a real sales sheet, new records are added regularly. Instead of manually changing:

C2:C8

You may want to use an open-ended range:

C2:C

However, open-ended ranges can include blank rows. If your formula starts processing blanks as zeros, the output may extend farther than expected.

A cleaner approach is to filter blank rows first:

=FILTER(A2:C,A2:A<>"")

You can then build your SCAN logic around the filtered data.

Common Pitfall: If one of your grouping columns contains blanks in the middle of the data, do not automatically assume that the row should be ignored. Decide whether a blank is valid data or an incomplete record before filtering.

Formula Pattern You Can Reuse

For future spreadsheets, remember this pattern:

=SCAN( starting_value, HSTACK(amount_range,reset_flag_range), LAMBDA(accumulator,current_row, IF( reset_condition, current_amount, accumulator+current_amount ) ) )

Translated into actual logic:

=SCAN(0,HSTACK(amount_range,reset_flag_range), LAMBDA(total,row, IF( INDEX(row,1,2)=TRUE, INDEX(row,1,1), total+INDEX(row,1,1) )))

You only need to change:

  • The amount range
  • The reset flag range
  • The reset conditions

Downloadable Example / Actionable Takeaway

COPY THIS WORKFLOW

Your 5-Step Setup

  1. Create columns for the conditions that define each group.
  2. Sort your data in the correct calculation order.
  3. Create a Reset Flag by comparing the current row with the previous row.
  4. Combine the amount and reset flag with HSTACK.
  5. Use SCAN and LAMBDA to either restart or add to the previous total.

Starter formulas:

Reset flag:

=OR(A3<>A2,B3<>B2)

Running total:

=SCAN(0,HSTACK(C2:C,D2:D), LAMBDA(total,row, IF( INDEX(row,1,2)=TRUE, INDEX(row,1,1), total+INDEX(row,1,1) )))
Keyboard Shortcut Tip: When editing a long formula, use your spreadsheet's formula bar to review line breaks and parentheses. I also recommend testing the formula on 5–10 rows first before applying it to a large dataset.

When Should You Use SCAN Instead of a Normal Running SUM?

Use a normal running total when every row belongs to one continuous sequence. Use SCAN when the result depends on the previous calculated state and you need logic such as:

  • Reset when a category changes
  • Reset when a new employee appears
  • Reset when a month changes
  • Reset when a project changes
  • Reset when any of several conditions changes
  • Apply different logic based on the previous accumulated value

Once you understand the accumulator concept, SCAN becomes much easier to use. The formula is essentially reading your data from top to bottom and carrying the previous result forward.

FAQ: Running Totals with SCAN and LAMBDA

1. Can SCAN reset a running total based on multiple columns?

Yes. Create a reset condition that checks whether any relevant column has changed compared with the previous row. For example, use OR to check Employee and Month, then pass that reset flag into SCAN.

2. Why does my running total reset in the wrong place?

The most common reasons are unsorted data, hidden spaces, inconsistent text values, or an incorrect reset condition. Check your reset flag first. If the flag is correct, the SCAN result should usually be correct as well.

3. Do I need a helper column for the reset condition?

No, but I recommend one for beginners. A helper column makes it easy to see exactly where the formula thinks a new group starts. After testing, you can build the reset logic into a more compact formula if needed.

4. Can I reset the running total when three or more conditions change?

Yes. Add more comparisons inside OR, or create a combined group key from all columns that define a group. The SCAN formula itself does not need to change much.

Final Takeaway

The hardest part of a running total with multiple reset conditions is usually not SCAN itself. It is defining exactly when a new group starts.

My practical recommendation is simple: first build and verify a reset flag, then use SCAN to carry the running total forward. This approach is easier to test, easier to debug, and much easier to adapt when you later add another reset condition.

Once you are comfortable with SCAN and LAMBDA, you can use the same pattern for cumulative sales, student marks, project hours, task counts, inventory movement, and many other row-by-row calculations.

Comments

Popular posts from this blog

Remove Duplicates in Google Sheets: The Complete Data Cleaning Blueprint

Executive Summary Duplicate records corrupt ledger reconciliations, inflate pipeline projections, and skew reporting dashboards across production spreadsheets. This guide covers four enterprise-grade deduplication techniques in Google Sheets—contrasting destructive native removal with non-destructive dynamic formulas—so your source records stay clean without downstream audit errors.   Remove Duplicates in Google Sheets The Real-World Business Scenario Duplicate data silently degrades your reporting accuracy. Suppose you run monthly sales settlements for ABC Logistics . Raw transaction reports exported from external order portals frequently record duplicate webhook events, retry attempts from payment gateways, or duplicate data entry inputs from branch staff. When you aggregate gross transaction volume using SUM(D2:D) or track completed shipments with COUNTA(A2:A) , repeated IDs double-coun...

Master XLOOKUP and Dynamic Arrays: Fix Broken Lookups, Multi-Criteria Matches, and #SPILL! Errors in Excel & Google Sheets

Executive Summary Legacy lookup functions like VLOOKUP and unanchored INDEX/MATCH chains break silently whenever columns shift, return false positives on duplicate keys, and drag down workbook calculation speed. This architecture guide provides drop-in formulas for multi-criteria lookups, 2-way matrix extractions, and dynamic array calculations using XLOOKUP, FILTER, and modern spill engines in Microsoft Excel and Google Sheets.   Master XLOOKUP and Dynamic Arrays Hardcoded index offsets and brittle lookup ranges cost corporate finance and operations teams hundreds of lost hours every quarter. When a junior analyst inserts a reconciliation column into a master dataset, static formulas return wrong row indexes, pollute balance sheets with #REF! flags, or mask silent computational errors that escape standard workbook audits. Modern spreadsheet engines operate on dynamic calculation topologie...

Power Query ETL Tutorial: Automate Excel & Google Sheets

Automation & Data Engineering Power Query for Automated ETL: Stop Cleaning Data Manually in Excel & Google Sheets Learn how to build reusable, one-click data cleaning pipelines that extract messy source files, transform structured tables, and load analysis-ready data effortlessly. In This Masterclass: 1. What is ETL? (And Why Doing It Manually Wastes 80% of Your Time) 2. Power Query Architecture: How the Mashup Engine Works 3. Step-by-Step: The Three Pillars of Power Query (E-T-L) 4. Essential Transformations: Unpivoting, Appending, & Merging 5. Introduction to M-Code: Under the Hood of Power Query 6. Building an Automated ETL Workflow in Google Sheets 7. End-to-End Walkthrough: Consolidating Multi-Branch CSVs 8. Top 6 Power Query Mistakes & Fixes 9. Frequently Asked Questions (FAQs) 1. What is ETL? (And Why Doing It Manually Wastes 80% of Your Ti...