LAMBDA · Dynamic arrays
Unpivot LAMBDA
xl.unpivot turns a crosstab into rows of row label, column label, and value. Austin and Dallas across 2024 and 2025 become four rows.
Syntax =xl.unpivot(row_headers, col_headers, data)
=LAMBDA(row_headers, col_headers, data,
LET(
r, ROWS(data),
c, COLUMNS(data),
i, SEQUENCE(r * c),
HSTACK(
INDEX(row_headers, INT((i - 1) / c) + 1),
INDEX(col_headers, MOD(i - 1, c) + 1),
INDEX(data, INT((i - 1) / c) + 1, MOD(i - 1, c) + 1)
)
)
)Arguments
| Name | Required | What to pass |
|---|---|---|
| row_headers | Required | A one-column list, one label per data row. |
| col_headers | Required | A one-row list, one label per data column. |
| data | Required | The numbers, without the headers. |
Examples
Two cities and two years
=xl.unpivot(A2:A3, B1:C1, B2:C3)Result Four rows: 2024 Austin 10, 2024 Dallas 20, 2025 Austin 12, 2025 Dallas 22.
| A | B | C | D | E | F | G | |
|---|---|---|---|---|---|---|---|
| 1 | Austin | Dallas | Year | City | Amount | ||
| 2 | 2024 | 10 | 20 | 2024 | Austin | 10 | |
| 3 | 2025 | 12 | 22 | 2024 | Dallas | 20 | |
| 4 | 2025 | Austin | 12 | ||||
| 5 | 2025 | Dallas | 22 |
xl.unpivot turns a crosstab into a list. Years run down the side. Cities run across the top. The four numbers become four rows, read across 2024 and then across 2025: 2024/Austin/10, 2024/Dallas/20, 2025/Austin/12, and 2025/Dallas/22.
SUMIFS and a chart want that list. A PivotTable wants the crosstab. This LAMBDA is the bridge from the second shape to the first.
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.unpivot, 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.unpivot(...).
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
SEQUENCE counts from 1 through rows times columns. Integer division by the column count picks the row. The remainder picks the column. INDEX reads the row label, the column label, and the value at that intersection. HSTACK spills the three columns.
For a 2 by 2 block the positions are (1, 1), (1, 2), (2, 1), and (2, 2). That is across, then down.
Limits
The headers are not inside the data. Include them and the first value will be a label. Blank cells in the data are still emitted. Drop them afterward if a blank should not be a row.