How to Rank Group Wise in Google Sheets in Sorted or Unsorted Group

Published on

You can use the RANK and FILTER functions to perform group-wise ranking in Google Sheets. This tutorial will guide you through the process.

For group-wise ranking, you typically have two columns:

  1. A category column may contain dates (for ranking based on dates, months, or years), or groups such as Group A, Group B, etc.
  2. The second column contains the numbers to be ranked.

Additionally, it’s worth noting that the data doesn’t need to be sorted in any particular order. Let’s explore how to rank groups in Google Sheets.

Rank Group Wise in Google Sheets (Rank Within Group)

Below is a sample dataset in the range A2:B8. The first column represents the groups, while the second column contains the numbers to be ranked.

Column A consists of two groups: “Group A” and “Group B”. You can include as many groups as necessary.

Rank Within Groups in Google Sheets

The following RANK formula in cell D2, which is then copied to the range D3:D8, calculates the ranking within each group.

=RANK(B2, FILTER($B$2:B$8, $A$2:A$8=A2), 0)

This adheres to the syntax: RANK(value, data, [is_ascending])

Where:

  • value: B2
  • data: FILTER($B$2:B$8, $A$2:A$8=A2)

The FILTER adheres to the syntax: FILTER(range, condition), where the range is $B$2:B$8 and the condition is $A$2:A$8=A2.

  • is_ascending: 0

In this formula, the greatest value in each group will receive rank 1. To assign rank 1 to the lowest value, change the last parameter to 1.

=RANK(B2, FILTER($B$2:B$8, $A$2:A$8=A2), 1)

To help you grasp the contrast between overall ranking and group-wise ranking, consider the sorted data presented below the original dataset, spanning from cells A12 to B18.

This sorted arrangement organizes the data based on the group in ascending order and then by the score in descending order. Examine the outcomes in column D within this sorted range.

Understanding this process should clarify how to perform ranking group-wise in Google Sheets.

RANK + Filter Combo Formula Explanation

When you copy or drag the formula from cell D2 down, the formula adjusts the rank value and filter condition accordingly. For instance, consider the formula in cell D4:

=RANK(B4, FILTER($B$2:B$8, $A$2:A$8=A4), 0)
Rank and Filter Combination in Group-Wise Ranking

This formula filters column B based on the values in column A that match “Group B”. For row 4, the filter returns the array {38; 92}.

Thus, for row #4, you can interpret the Rank formula as follows, resulting in a rank of 2:

=RANK(38, {38; 92}, 0)

Array Formula for Group-Wise Ranking

We can simplify applying the formula by using the MAP function. Clear cells D2:D8 and then insert the following array formula into cell D2:

=MAP(A2:A8, B2:B8, LAMBDA(cat, val, RANK(val, FILTER(B2:B8, A2:A8=cat), 0)))

Let’s break it down step by step.

Firstly, we’ll construct a custom function using the LAMBDA function.

The syntax for the LAMBDA function is: LAMBDA(name1, name2, formula_expression)

In our case, the formula_expression will include our RANK and FILTER combination that provides the group-wise ranking. However, we’ll replace A2 and B2 (which change as we copy-paste downwards) with more meaningful names. We’ll use ‘cat’ and ‘val’ for A2 and B2, respectively.

The custom lambda function will be:

LAMBDA(cat, val, RANK(val, FILTER(B2:B8, A2:A8=cat), 0))

Next, the syntax for the MAP function is as follows: MAP(array1, array2, lambda)

We’ll replace lambda with the custom lambda function we created above, and specify A2:A8 and B2:B8 in array1 and array2.

The MAP function will enable the lambda function to iterate over each value in the arrays A2:A8 and B2:B8, assigning ‘cat’ and ‘val’ to each corresponding value, respectively.

What About Dates in a Category Column?

In group-wise ranking, if the category column contains dates, you can rank based on date, month, or year.

Let’s assume A2:A8 contains dates.

For date-wise ranking, you can use the same formula as above:

=RANK(B2, FILTER($B$2:B$8, $A$2:A$8=A2), 0)
=MAP(A2:A8, B2:B8, LAMBDA(cat, val, RANK(val, FILTER(B2:B8, A2:A8=cat), 0)))

For month-wise ranking:

=RANK(B2, FILTER($B$2:B$8, EOMONTH($A$2:A$8, 0)=EOMONTH(A2, 0)), 0)
=MAP(A2:A8, B2:B8, LAMBDA(cat, val, RANK(val, FILTER(B2:B8, EOMONTH(A2:A8, 0)=EOMONTH(cat, 0)), 0)))

The EOMONTH function converts the dates in A2:A8 to end-of-the-month dates, facilitating month-wise ranking.

For year-wise ranking:

=RANK(B2, FILTER($B$2:B$8, YEAR($A$2:A$8)=YEAR(A2)), 0)
=MAP(A2:A8, B2:B8, LAMBDA(cat, val, RANK(val, FILTER(B2:B8, YEAR(A2:A8)=YEAR(cat)), 0)))

Resources

Here are some related resources.

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.

Lookup Values Under Categories in Excel and Google Sheets

We can use a combination of XLOOKUP and VLOOKUP in both Excel and Google...

Extract Items Under a Category from the Same Column in Excel

In Excel, you can use a combination of the XLOOKUP and DROP functions to...

How to Create a Searchable Table in Excel Using the FILTER Function

Finding specific records, or rows containing the required information, is straightforward in Excel using...

Time Sequences in Excel by Minute, Hour, or Second Increments

Creating time sequences, whether by hour, minute, or second increments in Excel, can be...

More like this

XMATCH Row by Row: Finding Values Across a Range in Google Sheets

Using the BYROW function with XMATCH in Google Sheets allows us to match values...

Limit Formula Expansion to a Specific Row in Google Sheets

In this tutorial, I’ll explain how to limit the expansion of an array formula...

3-D Referencing Structured Data Tables in Google Sheets

When you have several tables within a single sheet—not across multiple sheets in a...

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.