Situatie
Solutie
The GROUPBY function has eight arguments:
=GROUPBY(a,b,c,d,e,f,g,h)
Arguments a to c are required:
- a (row fields): The range (one or more columns) containing the values or categories by which the data is to be grouped.
- b (values): The range (one or more columns) containing the values that aggregate the data.
- c (function): A function used to aggregate the values in argument b.
Arguments d to h are optional, and you can learn more about these in the final section of this article:
- d (field headers): A number that specifies whether you selected headers in arguments a and b, and whether they should be displayed in the output.
- e (total depth): A number that determines whether the output should display totals.
- f (sort order): A number that indicates how the result is ordered.
- g (filter array): An array-oriented formula that filters out unwanted information.
- h (field relationship): A number that specifies the field relationships when multiple columns are provided in argument a.
GROUPBY in Action: Using the Required Arguments Only
Let’s imagine you own a chain of restaurants that serve up different dishes from different cuisines, and you’ve totted up the total sales and average customer rating for each cuisine-dish combination.
While these figures are useful, maybe you’re more interested in how the data compares across different categories. Specifically, you might want to find out the total cash each cuisine brings in, and the average customer rating for each type of dish.
To find out the sales totals for each cuisine, in cell F2, type:
=GROUPBY(
Since you’re looking to group the data by cuisine, select the cells containing this variable, and add a comma. In this case, because the data is in a formatted Excel table called TabFood, a structured reference to the column name is added to my formula:
=GROUPBY(TabFood[Cuisine],
Then, since you’re looking to see the total sales for each of those cuisines, select the cells containing these figures, and add another comma:
=GROUPBY(TabFood[Cuisine],TabFood[Sales],
The final mandatory argument is the function to be used on the aggregating data. In this case, since you want to find out the total sales for each cuisine, you need to insert the SUM function, and close the parentheses:
=GROUPBY(TabFood[Cuisine],TabFood[Sales],SUM)
When you press Enter, you’ll see that Excel has aggregated the total sales for each cuisine. Since you haven’t included any of the optional arguments in the GROUPBY function, the data is sorted in alphabetical order according to the values in column F by default, and there’s a total row at the bottom of your extracted data.
As the values in column G are financial, select the data and click the “Accounting” icon in the Number group of the Home tab on the ribbon.
Now, you want to find out the average customer rating for each type of dish, and the process for doing so is very similar.
In cell I2, type:
=GROUPBY(
Next, select the cells containing the category by which you want to group the data. In this case, it’s the different dishes. Remember to add a comma after each argument to move to the next.
=GROUPBY(TabFood[Dish],
Now, select the cells containing the data to be aggregated, and add another comma:
=GROUPBY(TabFood[Dish],TabFood[Customer rating]
Finally, because your aim on this occasion is to find out the average customer rating for each dish type, the function argument needs to be AVERAGE.
=GROUPBY(TabFood[Dish],TabFood[Customer rating],AVERAGE)
As the values in column J are decimalized averages, tidy up the number of decimal places on display by clicking the “Increase Decimal” and “Decrease Decimal” buttons in the Number group of the Home tab.
Leave A Comment?