HomeSCAN Function in Google Sheets – LAMBDA Helper FunctionFormulas & FunctionsSCAN Function in Google Sheets – LAMBDA Helper Function

SCAN Function in Google Sheets – LAMBDA Helper Function

The SCAN function in Google Sheets scans an array and applies a LAMBDA function to each value, moving row by row. The output is an array of intermediate values obtained at each step.

SCAN function tutorials offer the ultimate spreadsheet savior when you want to scan an array, apply a custom lambda function to each value row by row, and output an array of intermediate accumulator values. Whether you are calculating cumulative running totals or resetting annual balances, mastering this advanced lambda helper brings programming-level iteration to your spreadsheets.

Advanced lambda formulas packed with high-efficiency syntax, accumulator rules, and entirely free of manual running-total columns elevate your data management while keeping your workflows streamlined.

It’s hard to understand without seeing an example, so let’s do that.

The most straightforward SCAN formula is to create a running total calculation on a column of numbers.

The formula is:

=SCAN(0,A2:A11,LAMBDA(runningTotal,currentValue,runningTotal + currentValue))

Which produces this array output:

Scan Function Running Total Calculation

The lambda simply adds together the current running total and the new value, then returns the answer to the output array.

When all of the values from the input array have been considered, the output array is returned.

SCAN Function Syntax

=SCAN(initial_value, array_or_range, lambda)

It takes 3 arguments:

initial_value

This is the starting value of the accumulator.

array_or_range

This is the input array or range of data to be reduced.

lambda

The custom LAMBDA function that will be applied to each value in the input range. The LAMBDA function must have 2 name arguments and 1 function expression.

The first name will evaluate to the current result in the accumulator. The second name resolves to the current value of the input array.

SCAN Function Notes

  • The output array is the same size as the input array.
  • The SCAN function is a spill formula, i.e. it requires enough space for the array output to fully expand.

SCAN Function Example: Annual Running Total

Suppose we have monthly revenue data for our business for a number of years and we want to calculate a running total for each year. We want the running total to reset for each year.

We can do that with this SCAN formula:

=SCAN(0,A2:A25,LAMBDA(total,rowVal,IF(MONTH(rowVal)=1,OFFSET(rowVal,0,1),total + OFFSET(rowVal,0,1))))

The output of this formula is shown in column C:

Scan Function In Google Sheets

rowVal represents the current value from the input array, i.e. a date.

IF(MONTH(rowVal)=1

If it is a January month, we want to reset the running total, and start a new one with the January value, which we do as follows:

IF(MONTH(rowVal)=1,OFFSET(rowVal,0,1)

The OFFSET(rowVal,0,1) just grabs the value from column B on the same row, i.e. the revenue value.

Now, if the month is not January, then we want to add the new month value to the existing running total, which we do in the FALSE part of the IF statement:

IF(MONTH(rowVal)=1,OFFSET(rowVal,0,1),total + OFFSET(rowVal,0,1))

This is the formula expression for our LAMBDA:

LAMBDA(total,rowVal,IF(MONTH(rowVal)=1,OFFSET(rowVal,0,1),total + OFFSET(rowVal,0,1)))

Finally, we can plug this into the SCAN function. We set the initial value to 0 and select the date column as our input array:

=SCAN(0,A2:A25,LAMBDA(total,rowVal,IF(MONTH(rowVal)=1,OFFSET(rowVal,0,1),total + OFFSET(rowVal,0,1))))

Also Read : How to use the INDIRECT function in Google Sheets

Leave a Reply

Your email address will not be published. Required fields are marked *