Summarize data by grouping rows and columns
=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])
| Parameter | Description |
|---|---|
row_fields |
The values to use when grouping rows. |
col_fields |
The values to use when grouping columns. |
values |
The values to aggregate. |
function |
The calculation to run when aggregating. |
field_headers |
[optional] 0 = No, 1 = Yes, don't show, 2 = No, generate, 3 = Yes, show. |
row_total_depth |
[optional] Enable or disable row totals. 0 = None, 1 = Grand Totals, 2 = Grand and Subtotals, -1 = Grand Totals at top, -2 = Both at top. |
row_sort_order |
[optional] A column number indicating how rows should be sorted. Negative numbers = descending order. |
col_total_depth |
[optional] Enable or disable column totals. 0 = None, 1 = Grand Totals, 2 = Grand and Subtotals, -1 = Grand Totals at top, -2 = Both at top. |
col_sort_order |
[optional] A column number indicating how columns should be sorted. Negative numbers = descending order. |
filter_array |
[optional] Logic to exclude specific rows. |
relative_to |
[optional] Where to find values for the 2nd argument of a function. 0 = column totals (default), 1 = Row Totals, 2 = Grand Totals, 3 = Parent Col Total, 4 = Parent Row Total. |
The PIVOTBY function takes 11 arguments, but only the first four are required. In the worksheet below, we use the PIVOTBY function to summarize sales
=PIVOTBY(B5:B28,C5:C28,E5:E28,SUM)
Compared to the GROUPBY function (which groups only by row) the main benefit of PIVOTBY is the ability to group by row and by column. This behavior is
=PIVOTBY(B4:B28,C4:C28,E4:E28,SUM)
In the second table, we swap rows and columns. Now we group rows by Year and columns by City:
=PIVOTBY(C4:C28,B4:B28,E4:E28,SUM)
The field_header argument is optional. When field_header is omitted, PIVOTBY will try to detect headers in the source data automatically by testing va
PIVOTBY(B4:B28,,E4:E28,SUM,3) // display field headers
In the worksheet below, we have a list of meal preferences for 100 employees in different departments and the cost per meal. We can use the PIVOTBY fu
=PIVOTBY(D4:D104,,D4:D104,COUNTA,1)
Next, let's change the calculation to generate a total cost by meal, by using the SUM function instead of COUNTA. In the worksheet below, the formula
=PIVOTBY(D4:D104,,E4:E104,SUM,1)