Excel Lessons

PIVOTBY in Excel: a two-way summary in one formula

PIVOTBY summarises a table by rows and columns in one formula that updates by itself. How to build one, add totals, and when a PivotTable is still better.

Published . 6 min read.

By Ali, Founder, Excel Lessons

PIVOTBY builds a two-way summary from one formula. One field goes down the side, another goes across the top, and each cell holds a total. When the data changes, the summary changes too.

PIVOTBY is in Microsoft 365 only. Excel 2021 and Excel 2024 do not have it. There, the formula shows #NAME?.

A gym has two branches, Harbour and Park. The booking system exports one row per class session. The manager wants bookings by class and by branch.

Row A B C
1 Gym bookings
2 ClassBranchBookings
3 PilatesHarbour18
4 SpinHarbour24
5 PilatesPark12
6 YogaHarbour20
7 SpinPark30
8 YogaPark15
9 PilatesHarbour16
10 SpinPark22
11 YogaHarbour11

Build it

In E3, type =PIVOTBY(A3:A11,B3:B11,C3:C11,SUM). It fills E3 to H7:

HarbourParkTotal
Pilates341246
Spin245276
Yoga311546
Total8979168

The formula has 4 parts, in this order:

  1. A3:A11 goes down the side. Each class gets a row.
  2. B3:B11 goes across the top. Each branch gets a column.
  3. C3:C11 is the numbers to add.
  4. SUM is how to add them. Type the name only, with no brackets and no quote marks.

The top-left cell, E3, stays empty. It sits above the class names and left of the branch names, so it names neither.

Check it before you send it

Two quick checks catch most mistakes. First, the corner. H7 shows 168. =SUM(C3:C11) also gives 168, so no booking was lost.

Second, check one cell a different way. Spin at Park shows 52. =SUMIFS(C3:C11,A3:A11,"Spin",B3:B11,"Park") also gives 52. That is 30 plus 22, the two Spin sessions at Park.

Turn the totals off

Both kinds of total are on unless you turn them off. The 6th part controls the Total row. The 8th part controls the Total column. A 0 turns each one off.

=PIVOTBY(A3:A11,B3:B11,C3:C11,SUM,,0,,0) fills E3 to G6:

HarbourPark
Pilates3412
Spin2452
Yoga3115

Each pair of commas skips a part you do not need. Count them carefully. One comma too few, and the 0 lands in the wrong part.

To keep one kind, leave out its 0. =PIVOTBY(A3:A11,B3:B11,C3:C11,SUM,,0) has a Total column but no Total row.

Count instead of add

Change SUM to COUNT, and the table counts sessions instead of bookings.

=PIVOTBY(A3:A11,B3:B11,C3:C11,COUNT) fills E3 to H7:

HarbourParkTotal
Pilates213
Spin123
Yoga213
Total549

The corner is 9, one for each row of data. MAX and AVERAGE work the same way. With MAX, Spin at Park shows 30, its busiest session.

Turn it on its side

Swap the first two parts, and the branches go down the side instead.

=PIVOTBY(B3:B11,A3:A11,C3:C11,SUM) fills E3 to I6:

PilatesSpinYogaTotal
Harbour34243189
Park12521579
Total467646168

The numbers are the same. Pick the layout that fits the page: fewer columns is easier to read.

When a PivotTable is still better

PIVOTBYPivotTable
Updates when data changesYesAfter Refresh
Works in Excel 2021 or 2024NoYes
Try a new layout by draggingNoYes
Groups dates into monthsNoYes
Other formulas can read itYesHarder

Use a PivotTable when the file goes to somebody without Microsoft 365. Also use one to look through a new export. You can drag fields in and out and try a new layout in seconds.

A PivotTable is also better with dates. It can group a date column into months or years for you. PIVOTBY needs a month column first.

Use PIVOTBY for a report that must stay right when nobody clicks Refresh. For a table with only rows, see the GROUPBY article.

What goes wrong

Something is in the way. The table needs empty cells below and to the right. One value in that space turns the whole result into #SPILL!. A new class makes the table one row taller, so leave room.

A formula reads one cell by its position. =INDEX(PIVOTBY(A3:A11,B3:B11,C3:C11,SUM),3,3) gives 52 today. Add a new class or branch and the rows or columns can move. Then the same formula reads a different cell. Read one number by name with SUMIFS instead.

New rows are outside the range. A session typed in row 12 is not in A3:A11. Make the data an Excel table with Ctrl+T, and use its columns in the formula.

PIVOTBY has more optional parts, for sorting and filtering. You can build most reports without them.

The same table without PIVOTBY

If the file goes to Excel 2021, build the table with SUMIFS. Type the class names in E4 to E6. Type Harbour in F3 and Park in G3. Then, in F4, type:

=SUMIFS($C$3:$C$11,$A$3:$A$11,$E4,$B$3:$B$11,F$3)

Fill it right to G4, then down to row 6. F4 gives 34 and G5 gives 52, the same as PIVOTBY. $E4 always reads column E. F$3 always reads row 3. The SUMIFS exercises explain the argument order.

The cost is the list of names. A new class in the data does not appear in column E by itself. Check that the table adds up to the same total as =SUM(C3:C11).

You can practise PIVOTBY and GROUPBY in the lessons below. Each lesson is a real sheet you type into, and each answer is checked when you press Enter.

Related articles