The GROUPBY function allows us to group data in Excel according to user-defined parameters. It’s a very valid alternative to using PivotTables or grouping data from Power Query, though those remain viable options depending on the specific situation.
This function is compatible only with Excel for Microsoft 365, so if you have a desktop version older than 365 installed, you can follow this guide using the (free) web version of Excel.

The GROUPBY function is available only in Excel for Microsoft 365 and is now on the Current channel. If you don’t see it in your Excel yet, it’s just a matter of updates, try updating and checking again; otherwise, contact your company’s IT department. Along with the GROUPBY function, you should also have two other functions: PIVOTBY and PERCENTAGE.OF.
What the GROUPBY function does in Excel
Like many recent functions, GROUPBY has several possible arguments that allow it to perform different actions in Excel. Microsoft’s approach is to offer functions that are like Swiss Army knives, packed with a wide range of options. I agree with this because it lets us experiment much more quickly, but sometimes they can be a bit complex to wrap your head around:

=GROUPBY(row_fields,values,function,[field_headers],[total_depth],[sort_order],[filter_array],[field_relationship])
So let’s take a look at the individual parameters:
- `Row_fields` determines the row fields. We are therefore defining the data we want returned on each row, and this is the crucial framework for the rest of the function
- Values are the values we use for the subsequent calculation, similar to how you would add a field to a PivotTable
- Function is a predefined list of 15 functions we can use, including SUM
- Field_headers are the headers, and we can specify whether they are absent from the source range and should therefore not be displayed (0), whether the source data contains headers that should not be shown (1), whether the data lacks headers but they should be generated (2), or whether the data contains headers that should be displayed (3)
- `Total_depth` specifies the level of detail we want for the totals: whether we don’t want any totals (0), whether we want grand totals (1), whether we want grand totals and subtotals (2), whether we want grand totals at the top (-1), or whether we want grand totals and subtotals at the top (-2)
- Sort_order specifies the order in which the data is presented. In this case, a positive value indicates ascending order, and a negative value indicates descending order. We select the column of interest and can then decide whether to sort the data in ascending order by the third column (3) or in descending order by the fifth column (-5), and so on. Compared to functions like SORT or SORTBY, there is only one step here
- Filter_array allows us to filter the source data to group only the data from a subset according to Boolean logic. You can, for example, specify TBL_SALES[REGION]="Umbria" to retrieve only data from the Umbria region
- Field_relationship specifies the relationship between fields when there is more than one in row_fields. Entering 0 indicates that the fields are part of a hierarchy and displays subtotals, while entering 1 indicates that the fields are not part of a hierarchy (as seen in the Table view) and subtotals are not displayed. This field helps you sort more precisely if you have multiple fields in `row_fields`, since case 1 treats all combinations as independent, whereas case 0 simply sorts by the first field, ignoring any combinations with larger values
In summary: GROUPBY is becoming the go-to standard for creating complex arrays where the goal is to reduce the amount of data displayed to the user, shifting from an extremely granular view to a more summarized one. It will take some time for it to be adopted in business files, but it’s truly a function that shouldn’t be underestimated.
What functions can be used with GROUPBY in Excel

An interesting feature, common to both GROUPBY and PIVOTBY, is the inclusion of 15 functions that can be used by default in calculations (the “function” parameter). If you’ve ever used functions like SUBTOTAL or AGGREGATE, which include a selection of commands, you’ll find the logic familiar, but here, the advantage is that we can directly enter the function names instead of the numbers that identify them (9 for SUBTOTAL and SUM; here, you can directly enter SUM).
Let’s look at them one by one; I think you’re already familiar with many of them:
- SUM sums the values
- PERCENTAGE.OF calculates the percentage of the total. So if we have three values of 15, 27, and 58, with a total of 100, we’ll get 15%, 27%, and 58% as the results
- AVERAGE calculates the average
- MEDIAN calculates the median
- COUNT counts the number of values present
- COUNTA counts the values present
- MAX calculates the maximum value
- MIN calculates the minimum value
- PRODUCT multiplies the data by column or range. Note that this is not the same as SUM or SUMPRODUCT, because it always operates on the same range
- MATRIX.A.TEXT converts data from a matrix to text
- CONCAT concatenates the values present
- STDEV.S calculates the standard deviation based on a sample
- DEV.ST.P calculates the standard deviation based on the entire population
- VAR.C estimates the variance based on a sample
- VAR.P estimates the variance based on the entire population
- MODE.SNGL returns the most frequently occurring value within a range
- LAMBDA allows you to create a custom LAMBDA function
The convenience of these functions is obvious: instead of writing long, complex formulas and providing arguments for them, we can call them automatically without any extra steps.
A practical example of using GROUPBY in Excel

Our starting database is called TBL_VIDEOGAMES and is a reworked version of this database, which is freely available for download from Kaggle. We’ve kept only a few columns, for simplicity:
- Title: Video game titles
- Total_sales: sales data
- Console: the game console
- Genre: the game genre
- Publisher: the entity responsible for distributing the video game, "publisher"
The questions we’ll answer are:
- What is the total revenue per video game?
- Sort the video games by total revenue (from highest to lowest)
- Sort the consoles and their respective genres by total revenue (from highest to lowest) with and without subtotals
- Group consoles and genres by filtering exclusively for a specific console and sorting by revenue (Z-A)
- Group consoles and genres by filtering exclusively for a specific console and a specific genre, sorted by revenue (Z-A)
- Group consoles and genres by filtering exclusively for a specific console, sorting by revenue (Z-A), and calculating both the sum of revenue and the median
- Group consoles and genres, indicating the number of genres per console, listing them, and sorting the total by number of genres (Z-A)
Let’s get started!
How to Group Data Easily with GROUPBY in Excel

Let’s look at a simple example of grouping data with GROUPBY in Excel:
=GROUPBY(TBL_VIDEOGAMES[title],
TBL_VIDEOGAMES[total_sales],
SUM)
In this case, we’ve defined that:
- The video game titles are the items we want to display in the rows
- Revenue is what we want to use for the calculations
- The calculation will be a sum
- We want to see the total at the end of the grouping (this is an aspect that should not be underestimated)
Here’s a slight variation on the theme, to avoid seeing the final total:
=GROUPBY(TBL_VIDEOGAMES[title],
TBL_VIDEOGAMES[total_sales],
SUM,0,0)
To summarize: I retrieve the individual video games as if I were using UNIQUE (no duplicates) and then sum the revenue for each one. It seems simple, but creating this summary without this formula would have been much more complex, let’s say something like:
=HSTACK(UNIQUE(TBL_VIDEOGAMES[title]),SUMIFS(TBL_VIDEOGAMES[total_sales],TBL_VIDEOGAMES[title],UNIQUE(TBL_VIDEOGAMES[title])))
Here, GROUPBY wins hands down. Let’s increase the complexity of the grouping, though.
How to group and sort data with GROUPBY in Excel
GROUPBY lets you sort the results quite easily. Let’s look at a simple case where the data is sorted based on the argument called Sort_order:

=GROUPBY(TBL_VIDEOGAMES[title],
TBL_VIDEOGAMES[total_sales],
SUM,
0,0,-2)
I know, it seems much more complex than the previous one, but in reality, the first three steps are exactly the same as before. The part with ;0,0;-2 translates as follows:
- The first 0 represents the Field_headers: we don’t want to display the headers
- The second is the depth level of the totals, Total_depth. We’re not interested in them, so we’ll set this to 0 as well
- The -2 means sorting by the second column (2) in descending order (-).
So let’s take the video games, sum the revenue for each one, and then sort them from the one that generated the most revenue to the one that generated the least.
If you’re familiar with using functions like SORT, keep in mind that it would be a solution like SORT(range,2,-1), whereas here there’s simply the -2 (second column, descending).
How to Group and Sort Data Across Multiple Columns Using GROUPBY in Excel

Now let’s see how to group the data by console and genre, then calculate the total revenue, and sort the data by that final revenue
=GROUPBY(HSTACK(TBL_VIDEOGAMES[console],TBL_VIDEOGAMES[genre]),
TBL_VIDEOGAMES[total_sales],
SUM,
0,0,-3)
The HSTACK function effectively considers all combinations of console and genre, then groups the data and performs calculations for each pair. At this point, we sum the revenue and then sort by the third and final column of our array. Note that not only is the X360 the best-selling console, but the data is also sorted by each individual console. So we see the X360 first, but within the X360 subset, the genres are all sorted.

If we also wanted to see the subtotal, we’d simply need to change one of the previous parameters:
=GROUPBY(HSTACK(TBL_VIDEOGAMES[console],TBL_VIDEOGAMES[genre]),
TBL_VIDEOGAMES[total_sales],
SUM,
0,2,-3)
By entering 2, we’ll get the totals for the individual data points, which appear at the end of each subset. We’ll then see the X360 sorted from the best-selling genre to the least-selling one, concluding with the grand total for the X360 before moving on to the next console, in this case, the PS3.
How to group and sort data across multiple columns using GROUPBY in Excel, while filtering the data

How do we filter our results in GROUPBY? Let’s see how to select only the console entered in cell B2:
=GROUPBY(HSTACK(TBL_VIDEOGAMES[console],TBL_VIDEOGAMES[genre]),
TBL_VIDEOGAMES[total_sales],
SUM,
0,1,-3,
TBL_VIDEOGAMES[console]=B2)
Just as if we were using FILTER, we can enter our condition in `filter_array`. In this case, `TBL_VIDEOGAMES[console]=B2` narrows our scope, and we’ll get only the titles associated with a specific genre. As with other formulas, we obviously need to have all the data in the same table (TBL_VIDEOGAMES) to ensure that the formula returns the information we want.

What if there were multiple conditions? Let’s also include the publisher as a condition, as if we wanted to narrow the field down to a single publisher:
=GROUPBY(HSTACK(TBL_VIDEOGAMES[console],TBL_VIDEOGAMES[genre]),
TBL_VIDEOGAMES[total_sales],
SUM,
0,0,-3,
(TBL_VIDEOGAMES[console]=F2)*(TBL_VIDEOGAMES[publisher]=G2))
Since when we talk about FILTER we need to think in terms of Boolean algebra, the two conditions must be placed in parentheses because they generate an array of the form (TRUE\TRUE\TRUE) * (FALSE\TRUE\TRUE). Only when both conditions are met (TRUE*TRUE) will we get a result.

Another option is to insert the filter directly as the second argument of the function, a trick I discovered in this LinkedIn post by Jessica S:
=GROUPBY(HSTACK(TBL_VIDEOGAMES[console],TBL_VIDEOGAMES[genre]),
(TBL_VIDEOGAMES[total_sales])*
(TBL_VIDEOGAMES[console]=B2)*
(TBL_VIDEOGAMES[publisher]=C2),
SUM,
0,0,-3)
Essentially, instead of using `filter_array`, we place the conditions in the values section. The key difference is that, in this case, we’ll get all possible combinations of console and genre under those that actually have revenue, as you can see in the image.
How to Group and Sort Data Across Multiple Columns Using GROUPBY in Excel, Filtering Data and Performing Multiple Calculations

Let’s increase the complexity even further, since we want to find the sum and median of revenue for all consoles and their respective genres, and then filter by a single console. Here’s the complete formula:
=GROUPBY(HSTACK(TBL_VIDEOGAMES[console],TBL_VIDEOGAMES[genre]),
TBL_VIDEOGAMES[total_sales],
HSTACK(SUM,MEDIAN),
0,1,-3,
TBL_VIDEOGAMES[console]=B2)
In this case, we’ll use the HSTACK “trick” again, but instead of applying it to the initial data in our array, we’ll use it for the calculations. As a result, we’ll end up with four different columns: console | genre | total_revenue | median_revenue. You can add as many as you like!
Of course, we don’t really want to see both SUM and MEDIAN, so we can simply remove the first row:

=DROP(GROUPBY(HSTACK(TBL_VIDEOGAMES[console],TBL_VIDEOGAMES[genre]),
TBL_VIDEOGAMES[total_sales],
HSTACK(SUM,MEDIAN),
0,1,-3,
TBL_VIDEOGAMES[console]=B2),1)
DROP removes the first row, which also simplifies our final display. Generally, I prefer to add the headers myself and manipulate the data independently, avoiding pre-filled headers because they complicate things when you perform calculations, but, of course, this comes down to personal preference. Just be careful because, if you keep the headers as well, any calculations like sums and counts might be off, since the headers are inside the array rather than outside it.

What if we wanted to view these results vertically? The reasoning is very similar to the previous example, but this time we’ll work with VSTACK:
=GROUPBY(HSTACK(TBL_VIDEOGAMES[console],TBL_VIDEOGAMES[genre]),
TBL_VIDEOGAMES[total_sales],
VSTACK(SUM,MEDIAN),
0,0,-3,
TBL_VIDEOGAMES[console]=G2)
It always depends on the needs of each specific analysis, because in this case, breaking down the data, similar to an unpivot, helps if we need to perform further calculations later on, however, there will be significantly more rows.

But how would we go about calculating the total revenue for each publisher, as well as by genre, counting them uniquely? Here, too, it’s a combination of what we’ve already seen:
=DROP(GROUPBY(TBL_VIDEOGAMES[publisher],
HSTACK(TBL_VIDEOGAMES[total_sales],TBL_VIDEOGAMES[genre]),
HSTACK(SUM,LAMBDA(x,COUNTA(UNIQUE(x)))),
0,1,-2,
TBL_VIDEOGAMES[console]=B2),
1)
Essentially, the HSTACK values “balance out,” so we’ll sum them for revenue, while for genres we’ll count the unique values. This way, you can use multiple columns for your calculations, not just one!

Now let’s move on to the “final monster”:
=LET(summary,GROUPBY(TBL_VIDEOGAMES[console],
TBL_VIDEOGAMES[genre],
LAMBDA(cons,TEXTJOIN(" | ", TRUE, SORT(UNIQUE(cons)))),,0),
console,CHOOSECOLS(summary,1),
genres,CHOOSECOLS(summary,2),
single_genres,MAP(genres,
LAMBDA(gen,COUNTA(TEXTSPLIT(TEXTJOIN(",",TRUE,gen)," | ",",")))),
result,HSTACK(console,single_genres,genres),
sorted_result,SORT(result,{2,1},{-1,1}),
sorted_result)
In these cases, LET is a real lifesaver, because it lets us break down the formula step by step:
Summary: We’re grouping consoles with their corresponding genres. We combine all unique genres, sorted, into a single string separated by “|”. So, if a console has RPG, FPS, and Puzzle, it will become: FPS | Puzzle | RPG (in a single cell). You can see the result of this section by replacing the last argument, “sorted_result,” in the formula with “summary.” Unfortunately, it’s not very readable, nor can we sort the data, so it needs a little tweaking. SORT(UNIQUE( is necessary; otherwise, it won’t extract the unique values and will create an array with inconsistent results
Consoles and genres: With “consoles,” we’re selecting the unique values from the previous consoles. “Genres,” on the other hand, is the list of genres for each individual console. So far, everything is standard; we could also use INDEX, in the case of
"Single_Genres": since we want to count the genres per console. We use MAP to process each of the "Genres" values, which are the previous cells containing multiple genres in a single cell. We join (TEXTJOIN) the data using a comma, then split the entire result first by the preceding vertical bar "|" and then by the comma "," (TEXTSPLIT). This way, we’re splitting each value into different columns based on the number of genres we find, we count them (COUNTA) and then calculate the total for each group
Result: All that’s left is to combine everything, listing the consoles first, then the number of individual genres, and finally the list of genres themselves.
Sorted_Result: We sort the data by applying more complex conditions than usual. SORT tells us that it will prioritize the second column of our array (the number of individual genres) and then the console name ({2\1}). The number of individual genres will then be sorted from Z to A, and the console name in ascending order ({-1\1})
Conclusions
We’ve seen how GROUPBY is a versatile and convenient function for working with Excel. If you need to analyze a database, this function is an ideal starting point for creating complex analyses, helping you avoid getting bogged down in all sorts of “workarounds” involving many functions that are difficult to reuse.
In my workflow, GROUPBY is a staple, and I hope that, after reading this guide, it will become part of your personal arsenal of Excel functions!
