Skip to content

PIVOTBY

Builds a two-way summary of values, with one field down the side and another across the top, from a single formula.

Syntax

=PIVOTBY(, , , , , , , , , , )

Dynamic arraysStatic examples

Excel for Microsoft 365 only, in recent builds. Not available in Excel 2024, Excel 2021 or earlier, where the formula returns #NAME?.

How PIVOTBY works

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.

Arguments

row_fields
The column to group down the side, such as A2:A10. Each distinct value becomes a row of the result.
col_fields
The column to group across the top, such as B2:B10. Each distinct value becomes a column of the result.
values
The numbers to summarise, such as C2:C10. It must have the same number of rows as the row and column fields.
function
The calculation applied to each combination. Give a function name without brackets, such as SUM, AVERAGE, COUNT, MAX or MIN, or a LAMBDA.
field_headersoptional
Whether the ranges include a header row. 0 no, 1 yes but do not show them, 2 no but generate them, 3 yes and show them. Leave it out and Excel decides from the data.
row_total_depthoptional
Totals for the row groups. 0 none, 1 a grand total, 2 a grand total and subtotals. A negative number puts them above the data. Left out, a grand total is added.
row_sort_orderoptional
How to sort the rows, as a column number with a minus sign for descending. The default is ascending by the row field.
col_total_depthoptional
Totals for the column groups, in the same way as row_total_depth. Left out, a grand total column is added.
col_sort_orderoptional
How to sort the columns, as a number with a minus sign for descending. The default is ascending by the column field.
filter_arrayoptional
A column of TRUE and FALSE values, one per row of data. Only rows with TRUE are included.
relative_tooptional
Used with percentage calculations, to choose what the percentage is measured against. Most pivots do not need it.

Examples

Example 1

Total sales by region and product

E2 =PIVOTBY(A2:A10,B2:B10,C2:C10,SUM)

Result: , Gloves, Tape, Total; East, 1530, 310, 1840; North, 1840, 980, 2820; South, 760, 450, 1210; West, 2100, 540, 2640; Total, 6230, 2280, 8510

Regions run down the side and products across the top, both in alphabetical order. The top-left cell is blank. North and Gloves is 1,200 + 640 = 1,840.

Shown as a static example: our practice sheet doesn't calculate this function yet.

Example 1: Total sales by region and product
RowABCDEFGH
1RegionProductSales
2NorthGloves1,200GlovesTapeTotal
3SouthTape450East15303101840
4NorthTape980North18409802820
5EastGloves1,530South7604501210
6SouthGloves760West21005402640
7NorthGloves640Total623022808510
8EastTape310
9WestGloves2,100
10WestTape540
11
12
13

Example 2

The same grid with no totals

E2 =PIVOTBY(A2:A10,B2:B10,C2:C10,SUM,0,0,,0)

Result: , Gloves, Tape; East, 1530, 310; North, 1840, 980; South, 760, 450; West, 2100, 540

The two 0s switch off the row totals and the column totals. The empty argument between them keeps the default row sort.

Shown as a static example: our practice sheet doesn't calculate this function yet.

Example 2: The same grid with no totals
RowABCDEFGH
1RegionProductSales
2NorthGloves1200GlovesTape
3SouthTape450East1530310
4NorthTape980North1840980
5EastGloves1530South760450
6SouthGloves760West2100540
7NorthGloves640
8EastTape310
9WestGloves2100
10WestTape540
11
12
13

Example 3

Count the orders instead of adding them up

E2 =PIVOTBY(A2:A10,B2:B10,C2:C10,COUNT,0,0,,0)

Result: , Gloves, Tape; East, 1, 1; North, 2, 1; South, 1, 1; West, 1, 1

The function argument decides the calculation. North sold gloves twice, so that cell is 2.

Shown as a static example: our practice sheet doesn't calculate this function yet.

Example 3: Count the orders instead of adding them up
RowABCDEFGH
1RegionProductSales
2NorthGloves1200GlovesTape
3SouthTape450East11
4NorthTape980North21
5EastGloves1530South11
6SouthGloves760West11
7NorthGloves640
8EastTape310
9WestGloves2100
10WestTape540
11
12
13

Example 4

Pull out one figure: the grand total

E2 =LET(p,PIVOTBY(A2:A10,B2:B10,C2:C10,SUM),INDEX(p,ROWS(p),COLUMNS(p)))

Result: 8510

The pivot is a normal array, so INDEX can read any cell of it. The last row and last column meet at the grand total.

Shown as a static example: our practice sheet doesn't calculate this function yet.

Example 4: Pull out one figure: the grand total
RowABCDEFGH
1RegionProductSales
2NorthGloves12008510
3SouthTape450
4NorthTape980
5EastGloves1530
6SouthGloves760
7NorthGloves640
8EastTape310
9WestGloves2100
10WestTape540
11
12
13

Your turn.

Solve it in the sheet. Every answer is checked, with hints if you need them.

PIVOTBY needs a recent Microsoft 365 build, so build its parts with functions that work everywhere. In E2, list each region once, in alphabetical order, down a column.

=SORT(UNIQUE(A2:A10))

In F1, list each product once, in alphabetical order, across a single row.

=TOROW(SORT(UNIQUE(B2:B10)))

In F2, return the total sales for every region and product combination, as a grid: regions down (A to Z) and products across (A to Z). Use SUMIFS with a list of criteria for each direction.

=SUMIFS(C2:C10,A2:A10,SORT(UNIQUE(A2:A10)),B2:B10,TOROW(SORT(UNIQUE(B2:B10))))

Common mistakes

  • The function is a name, not a call. Write SUM, not SUM(C2:C10).
  • #NAME? means your version of Excel does not have PIVOTBY. It is only in recent Microsoft 365 builds. Use SUMIFS with UNIQUE in workbooks that have to open everywhere.
  • The row field, column field and values must be the same height. A range that is a row short returns #VALUE!.
  • Include headers in all three ranges or none. Mixing them makes the sizes differ.
  • The result has no number formatting. Format the spill range yourself, for example with #,##0.
  • #SPILL! means something is in the cells the grid needs.

Questions people ask

What does PIVOTBY do in Excel?

It builds a pivot-style grid with a formula: one field down the side, another across the top, and a summary such as the sum where they cross.

What is the difference between PIVOTBY and GROUPBY?

GROUPBY has one grouping field and returns a list of groups. PIVOTBY has two, one down and one across, and returns a grid.

Is PIVOTBY better than a PivotTable?

It suits reports that must recalculate by themselves. A PivotTable is better for exploring, because you can drag fields and add slicers. PIVOTBY is a formula, so it never needs a refresh.

Why does PIVOTBY return #NAME??

Your version of Excel does not have it. PIVOTBY is only in recent Microsoft 365 builds. In Excel 2024 and earlier, use SUMIFS with UNIQUE, or a PivotTable.

How do I remove the totals from PIVOTBY?

Set the two total depths to 0: `=PIVOTBY(A2:A10,B2:B10,C2:C10,SUM,0,0,,0)` removes the total column and the total row.

Learn PIVOTBY in a real sheet.

Tracks take you from the basics to modelling, one graded exercise at a time.