The Challenge of Array Formulas in Google Sheets
Traditionally in Google Sheets, if you wanted to apply a formula to an entire column of data, you would write the formula in the first cell and drag the fill handle down. While ARRAYFORMULA solved this by allowing a single formula to spill down a column, it has severe limitations—it cannot handle complex logic involving functions like AND, OR, or functions that inherently expect single values.
This is where the MAP function, combined with the LAMBDA function, becomes incredibly powerful. MAP allows you to take an array of data, pass each individual value into a custom LAMBDA function one by one, and output an array of results.
Understanding the Syntax
To use these functions together, you must understand their basic syntax:
=MAP(array1, [array2, ...], LAMBDA(name1, [name2, ...], formula_expression))
- array1: The range of data you want to process (e.g., A2:A10).
- LAMBDA: The custom function that will process the data.
- name1: A variable name you assign to represent the current cell being evaluated in the array (e.g., “cell” or “x”).
- formula_expression: The calculation performed on the variable.
Example 1: Processing a Single Array
Suppose you have a list of text strings in column A (A2:A100) that represent URLs, and you want to check if each URL contains “https” AND “digitash.com”. Using a standard ARRAYFORMULA with AND will fail because AND evaluates the entire array at once.
Instead, use MAP and LAMBDA:
=MAP(A2:A100, LAMBDA(url, AND(REGEXMATCH(url, "https"), REGEXMATCH(url, "digitash.com"))))
Here is what happens:
- MAP looks at the range A2:A100.
- It takes the value in A2 and assigns it to the variable url.
- The LAMBDA function evaluates the two REGEXMATCH conditions for that specific url.
- It returns TRUE or FALSE.
- MAP moves to A3, repeats the process, and spills the results down the column.
Example 2: Processing Multiple Arrays
The MAP function is not limited to a single column; it can map multiple arrays simultaneously. This is essential when a calculation requires data from multiple columns in the same row.
Imagine you have a list of “Cost” in column B (B2:B100) and “Quantity” in column C (C2:C100). You want to multiply them together, but only if the “Status” in column D is “Approved”.
=MAP(B2:B100, C2:C100, D2:D100, LAMBDA(cost, qty, status, IF(status="Approved", cost * qty, 0)))
In this formula:
- We feed three arrays into MAP (B, C, and D).
- The LAMBDA function defines three corresponding variables (cost, qty, and status).
- The formula evaluates the variables row by row, ensuring that the logic applies correctly to each individual line item before spilling the final array.
Why Use MAP and LAMBDA?
Using MAP and LAMBDA drastically reduces spreadsheet bloat. Instead of maintaining 5,000 individual formulas in a column—which slows down Google Sheets—you only maintain one dynamic master formula at the top of the column. When new data is added, the formula automatically processes it without requiring manual drag-and-drop adjustments.