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.
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(B2:B10,C2:C10,D2:D10,SUM,0,1,,1)Arguments
| Name | Required | What to pass |
|---|---|---|
| row_fields | Required | The column or columns that label the rows. Two columns give a second row level, which is what row subtotals need. |
| col_fields | Required | The column or columns that label the columns of the result. Same height as row_fields. |
| values | Required | The numbers to aggregate. Same height as row_fields and col_fields. |
| function | Required | SUM, 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_headers | Optional | 0 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_depth | Optional | Omit 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_order | Optional | Which 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_depth | Optional | The 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_order | Optional | Which 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_array | Optional | TRUE or FALSE for each source row, the same height as row_fields and col_fields. |
| relative_to | Optional | Where 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.
| A | B | C | D | E | F | G | H | I | |
|---|---|---|---|---|---|---|---|---|---|
| 1 | Date | State | Channel | Amount | |||||
| 2 | 3/2/2026 | Texas | Retail | 2500 | Retail | Web | Total | ||
| 3 | 3/4/2026 | Texas | Retail | 700 | California | 2800 | 1000 | 3800 | |
| 4 | 3/6/2026 | California | Retail | 1800 | Ohio | 1500 | 500 | 2000 | |
| 5 | 3/9/2026 | California | Retail | 1000 | Texas | 3200 | 1000 | 4200 | |
| 6 | 3/11/2026 | Ohio | Retail | 1500 | Total | 7500 | 2500 | 10000 | |
| 7 | 3/14/2026 | California | Web | 1000 | |||||
| 8 | 3/18/2026 | Ohio | Web | 200 | |||||
| 9 | 3/18/2026 | Ohio | Web | 300 | |||||
| 10 | 3/21/2026 | Texas | Web | 1000 |
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.