How Many Times Each Value Repeats in a Column: Google Sheets

Published on

The QUERY function is a versatile method for counting how many times each value repeats in a column in Google Sheets.

Counting the occurrence of each value in a column can provide greater insight into your data.

For example, in a truck transportation list, you can count departure dates in a column to determine the number of trips on each date. (Note: This method does not apply to timestamp columns.)

You can easily find the multiple occurrences of a single value or keyword in a column. Google Sheets count functions are a great help in this case.

To find how many times a single item repeats in a column, you can use the COUNTIF function.

E.g.:

=COUNTIF(A2:A, "Mango")

But this way, you can’t easily count different repeating items at the same time. For that, you may need to use UNIQUE and ARRAYFORMULA. We will discuss that combination formula later on.

As mentioned above, we can use the QUERY function to find how many times each value repeats in a column. Let’s see that first.

Count Repetitions of Each Value in a Google Sheets Column: QUERY

Sample Data:

The following sample data consists of receipts of fruits in equal quantities on various dates. It’s in the format Product, Qty, Unit, and Date of Receipt.

Sample data to count multiple items

We will count the fruit names in column A and determine how many times each fruit repeats in the column. This will indicate the number of lots you received.

Formula:

=QUERY(A1:A, "SELECT A, COUNT (A) WHERE A IS NOT NULL GROUP BY A", 1)

When using this formula, you should replace 1 in the last part of the formula with 0 if you don’t have a header at the top of the column.

When you apply the above QUERY formula, you will get the following result:

QUERY formula to count the frequency of each value in a column

From this, we can find how many times each value repeats in a column. For example, we can see the product Apple repeats twice, Banana once, etc.

The formula follows the syntax QUERY(data, query, [headers]).

Where:

  • data: A1:A – the column that contains the values to count for occurrences of each value.
  • query: "SELECT A, COUNT(A) WHERE A IS NOT NULL GROUP BY A" – selects and counts column A, filtering out blank rows and grouping by unique values.
  • header: 1 – the number of header rows.

Two Alternatives to Count How Many Times Each Value Repeats in a Column

I have two interesting combinations to replace the above formula. Both alternatives are worth trying.

1. UNIQUE + COUNTIF

In this method, we will use the UNIQUE function to get the distinct values in column A and COUNTIF to get the count of those values.

Formula:

=ArrayFormula(
   LET(
      val, TOCOL(UNIQUE(A2:A), 1), 
      HSTACK(val, COUNTIF(A2:A, val))
   )
)

Formula Explanation:

  • UNIQUE(A2:A): Returns the distinct values.
  • TOCOL(…, 1): Removes empty cells from the distinct values, if any.
  • The LET function assigns the name ‘val’ to the above result.
  • COUNTIF(A2:A, val): Returns the count of ‘val’ in the range A2:A.
  • HSTACK(val, …): Horizontally stacks the ‘val’ with the count results.

The COUNTIF function requires ARRAYFORMULA when counting multiple criteria, here ‘val’ in a range.

Similar to QUERY, this also returns how many times each value repeats in a column. This has one advantage over the QUERY solution: it won’t sort the values in column A.

2. SORTN + COUNTIF

In this approach, we will use the COUNTIF function to count the values in A2:A and SORTN to get the distinct rows.

Formula:

=LET(
   val, TOCOL(A2:A, 1), 
   SORTN(HSTACK(val, COUNTIF(val, val)), 9^9, 2, 1, 1)
)

Formula Explanation:

  • TOCOL(A2:A, 1): Removes empty cells in A2:A.
  • The LET function assigns the name ‘val’ to this output.
  • COUNTIF(val, val): Returns the count of values against those values.
  • HSTACK(val, …): Horizontally stacks the ‘val’ with the above output.
  • SORTN(…, 9^9, 2, 1, 1): Returns 9^9 rows, removing duplicates and sorting the ‘val’ in ascending order.

Here the COUNTIF does not require ARRAYFORMULA since the SORTN function acts as an array function.

You can use this to return how many times each value repeats in a column, similar to QUERY.

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 Create a Case-Sensitive Pivot Table in Excel

As you may know, Pivot Tables in Excel are case-insensitive by default. To create...

How to Create a Case-Sensitive Pivot Table in Google Sheets

To create a case-sensitive pivot table in Google Sheets, we will use a helper...

Marking Case-Sensitive Unique Values in Excel

Marking case-sensitive unique values provides several benefits compared to merely extracting them in an...

Highlight Upcoming Birthdays in Google Sheets

When highlighting upcoming birthdays in Google Sheets, one important aspect is ignoring the year...

More like this

How to Create a Case-Sensitive Pivot Table in Google Sheets

To create a case-sensitive pivot table in Google Sheets, we will use a helper...

Highlight Upcoming Birthdays in Google Sheets

When highlighting upcoming birthdays in Google Sheets, one important aspect is ignoring the year...

Calculate Weighted Average in Pivot Table in Google Sheets

You can calculate a weighted average in a Pivot Table using a custom formula...

12 COMMENTS

  1. Is there any formula available?

    If I have a column (column A) of unique values data, and a second column (column E) with some values taken from the unique values and repeating again and again, I want to count the repeating values and see how many times each value repeats in column E.

    • Hi Khalid,

      You can use this COUNTIF formula with ARRAYFORMULA:

      =ARRAYFORMULA(COUNTIF(E2:E12, A2:A5))

      Adjust the range references as per your original ranges in the sheet.

      I hope this helps!

  2. How would you limit your results to only display the data that actually have duplicates, then their total number of duplicates? (display line items only if the results = 2 or greater)

  3. Thank you for the great write-up!

    Question: how do I preserve the original length of the dataset and add a column of # of occurrences?

  4. So I need to do something like this but haven’t been able to work it out yet.
    I have two date columns in my source data table (received date, complete date).
    I would like to create a Query that displays the results as col 1: date, col 2: count of received, col 3: count of complete. Any suggestions? Thx

    • Hi, Jonny,

      You can try this for the range A2:B (A – received, B – completed)

      =query(A1:B,"Select A,B,count(A),count(B) where A is not null group by A,B")

      It returns a four-column Array. So empty D1:G and insert it in cell D1.

  5. Syntax has changed on Google Sheets. Use ; instead , … Here the working one:

    =QUERY(A2:N1500;"Select A, count (A) WHERE I = TRUE group by A ORDER BY count (A) DESC"; 1)

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.