HomeGoogle DocsSpreadsheetSum Max n Values Group Wise in Google Sheets

Sum Max n Values Group Wise in Google Sheets

Published on

In order to sum max n values group wise, I am using the Query and running count array formula in Google Sheets.

This formula will be useful in many ways in your real-life. Like;

  1. Sum the highest/max ‘n’ scores of each player.
  2. Sum the max/highest ‘n’ sales done by each salesperson.
  3. Extract and sum the highest marks scored in any three subjects by all students.

You can relate so many like examples.

To sum max n values group wise in Google Sheets, as I have said, the running count plays an important role.

How to Sum Max n Values Group Wise in Google Docs Sheets

For every example, sample data is a must. I am taking this one, the marks of two students in five subjects, for our example purpose.

demo data to learn how to Sum Max n Values Group Wise in Sheets

First I am going to provide you with a formula that works in a sorted dataset. For unsorted dataset, there is another formula but both are almost similar.

Sum Highest n Values Group Wise in a Sorted Range

The above sample data contains sorted information. As you can see the names are sorted in column A. But we want column B also sorted that in descending order.

So first sort the data properly. To do that select the array A2:C11. Then go to the menu Data > Sort range. Do the sorting as per the below setting.

manual sorting of data in google Docs Sheets

Here I am taking the number 3 as ‘n’.

The Query is the key function that we can use to sum the highest n values in each group. But Query alone can’t do that.

We need to make a virtual helper column that contains the running count.

ARRAYFORMULA(if(len(A2:A),COUNTIFS(A2:A,A2:A,ROW(A2:A),"<="&ROW(A2:A))))

This is that formula that generates the virtual helper column. We can use this to generate a four column array as below.

={A2:C,ARRAYFORMULA(if(len(A2:A),COUNTIFS(A2:A,A2:A,ROW(A2:A),"<="&ROW(A2:A))))}

This is a four column dataset which contains the “Name” in the first column, “Marks out of 100” in the second column, “Subject” in the third column, and the “Helper” column as the fourth column.

Use the comparison operator <= to limit the number of rows to three in each group as below. It’s possible because of the running count in the fourth column.

=Query({A2:C,ARRAYFORMULA(if(len(A2:A),COUNTIFS(A2:A,A2:A,ROW(A2:A),"<="&ROW(A2:A))))},"Select * where Col4 <=3")

With the help of another Query we can sum max 3 values group wise.

Final Formula:

=Query(Query({A2:C,ARRAYFORMULA(if(len(A2:A),COUNTIFS(A2:A,A2:A,ROW(A2:A),"<="&ROW(A2:A))))},"Select * where Col4 <=3"),"Select Col1, Sum(Col2) where Col1 is not null group by Col1")
Sum max n values in each group in a sorted range

Sum Max n Values Group Wise in an Un-sorted Range

Suppose my data is not sorted or it’s imported using the Importrange and frequently updating. So it won’t be a wise choice to manually sort the data each and every time.

I want a set and forget formula to sum max n values group wise in Google Sheets irrespective of the data order.

You can use the above final formula with the following changes in an unsorted array.

Use the below SORT formula to replace the array A2:C in the above formula.

sort(A2:C,1,true,2,false)

Then use the below SORT formula to replace the array A2:A inside the COUNTIFS (only the first two references).

sort(A2:A,1,true)

So the final formula to sum max 3 values group wise in an un-sorted array would be as follows.

=Query(Query({sort(A2:C,1,true,2,false),ARRAYFORMULA(if(len(A2:A),COUNTIFS(sort(A2:A,1,true),sort(A2:A,1,true),ROW(A2:A),"<="&ROW(A2:A))))},"Select * where Col4 <=3"),"Select Col1, Sum(Col2) where Col1 is not null group by Col1")
Sum Max n Values Group Wise – Un-sorted Range
Prashanth KV
Prashanth KV
Your Trusted Google Sheets and Excel Guide Prashanth KV brings a wealth of experience in Google Sheets and Excel, cultivated through years of work with multinational corporations in Mumbai and Dubai. As a recognized Google Product Expert in Docs Editors, Prashanth shares his expertise through insightful blogging since 2012. Explore his blog for practical tips and guidance on maximizing your spreadsheet skills.

Running Total By Month in Excel

This tutorial demonstrates how to calculate the running total by month in a range...

SORT and SORTBY – Excel Vs Google Sheets

While Excel offers the SORT and SORTBY functions for sorting a table, Google Sheets...

Get Top N Values Using Excel’s FILTER Function

When using the FILTER function in Excel to extract the top N values, it's...

XLOOKUP in Excel: Working with Visible Rows in a Table

In Excel, the XLOOKUP function isn't inherently designed to exclusively work with visible rows;...

More like this

Interactive Random Task Assigner in Google Sheets

You have multiple tasks and multiple people. Here's a fun way to randomly assign...

Google Sheets Bar and Column Chart with Target Coloring

To color each data point in the column or bar chart according to the...

Analyzing Column A Between Non-Adjacent Values in Column B: Google Sheets

This tutorial addresses a common scenario encountered in Google Sheets: how to sum, average,...

LEAVE A REPLY

Please enter your comment!
Please enter your name here

This site uses Akismet to reduce spam. Learn how your comment data is processed.