PIVOTBY builds a cross-tab: one field down the side, another across the top, and a calculation where they meet. It is the formula equivalent of dragging a field to Rows, another to Columns and a number to Values in a PivotTable, and the result updates when the data does.
The arguments
- row_fields is what goes down the left side. Each distinct value becomes a row.
- col_fields is what goes across the top. Each distinct value becomes a column.
- values is the number to summarise, and it must be the same height as the two fields.
- function is the calculation:
SUM, AVERAGE, COUNT, MAX, MIN or a LAMBDA. Name it without brackets.
=PIVOTBY(A2:A10,B2:B10,C2:C10,SUM) totals sales for each region and product combination. The regions run down the side in alphabetical order and the products across the top, with a total column on the right and a total row at the bottom. The cell in the top-left corner is empty.
The optional arguments
Most of the optional arguments come in pairs, one for the rows and one for the columns. The totals arguments say which totals to include: 0 removes them. The sort arguments choose the order. The default is ascending by label.
=PIVOTBY(A2:A10,B2:B10,C2:C10,SUM,0,0,,0) leaves out both sets of totals, and so returns the plain grid of region and product totals. The empty slot keeps the default row sort.
field_headers tells PIVOTBY whether the ranges include a header row. If you select the headers along with the data, set it to 3 to show them.
Compared with a PivotTable
A PivotTable is a separate object that you refresh, with a field list for layout. PIVOTBY is a formula: it recalculates with your data, sits next to other formulas, and spills into the sheet. A PivotTable still wins for exploring data interactively, with drag-and-drop fields and slicers. PIVOTBY is better for a report that has to rebuild itself without anyone pressing Refresh.
For a one-way summary, with only a field down the side, use GROUPBY.
Without PIVOTBY
In versions without it, build the grid from SUMIFS and the distinct lists. A column of criteria and a row of criteria together make a grid:
=SUMIFS(C2:C10,A2:A10,SORT(UNIQUE(A2:A10)),B2:B10,TOROW(SORT(UNIQUE(B2:B10))))
It leaves out the totals, which you add with extra formulas, but it works in any version that has dynamic arrays. The exercise on this page builds it step by step.
Notes
The result spills, so #SPILL! means the cells it needs aren't empty. PIVOTBY is a recent addition. If it returns #NAME?, your version does not have it. The examples show the results Excel returns.