Smart Systems for Home

Plain how-to guides for smart home, security and home Wi-Fi

Excel GROUPBY Function: Step-by-Step Example

· 5 min read · Home Tech

In this article
  1. What You'll Need
  2. Step-by-Step
  3. Common Mistakes
  4. Tips
  5. Troubleshooting
  6. Frequently Asked Questions

Excel's GROUPBY function summarises a table of data by one or more categories in a single formula, returning a spilled array of results. For example, to total sales by region from a table with Region and Sales columns, you enter =GROUPBY(A2:A100, B2:B100, SUM) and Excel spills one row per region with its total. GROUPBY is available in Excel for Microsoft 365 (Current Channel) and Excel for the web; it is not in Excel 2021, 2019 or earlier, and it is not in Google Sheets. You need a contiguous data range with a header row and a blank area to the right and below where the results can spill.

What You'll Need

  • Excel for Microsoft 365 (Current Channel) or Excel for the web. If your version does not have GROUPBY, the formula returns #NAME? and you will need to use a PivotTable instead.
  • A data table with a header row and no completely blank rows or columns inside the range.
  • A blank cell to the right of and below your data where the spilled results can appear. If anything blocks the spill area, Excel returns #SPILL!.
  • Basic familiarity with referring to ranges (for example A2:A100) and with functions such as SUM, AVERAGE or COUNT.

Step-by-Step

  1. Lay out your data. Put your source data in a contiguous block. For this example, use columns A (Region), B (Product) and C (Sales), with headers in row 1 and data from row 2 down to row 100. Do not leave blank rows inside the range.
  2. Choose a blank output cell. Click a cell that has enough empty space to its right and below for the results. For example, click E2. The formula will spill into as many rows as there are unique groups and as many columns as you request.
  3. Enter the basic GROUPBY formula. Type =GROUPBY(A2:A100, C2:C100, SUM) and press Enter. Excel returns one row per unique region with the total Sales for that region. The first argument is the row field (the category), the second is the values to aggregate, and the third is the aggregation function.
  4. Add a second grouping level. To group by Region and then Product, enter =GROUPBY(A2:A100, C2:C100, SUM, 3, 1). The fourth argument is field_headers (3 means the source has headers but do not show them in the output), and the fifth argument is total_depth (1 adds a grand total row at the bottom). The result lists each region and product combination with subtotals and a grand total.
  5. Choose a different aggregation. Replace SUM with AVERAGE, COUNT, MAX or MIN. For example, =GROUPBY(A2:A100, C2:C100, AVERAGE) returns the average sale per region. You can also pass an array of functions such as {SUM, AVERAGE} to return multiple summary columns side by side.
  6. Sort the results. The sixth argument is sort_order. Use 1 for ascending, -1 for descending, or an array to sort by multiple columns. For example, =GROUPBY(A2:A100, C2:C100, SUM, 3, 1, -1) sorts regions by total sales from highest to lowest.
  7. Filter the groups. The seventh argument is filter_array. To include only regions with total sales above 1000, enter =GROUPBY(A2:A100, C2:C100, SUM, 3, 1, -1, C2:C100>1000). The filter is applied to the source rows before aggregation, so only rows meeting the condition are summarised.
  8. Check the spill. If you see a single value instead of a table, or a #SPILL! error, clear the cells to the right and below the formula cell. GROUPBY always returns an array; it cannot be entered in a cell that is part of an Excel Table (ListObject) unless you convert the table to a range first.

Common Mistakes

  • Using GROUPBY in an older Excel version. The function is not available in Excel 2021, 2019 or earlier, and it is not in Google Sheets. The formula returns #NAME?. Use a PivotTable or SUMIFS instead.
  • Forgetting the aggregation function. GROUPBY requires a function as its third argument. Omitting it or typing a text label such as "Sum" instead of the function SUM causes an error.
  • Blocking the spill range. Any existing value, merged cell or table in the output area causes #SPILL!. Clear the area or move the formula.
  • Mismatched range sizes. The row field, values and filter arrays must be the same height. A mismatch returns #VALUE!.
  • Expecting headers by default. The fourth argument controls whether headers are shown. Setting it to 3 (headers present, do not show) is a common choice when you want a clean table.

Tips

  • Use structured references if your data is in a table: =GROUPBY(Table1[Region], Table1[Sales], SUM). Convert the table to a range first if you need to place the formula inside the same sheet area.
  • Combine GROUPBY with SORT and FILTER for dynamic dashboards. The results update automatically when the source data changes.
  • To show a grand total, set total_depth to 1. To show subtotals for multiple row fields, use 2.
  • If you need the output as static values, copy the spilled range and paste as values.

Troubleshooting

If GROUPBY returns #NAME?, your Excel version does not support it. Check File > Account > About Excel and confirm you are on Microsoft 365 Current Channel, or use Excel for the web. If you see #SPILL!, select the formula cell and look for the dashed spill border; delete or move anything inside that border. If you see #VALUE!, check that all range arguments have the same number of rows. If the formula returns unexpected groups, verify there are no leading or trailing spaces in the category column and that blank cells are not being treated as a group. If the function is greyed out or unavailable in Formula AutoComplete, the feature is not rolled out to your build; use a PivotTable as a fallback.

Frequently Asked Questions

Does GROUPBY work in Excel 2021 or Google Sheets?

No. GROUPBY is exclusive to Excel for Microsoft 365 (Current Channel) and Excel for the web. Excel 2021, 2019 and Google Sheets do not have it; use a PivotTable or SUMIFS instead.

Can I group by more than one column?

Yes. Pass an array of ranges as the first argument, for example HSTACK(A2:A100, B2:B100), or use a range that spans multiple columns such as A2:B100. The output will have one row per unique combination.

How do I add a grand total row?

Set the fifth argument (total_depth) to 1. For multiple row fields, use 2 to show subtotals at each level plus a grand total.

Why does GROUPBY return a #SPILL! error?

The output area is blocked. Clear the cells to the right and below the formula cell, or move the formula to an empty area. Merged cells and Excel Tables also block spills.

Can I use custom aggregation functions?

Yes, you can use any function that accepts a range and returns a single value, such as SUM, AVERAGE, COUNT, MAX, MIN, or a LAMBDA. For example, LAMBDA(x, MAX(x)-MIN(x)) returns the range of values per group.