Going Stellar

Home/Google Sheets reference

Google Sheets function · Logical

LAMBDA in Google Sheets: syntax and examples

LAMBDA defines a function in the cell: you name the inputs and write the formula that uses them. On its own it does nothing until you call it with values. It is mostly used as the last argument of MAP, BYROW, REDUCE and similar helpers, or saved as a named function.

Syntax

LAMBDA(name, formula_expression)

Arguments

ArgumentTypeRequiredWhat it does
nameanyyesA name for an input value. You can list several names, one per input.
formula_expressionanyyesThe formula that uses the names. It is the last argument.

Examples

The examples use this table, starting in A1:

ItemQtyPrice
Pen102.5
Pad412
Ink230
Tape64
=LAMBDA(x, x*2)(5)

Result: 10

Define a function of x and call it with 5 right away.

Run in a Google Sheet (English US locale) on .

=LAMBDA(q, p, q*p)(B2, C2)

Result: 25

Two names, two values. Quantity times price for the first row.

Run in a Google Sheet (English US locale) on .

=MAP(B2:B5, C2:C5, LAMBDA(q, p, q*p))

Result:

25
48
60
24

MAP calls the LAMBDA once per row, which gives a line total for each.

Run in a Google Sheet (English US locale) on .

=BYROW(B2:C5, LAMBDA(r, SUM(r)))

Result:

12.5
16
32
10

BYROW passes each row to the LAMBDA as r. Here it adds the row.

Run in a Google Sheet (English US locale) on .

=REDUCE(0, B2:B5, LAMBDA(acc, v, acc+v))

Result: 22

A running total: acc starts at 0 and takes each value v in turn.

Run in a Google Sheet (English US locale) on .

Common errors

  • A LAMBDA that is never called returns #N/A. Add the call in brackets at the end, or pass it to MAP, BYROW or REDUCE.
  • Calling it with fewer values than names returns #N/A: LAMBDA(x, y, x+y)(1) fails.
  • Calling it with more values than names also returns #N/A.
  • A parameter name that looks like a cell, such as A1, returns #NAME?. Use names like x, qty or total.

Compared with Excel

Same idea and syntax as Excel 365 LAMBDA. Sheets saves reusable ones from Data, Named functions, Excel from Name Manager.

Questions and answers

Where do I save a LAMBDA to reuse it?

Create a named function from Data, Named functions. It gets a name and arguments, and you call it like any built-in function.

Sources

  1. LAMBDA (Docs Editors Help)

Markdown version · All functions