Home > Formulas > Functions > PIVOTBY

Excel PIVOTBY Function Examples

Debra Dalgleish - Contextures

Use the Excel PIVOTBY function to quickly summarize data, in a layout that's similar to a pivot table. If the source data changes, the PIVOTBY results will update automatically - no need to refresh!

pivotby with 2 column fields and Max function

1) Excel PIVOTBY Function

Use the Microsoft Excel PIVOTBY function to quickly summarize data, in a layout that's similar to a pivot table.

pivotby with 1 row field and sum function

And if the source data changes, the PIVOTBY formula results get an automatic update! You don't need to refresh, like you have to do with a pivot table.

Video: PIVOTBY Function - 4 Examples

In this video, I show four quick examples for the PIVOTBY function, to create different summary reports, all from the same sales data.

There are written steps below the video, and you can download the PIVOTBY Function - Required Argument sample file, to follow along with the examples.

2) PIVOTBY Syntax

The PIVOTBY function has the following syntax for its arguments, with 4 required arguments, and 7 optional arguments:

  • =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])

That's a long, complicated list of function arguments!

Instead of trying to explain those arguments all at once, I've broken it into two main sections:

  1. In my first few examples below, I'll focus on the required arguments only.
  2. Further down the page, I'll show examples that include the optional arguments.

3) PIVOTBY Examples - Source Data

For all of the examples below, the source data is a table named Sales_Data.

The table contains sales orders, from a fictional food company. The first few rows of the FoodSales table are shown below.

You can download the PIVOTBY Function - Required Argument sample file, to get the data, and follow along with the examples.

food sales data from pivotby sample file

4) PIVOTBY - Required Arguments

In every PIVOTBY formula, you need to enter its 4 required arguments:

  1. row_fields - an array or range, with one or more columns, with the values to use for grouping rows
  2. col_fields - an array or range, with one column, with the values to use for grouping columns
  3. values - an array or range, with one or more columns, with the values to aggregate
  4. function - function(s) that defines how to aggregate the data, such as SUM or COUNT

In the examples below, I'll show four PIVOTBY formulas, using only these 4 required arguments.

  • 4.1) Req 01: Region, Year, Qty, SUM
  • 4.2) Req 02: Region & Cat, Year, Qty, COUNT
  • 4.3) Req 03: Region, Year, Qty & Orders, SUM
  • 4.4) Req 04: Category, Year & Region, Qty, MAX

4.1) Req 01: Region, Year, Qty, Sum

In this first example, the sales data is summarized by Region and Year, showing the Sum of quantities (Qty values) sold.

The formula in cell B4 has required arguments for Row, Column, Value, and Function:

  • =PIVOTBY( Sales_Data[[#All],[Region]],
    Sales_Data[[#All],[Year]],
    Sales_Data[[#All],[Qty]],
    SUM)

pivotby with 1 row field and sum function

Headers

In the formula above, I selected the table's heading row along with the data, so the reference includes [#All]

  • Sales_Data[[#All],[Region]]

If I select the data only, and omit the header, the reference look like this:

  • Sales_Data[Region]

Note: Be sure to make the same type of selection for all 3 fields

4.2) Req 02: Region & Cat, Year, Qty, Count

In this example, there are two row fields - Region and Category.

  • These fields are in adjacent columns in the Sales_Data table.
  • To use non-adjacent columns, you'd need a more complex formula

The formula in cell B4 shows the 2 row fields, Year in the column fields, Qty as the values argument, and the function is COUNT:

  • =PIVOTBY(Sales_Data[[#All],[Region]:[Category]],
    Sales_Data[[#All],[Year]],
    Sales_Data[[#All],[Qty]],
    COUNT
    )

pivotby with 2 row fields and count function

4.3) Req 03: Region, Year, Qty & Orders, Sum

In this example, there are two value fields - Qty and Orders.

  • These fields are in adjacent columns in the Sales_Data table.
  • To use non-adjacent columns, you'd need a more complex formula

The formula in cell B4 shows the 2 value fields, and the function is SUM:

  • =PIVOTBY(Sales_Data[[#All],[Region]],
    Sales_Data[[#All],[Year]],
    Sales_Data[[#All],[Qty]:[Orders]],
    SUM)

There are 2 columns for each year, with total Qty in the first column, and total Orders in the second column

pivotby with 2 row fields and count function

4.4) Req 04: Category, Year & Region, Qty, Max

In this example, there are two column fields - Year and Region.

  • These fields are in adjacent columns in the Sales_Data table.
  • To use non-adjacent columns, you'd need a more complex formula

The formula in cell B4 shows the 2 column fields, and the function is MAX:

  • =PIVOTBY(Sales_Data[[#All],[Category]],
    Sales_Data[[#All],[Year]:[Region]],
    Sales_Data[[#All],[Qty]],
    MAX
    )

There are 2 columns for each year, with East amounts in the first column, and West amounts in the second column

pivotby with 2 column fields and Max function

5) PIVOTBY - Optional Arguments

In addition to the 4 required arguments, the PIVOTBY function has 7 optional arguments. You can omit the optional arguments, or use one or more of the optional arguments to adjust the PIVOTBY results.

For example:

  • show or hide row and column headers
  • show or hide grand totals and subtotals
  • sort rows and columns in ascending or descending order
  • filter the source data based on criteria

In the video below, I show examples for 5 of the optional arguments, for headings, totals, and sorting.

Optional Argument Details & Examples

There are optional argument details, and more examples, in the sections below:

  • 5.1) Opt 01: Optional Arguments Testing File
  • 5.2) Opt 02: Field Headers
  • 5.3) Opt 03: Row/Column Total Depth
  • 5.4) Opt 04: Row/Column Sort Order
  • 5.5) Opt 05: Filter Array
  • 5.6) Opt 06: Relative To

5.1) Optional Arguments Testing File

To learn about a new Excel function, I like to experiment with it, and try all the argument settings.

For the PIVOTBY function, I created an interactive worksheet, where I could easily test the optional arguments.

  • There's a data validation drop-down list in each of the green cells.
  • Each list shows the valid options, like the sort options showing in cell G6
  • Change any setting, to see its effect on the PIVOTBY formula results

You can download the PIVOTBY Function - Optional Argument sample file, to get the data, and follow along with the examples.

pivotby with 2 column fields and Max function

5.1.1) Use Optional Arguments - Yes/No

There are 5 optional arguments listed on the worksheet, in cells F2:F6

There's also a setting to use the optional arguments Yes or No. Choose from the drop down list in cell C7.

  • Yes: Optional arguments are included, and use the settings selected in the green cells
  • No: Only the required arguments are used, with the default settings for the optional arguments.

5.2) Field Headers

Use the field_headers argument to control whether or not field headers appear in the PIVOTBY formula results

In the formula's required arguments, you select source data ranges for the row, column, and value fields. Those ranges can include Headers and Data, or Data only.

The field_headers argument is like a 2-part question

  1. Does the data range include headings?
  2. Do you want to show headings?

Here are the four settings:

  • 0 - No (this is the default setting - no headings in data, don't show)
  • 1 - Yes and don't show (headings in data, don't show them)
  • 2 - No but generate (this creates generic headings - not too useful!)
  • 3 - Yes and show (headings in data, show them)

In the sample file, headers are included with the data. For the screen shot below, I chose 3 - Yes and Show, so the Row, Column and Value headings are displayed in the formula results (I've highlighted them in yellow)

Here's the PIVOTBY formula, with 3 as the field_headers argument:

  • =PIVOTBY(
    Sales_Data[[#All],[Region]:[Category]],
    Sales_Data[[#All],[Year]],
    Sales_Data[[#All],[Qty]], SUM, 3)

pivotby field_headers setting 1 - Yes and Show

5.3) Row/Column Total Depth

There are two optional arguments to control the grand totals and subtotals:

  • row_total_depth
  • column_total_depth

To show subtotals, the row/column must have at least 2 fields.

Both arguments have the same five settings:

  • 0 No Totals (this is the default setting)
  • 1 Grand Totals
  • 2 Grand and Subtotals
  • -1 Grand Totals at Top
  • -2 Grand and Subtotals at Top

For the screen shot below, for row_total_depth, I chose -2 Grand and Subtotals at Top. (I've highlighted them in yellow)

Here's the PIVOTBY formula, with -2 as the row_total_depth argument, and 0 for the column_total_depth argument:

  • =PIVOTBY(
    Sales_Data[[#All],[Region]:[Category]],
    Sales_Data[[#All],[Year]],
    Sales_Data[[#All],[Qty]],SUM,, -2,, 0)

pivotby row_total_depth  setting -2 Grand and Subtotals at Top

5.4) Row/Column Sort Order

There are two optional arguments that control how the data is sorted:

  • row_sort_order
  • column_sort_order

For these arguments, enter the row/column number that you want to sort by.

  • Use a positive number for A-Z, and negative number for Z-A
  • For row_sort_order, the count is based on the row fields, then the value fields
  • For column_sort_order, the count is based on the column fields, then the value fields

In the screen shot below, for row_sort_order, I used 3

  • fields: 1) row1 - Region, 2) row2 - Category, 3) value1 - Qty
  • Quantities are sorted A-Z, based on the Region subtotals
    • Those subtotals are used, even if they're not showing

For column_sort_order, I used -2

  • fields: 1) col1 - Year, 2) value1 - Qty
  • Quantities are sorted Z-A, based on the Year grand totals
    • Those grand totals are used, even if they're not showing

Here's the PIVOTBY formula, with 3 as the row_sort_order argument, and -2 as the column_sort_order argument:

  • =PIVOTBY(
    Sales_Data[[#All],[Region]:[Category]],
    Sales_Data[[#All],[Year]],
    Sales_Data[[#All],[Qty]],SUM,
    1,2, 3,1, -2)

pivotby row and column sort order settings

5.5) Filter Array

Use the filter_array argument when you want to return data based on specific criteria.

There are 2 cells where you can select criteria, from drop-down lists:

  • C2 (MinYr): choose the minimum year for the sales data
  • C3 (ExCat): choose a category to exclude from the data

pivotby criteria for filter_array argument

In cell C6, the following formula creates a filter array, referring to those criteria cells:

  • =PIVOTBY(Sales_Data[[#All],[Region]:[Category]],
    Sales_Data[[#All],[Year]],
    Sales_Data[[#All],[Qty]], SUM,,,,,,
    VSTACK("OK",
    (Sales_Data[Year]>=MinYr) *
    (Sales_Data[Category]<>ExCat))
    )

5.5.1) How It Works

The filter_array argument needs an array of boolean values, the same size as the row, column and value ranges.

  1. First, the formula creates the array of boolean values (TRUE / FALSE).
    • It checks each row in the Year data, to see if it is greater than or equal to the number in the MinYr cell -- TRUE or FALSE
    • Next, it checks each row in the Category data, to see if it is NOT equal to the number in the ExCat cell -- TRUE or FALSE
    • Those two results are multiplied, using the * operator (asterisk)
    • TRUE = 1, and FALSE = 0, so if either criteria check is FALSE, the row's result is FALSE, and it won't pass through the filter
      • TRUE * TRUE = TRUE      (1 * 1 = 1)
      • TRUE * FALSE = FALSE   (1 * 0 = 0)
      • FALSE * FALSE = FALSE  (0 *0 = 0)
  2. Next, the array needs a heading, to be like the row, column and value ranges, so I used the text "OK"
  3. Finally, the VSTACK function vertically combines the "OK" heading, and the array of boolean values, to create the filter_array

5.6) Relative To

Use the relative_to argument if you use a function that requires two arguments.

Enter one of the following numbers, to control the function's second argument:

  • 0 - Column Totals (Default)
  • 1 - Row Totals
  • 2 - Grand Totals
  • 3 - Parent Column Total
  • 4 - Parent Row Total 

In the example shown below, the function is PERCENT OF.

pivotby criteria for relative_to argument

In cell C3, I selected Grand Totals from the drop-down list of options.

In cell C4 (RefTo), the value for that description is calculated, and that cell is referenced in the PIVOTBY formula (cell B7)

  • =PIVOTBY(Sales_Data[[#All],[Region]:[Category]],
    Sales_Data[[#All],[Year]],
    Sales_Data[[#All],[Qty]],
    PERCENTOF,,,,,,,RelTo)

Get the Excel Files ✅

  • PIVOTBY - Required Arg -- Click here to download the PIVOTBY Function - Required Argument sample file. It has the 4 Required Argument examples from this page. The zipped file is in xlsx format, and does not contain any macros.
  • PIVOTBY - Optional Interactive -- Click here to download the PIVOTBY Function - Optional Argument sample file. It has the interactive examples from this page, so you can test the optional argument settings. The zipped file is in xlsx format, and does not contain any macros.
  • PIVOTBY - Optional Arg 01 -- Click here to download the PIVOTBY Function - Optional Argument 01 sample file. It has examples for the field headers, row/column total depth, and row/column sort order arguments. The zipped file is in xlsx format, and does not contain any macros.

Where to Go Next

Combine Text/Numbers

Date Functions

Spill Function Examples

 

VLOOKUP

 

Last updated: June 10, 2025 11:56 AM