ID EN
#Dynamic array

GROUPBY

Excel Functions

Summarize data by grouping rows

Syntax

EXCEL
=GROUPBY(row_fields, values, function, [field_headers], [total_depth], [sort_order], [filter_array], [field_relationship])

Arguments

Parameter Description
row_fields The values for grouping.
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.
total_depth [optional] Totals and subtotals. 0 = No, 1 = Grand Totals, 2 = Both, -1 = Grand Totals at top, -2 = Both at top.
sort_order [optional] Sort by index number. Negative numbers = descending order.
filter_array [optional] Logic to exclude specific rows.
field_relationship [optional] Field relationship when multiple columns are provided as row fields. 0 = Hierarchy (default), 1 = Table.

Return Value

Data summary table

Details

The GROUPBY function is designed to summarize data by grouping rows and aggregating values. The result is a dynamic summary table created with a single formula. The output from the GROUPBY function is similar to the output from a Pivot Table, but without formatting. The summary returned by the GROUPBY function is fully dynamic and will immediately recalculate when source data changes. Here is a brief list of GROUPBY features and limitations: The GROUPBY function takes eight arguments, but only the first three are required. In the worksheet below, we use the GROUPBY function to summarize Sales by City. The formula in cell F5 is: For this example, GROUPBY is configured with three arguments as follows: With the inputs above, the GROUPBY function sums Sales by City and outputs the table in F4:

Examples

GROUPBY basic example

The GROUPBY function takes eight arguments, but only the first three are required. In the worksheet below, we use the GROUPBY function to summarize Sa

EXCEL
=GROUPBY(B5:B16,D5:D16,SUM)
GROUPBY with field headers

The field_headers argument is optional. When field_headers is omitted, GROUPBY will automatically detect headers in the source data by testing values.

EXCEL
=GROUPBY(B4:B16,D4:D16,SUM,3) // field headers enabled
GROUPBY calculation options

The third argument in GROUPBY is function, which specifies the calculation to perform when values are grouped. Available calculations include Excel fu

EXCEL
=GROUPBY(D4:D104,D4:D104,COUNTA,1)
GROUPBY calculation options

In the next example, we've changed the calculation to generate a total cost by meal, as seen below. The formula in G5 now looks like this:

EXCEL
=GROUPBY(D4:D104,E4:E104,SUM,1)
GROUPBY with multiple calculations

It is possible to perform more than one calculation with GROUPBY, but it is not obvious how to do so, since the function argument accepts just one val

EXCEL
=GROUPBY(D4:D104,E4:E104,HSTACK(COUNT,SUM),1)
GROUPBY with multiple calculations

Notice that the formula is now entered in cell G4, even though we have set field_headers to 1 (do not display). This is because GROUPBY automatically

EXCEL
=GROUPBY(D4:D104,E4:E104,HSTACK(COUNT,SUM,PERCENTOF),1)

See Also

PIVOTBY HSTACK VSTACK PERCENTOF