LAMBDA · Statistical

Count unique LAMBDA

xl.count.unique counts distinct values and ignores blanks. Austin, Dallas, Austin, Houston counts as 3.

Microsoft 365, Excel 2024, and Excel for the web. Excel 2021 does not have LAMBDA.

Syntax =xl.count.unique(values)

xl.count.unique
=LAMBDA(values,
  LET(
    flat, UNIQUE(TOCOL(values, 1)),
    ROWS(flat)
  )
)

Arguments

NameRequiredWhat to pass
valuesRequiredA row or a column. Blanks are ignored. Text matching is not case-sensitive inside UNIQUE for this use only if the values differ only by case. UNIQUE is case-sensitive.

Examples

Four cities, one repeated

=xl.count.unique(A1:A4)

Result 3

=ROWS(UNIQUE(TOCOL(A1:A4,1)))B5
Cities
AB
1Austin
2Dallas
3Austin
4Houston
5Distinct3
A picture of the sheet, not a live calculator. UNIQUE is case-sensitive. austin and Austin are two values.

xl.count.unique counts Austin once. Austin, Dallas, Austin, Houston is 3. Blanks are dropped before the count, so an empty cell at the bottom of a column does not become its own category.

UNIQUE is case-sensitive. austin and Austin are two values. COUNTIF would have treated them as one. This function does not.

Add it to the workbook

Copy the formula in the block above, including the line breaks. Choose Formulas, Name Manager, New. Set the name to xl.count.unique, the scope to Workbook, and paste into Refers to. Excel Labs can do the same step: open the Advanced Formula Environment, add that name, and save it back to the workbook. A cell then calls =xl.count.unique(...).

The name needs Microsoft 365, Excel 2024, or Excel for the web. Excel 2021 returns #NAME? for LAMBDA even when it already has XLOOKUP, FILTER, or LET.

How it works

TOCOL with ignore flag 1 stacks the range and drops blanks. UNIQUE keeps the first of each remaining value. ROWS counts that list.

Limits

Errors are not ignored. A #N/A in the list makes the count fail. Numbers and text are both counted. 10 and the text “10” are different.