LAMBDA · Dynamic arrays

Sort unique LAMBDA

xl.sort.unique spills the distinct values in ascending order. Austin, Dallas, Austin, Houston comes back as Austin, Dallas, Houston.

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

Syntax =xl.sort.unique(values)

xl.sort.unique
=LAMBDA(values,
  LET(
    flat, UNIQUE(TOCOL(values, 1)),
    SORT(flat)
  )
)

Arguments

NameRequiredWhat to pass
valuesRequiredA row or a column. Blanks are ignored. Text sorts alphabetically and numbers sort numerically, but a mixed list sorts as Excel sorts a mixed list.

Examples

Cities in first-seen order, returned A to Z

=xl.sort.unique(A1:A4)

Result Austin, Dallas, Houston

=SORT(UNIQUE(TOCOL(A1:A4,1)))B1
Cities
AB
1HoustonAustin
2AustinDallas
3DallasHouston
4Austin
A picture of the sheet, not a live calculator. B2 and B3 are the spill.

xl.sort.unique is the dropdown list. Houston, Austin, Dallas, Austin becomes Austin, Dallas, Houston. UNIQUE alone would have kept Houston first, because that is the first value it saw. SORT puts them in order afterward.

Blanks are removed. The spill is a column even if the input was a row.

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.sort.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.sort.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 ignores blanks and stacks everything. UNIQUE drops repeats, and it is case-sensitive. SORT uses ascending order, the default.

Limits

A mix of numbers and text sorts the numbers first in current Excel. If you need a custom order, SORTBY with a key column is the tool, not this name. The function does not trim spaces, so “Austin” and “Austin ” are different.