Function · Dynamic arrays

Excel PIVOTBY function

The Excel PIVOTBY function spills a two-way sales summary. March 2026 retail is $7,500, web is $2,500, and nine orders total $10,000.

Microsoft 365 and Excel for the web. Excel 2024, Excel 2021, and Excel 2019 do not have it.

Syntax =PIVOTBY(row_fields, col_fields, values, function, [field_headers], [row_total_depth], [row_sort_order], [col_total_depth], [col_sort_order], [filter_array], [relative_to])

PIVOTBY
=PIVOTBY(B2:B10,C2:C10,D2:D10,SUM,0,1,,1)

Arguments

NameRequiredWhat to pass
row_fieldsRequiredThe column or columns that label the rows. Two columns give a second row level, which is what row subtotals need.
col_fieldsRequiredThe column or columns that label the columns of the result. Same height as row_fields.
valuesRequiredThe numbers to aggregate. Same height as row_fields and col_fields.
functionRequiredSUM, AVERAGE, COUNTA, PERCENTOF, or a LAMBDA that returns one value for the intersection. Pass the name SUM, with no parentheses. HSTACK(SUM, AVERAGE) returns two blocks side by side.
field_headersOptional0 means the ranges have no headers. 1 means they do, and the result should hide them. 2 generates headers. 3 means the ranges include headers and the result should show them. Omit it and Excel looks at values. Text in the first cell and a number in the second is treated as a header.
row_total_depthOptionalOmit it for a grand total, plus subtotals when row_fields has two or more columns. 0 hides the total row. 1 shows it. 2 adds subtotals. -1 and -2 put those rows at the top. Subtotals with a single row field are
row_sort_orderOptionalWhich row-result column to sort the states by, starting at 1. A negative number sorts Z to A, or largest to smallest. Omit it and the row labels sort A to Z.
col_total_depthOptionalThe same scale as row_total_depth, for the total column. -1 and -2 put that column on the left. Subtotals need at least two col_fields columns.
col_sort_orderOptionalWhich column to sort the column labels by, counting the col_fields first and then the value columns. A negative number reverses them. Omit it and they sort A to Z, so Retail sits left of Web.
filter_arrayOptionalTRUE or FALSE for each source row, the same height as row_fields and col_fields.
relative_toOptionalWhere PERCENTOF, or a two-argument LAMBDA, finds its second input. 0, the default, is the column total. 1 is the row total. 2 is the grand total. 3 is the parent column total. 4 is the parent row total. A custom LAMBDA for this argument has the shape LAMBDA(subset, totalset, SUM(subset)/SUM(totalset)).

Examples

Sales by state and channel

=PIVOTBY(B2:B10,C2:C10,D2:D10,SUM,0,1,,1)

Result Retail $7,500, web $2,500, grand total $10,000

Each cell as a share of the $10,000

=PIVOTBY(B2:B10,C2:C10,D2:D10,PERCENTOF,0,1,,1,,,2)

Result California retail 28% and web 10%. Ohio 15% and 5%. Texas 32% and 10%. The total row is 75% and 25%.

How many orders sit in each cell

=PIVOTBY(B2:B10,C2:C10,D2:D10,COUNTA,0,1,,1)

Result California 2 and 1, Ohio 1 and 2, Texas 2 and 1. Retail totals 5, web totals 4, and the grand total is 9.

Orders of at least $1,000 in each cell

=PIVOTBY(B2:B10,C2:C10,D2:D10,LAMBDA(orders,SUM(1*(orders>=1000))),0,0,,0)

Result California retail 2 and web 1. Ohio retail 1 and web 0. Texas retail 1 and web 1.

=PIVOTBY(B2:B10,C2:C10,D2:D10,SUM,0,1,,1)F2
Orders
ABCDEFGHI
1DateStateChannelAmount
23/2/2026TexasRetail2500RetailWebTotal
33/4/2026TexasRetail700California280010003800
43/6/2026CaliforniaRetail1800Ohio15005002000
53/9/2026CaliforniaRetail1000Texas320010004200
63/11/2026OhioRetail1500Total7500250010000
73/14/2026CaliforniaWeb1000
83/18/2026OhioWeb200
93/18/2026OhioWeb300
103/21/2026TexasWeb1000
A picture of the sheet, not a live calculator. F2 holds the formula and displays blank. Retail, Web, and Total in G2:I2 are the spill, then the three states and the total row. These are the same March 2026 orders as GROUPBY.

PIVOTBY spills a grid from the same nine March 2026 orders as GROUPBY. States run down, Retail and Web run across, and both axes get a total. Retail is $7,500, web is $2,500, and the grand total is $10,000. The column labels sort A to Z, so Retail sits left of Web. PERCENTOF with relative_to 2 divides each cell by that $10,000, and California retail is 28%. relative_to 0 divides by the column total. relative_to 1 divides by the state total, so Ohio retail is 75% of Ohio’s $2,000.

Excel 2024, Excel 2021, and Excel 2019 return #NAME?. A PivotTable is the fallback, and GETPIVOTDATA can read one cell of it. Excel 2021 and Excel 2024 can still build the one-way list with UNIQUE and SUMIFS. The Sales Pipeline Tracker totals deals by stage with SUMIFS, the Invoice and Payment Tracker totals the open balance with SUMIFS, and the Sales Commission Calculator totals each rep. All three open in Excel 2010.