Group and Average Unique Values by Category in Google Sheets

Before diving into the formulas, let’s first clarify what it means to group and average unique values by category in Google Sheets. We’ll use the following sample data as an example:

NameAreaQty. Sold
CaryNorth10
CaryNorth10
CaryNorth10
CarySouth20
DariaNorth10
DariaSouth5
DariaWest15

In this dataset, we’ll show different methods to calculate averages by grouping categories.

Basic Average of Quantities Sold

To calculate the simple average of the quantities sold (column C), you can use the formula:

=AVERAGE(C2:C)

This formula calculates the overall average without grouping by any category, returning the average of all quantity values in column C.

Average by Single Category (Name)

If you want to calculate the average quantity sold by each unique Name (ignoring duplicates within each group), the average will differ from a simple overall average. Let’s calculate it manually first to understand the concept:

  • Cary‘s total sales are 10 + 10 + 10 +20 = 50
  • Daria‘s total sales are 10 +5 +15 = 30

Thus, the average by name is (50 +30) / 2 = 40

This approach is referred to as Average by Unique Name.

Average by Multiple Categories (Name and Area)

If you want to consider both Name and Area while calculating the average, the calculation changes.

  • Cary has two unique areas:
    • North (total: 10 + 10 + 10 = 30)
    • South (total: 20)
    • Average for Cary: (30 + 20) / 2 = 25
  • Daria has three unique areas:
    • North (total: 10)
    • South (total: 5)
    • West (total: 15)
    • Average for Daria: (10 + 5 + 15) / 3 = 10

This approach is referred to as Average by Unique Name and Area.

Calculating Unique Averages by Single Category

We’ll use the QUERY function to calculate unique averages by a single category. Here’s the formula to group and calculate unique values by a single category (e.g., Name):

=AVERAGE(
   QUERY({A2:A, C2:C}, "SELECT SUM(Col2) WHERE Col1 IS NOT NULL GROUP BY Col1 LABEL SUM(Col2)''", 0
   )
)
Calculating Unique Averages by a Single Category in Google Sheets

Explanation:

  • Replace A2:A with the category column reference and C2:C with the column containing the values to be averaged.
  • This formula sums up quantities for each unique Name and calculates their overall average.

In this example:

  • Cary’s total is 50, and Daria’s total is 30, resulting in (50 + 30) / 2 = 40

Calculating Unique Averages by Multiple Categories

We can use two QUERY functions combined with the LET function to calculate unique averages by multiple categories (e.g., Name and Area). Here’s the formula, followed by an explanation:

=LET(
   qryA, QUERY({A2:A, C2:C}, "SELECT Col1, SUM(Col2) WHERE Col1 IS NOT NULL GROUP BY Col1 LABEL Col1'', SUM(Col2)''", 0), 
   qryB, QUERY(UNIQUE({A2:A, B2:B}), "SELECT COUNT(Col1) WHERE Col1 IS NOT NULL GROUP BY Col1 LABEL COUNT(Col1)''", 0), 
   HSTACK(CHOOSECOLS(qryA, 1), INDEX(CHOOSECOLS(qryA, 2)/qryB))
)
Calculating Unique Averages by Multiple Categories in Google Sheets

Explanation

The LET function simplifies the formula by assigning the queries to qryA and qryB, making it easier to reference them in calculations.

1. qryA:

QUERY({A2:A, C2:C}, "SELECT Col1, SUM(Col2) WHERE Col1 IS NOT NULL GROUP BY Col1 LABEL Col1'', SUM(Col2)''", 0)

This query groups by the Name and calculates the total quantity sold for each name, returning:

Cary50
Daria30

2. qryB:

QUERY(UNIQUE({A2:A, B2:B}), "SELECT COUNT(Col1) WHERE Col1 IS NOT NULL GROUP BY Col1 LABEL COUNT(Col1)''", 0)

This query counts unique Name and Area combinations. Using UNIQUE removes duplicate entries, and COUNT calculates the number of unique areas per name. The result:

2
3

3. Final Calculation:

HSTACK(CHOOSECOLS(qryA, 1), INDEX(CHOOSECOLS(qryA, 2)/qryB))
  • CHOOSECOLS(qryA, 1) selects the first column from qryA, which contains the names.
  • INDEX(CHOOSECOLS(qryA, 2)/qryB) divides the total quantities from qryA by the unique area counts from qryB, calculating the average quantity sold per unique area.
  • HSTACK then horizontally combines the names from qryA with the calculated averages.

This results in:

Cary25
Daria10
  • Cary’s average: 50/2 = 25
  • Daria’s average: 30/3 = 10

This formula effectively calculates the unique averages by dividing the total quantity sold by the count of unique categories, giving a per-category average for each name.

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.

How to Extract Numbers from Text in Excel with Regex

You can use the REGEXEXTRACT or REGEXREPLACE functions to easily extract numbers from text...

Using OFFSET and MATCH Together in Google Sheets: Advanced Tips

One powerful and flexible way to look up values is by combining the OFFSET...

How to Use OFFSET and XMATCH Functions Together in Excel

We often use the OFFSET and XMATCH functions together to match a value in...

How to Calculate Maximum Drawdown in Excel and Google Sheets

You can use the following dynamic array formula to calculate maximum drawdown (MDD) in...

More like this

Using OFFSET and MATCH Together in Google Sheets: Advanced Tips

One powerful and flexible way to look up values is by combining the OFFSET...

Running Count with Structured References in Google Sheets

Running a count with structured references is achievable in Google Sheets tables using the...

Running Total with Structured Table References in Google Sheets

You can use two types of formulas to create a running total with structured...

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.