Extract First n Rows From Each Group in Google Sheets

Published on

To limit rows, I normally use Query in Google Spreadsheets. Can we use Query to extract n first rows from each group in Sheets?

Yes! We can. You can limit n rows in each group in Query in Google Sheets.

Before getting into the details, here is one similar tutorial that some of you may find useful – How to Find the Highest N Values in Each Group in Google Sheets.

I am using the below data to explain to you how to extract the first n rows from each group in Google Sheets.

In the demo data, the column A contains the Group names and B some values.

Remember the data is already sorted by column A to arrange the group of items in order.

If your data is unsorted (not grouped) then there is a different formula. I have included that at the last part of this tutorial. But I suggest you to first learn the solution with the sorted data.

How to Extract First n Rows From Each Group

Data is Sorted by Column A:

Extract First n Rows From Each Group

I have a list contain the names of flowers like Orchids, Tulips, and Marigold in column A and its stem count in column B.

I want to filter the first n rows from each group of flowers. With the help of Query Limit clause we can limit the number of rows, but not in each group –Three Different Ways to Limit Number of Rows in Google Sheets Query.

In this workaround I am using Query Where clause and a running count formula.

Here is the formula that extracts the first 2 rows from each flower group. I’ve used this formula in cell D2 above.

=Query({A2:B,ARRAYFORMULA(COUNTIFS(A2:A,A2:A,ROW(A2:A),"<="&ROW(A2:A)))},"Select Col1,Col2 where Col3<=2")

Filter First N Rows in Group – Formula Explanation

This section handles the above formula explanation.

Actually, we have two column data ‘physically’. But in Query, there is a third column generated by the running count formula.

Running Count to Filter First N Rows in Group

The running count populates sequential numbers 1, 2, 3 … that restarts from counting from 1 in each change in the group.

If you enter the running count in a blank cell (for example in cell C2) to test, the result would be as follows.

serial number restarts in each group

Want to know more about the above running count formula?

Must Check: Running Count in Google Sheets – Formula Examples.

I have this third column virtually preset in Query. In Query Where clause I have used the “<=” comparison operator to filter the data, i.e. Col3 <=2. That means every first two rows in each group.

Change this # 2 to the number of rows that you want to extract from each group.

To extract first n rows from each group, you can follow the above method in Google Sheets.

Extract n First Rows from Each group (Unsorted)

You can easily tweak the above formula and use it in an unsorted group of data as below.

sort and filter n rows in unsorted list

Here is the new formula to extract the first n rows from each unsorted group in Google Sheets.

=Query({sort(A2:B,1,0),ARRAYFORMULA(COUNTIFS(sort(A2:A,1,0),sort(A2:A,1,0),ROW(A2:A),"<="&ROW(A2:A)))},"Select Col1,Col2 where Col3<=2")

Here I have used the SORT function with all the data ranges used in the formula except in the ROW function.

I have virtually sorted that data using the SORT function. That’s the one and only changes in the formula.

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.

Hierarchical Numbering Sequences in Excel

Creating hierarchical numbering sequences in an Excel spreadsheet can significantly improve the way you...

How to Easily Repeat a Sequence of Numbers in Excel

Excel offers multiple ways to accomplish tasks, and the simplicity of each approach depends...

Create a Sequence of Dates at Every Nth Row in Excel (Dynamic Array)

Would you like to create a sequence of dates in every nth row in...

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...

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...

14 COMMENTS

  1. This is the best. Thank you!

    I’ve first sorted my data by group, then by value. Would there be a way to return multiple rows if there was a tie for the largest value?

    It’s fine if this requires helper columns, but I would like them to dynamically grow to fit the data. I greatly appreciate your help!

  2. Prashanth,

    Col5 is the Login Time Stamp, Col1 is the Username.

    I need the resulting table to show the first Login Time Stamp per Username and all dates.

    So, the list should show a unique Time Stamp (Minimum) per Username and Date.

    I think this is like a “Group by Date,” but I have no idea how to do it.

    Thanks, I appreciate any help on it!

    • Hi, Jose,

      I’m not clear.

      If you want to group data, there should be an aggregation column. Also, you must convert timestamp to date.

      You can start with this.

      =ArrayFormula(QUERY({A1:D,{"Date";INT(E2:E)},F1:H},"Select * where Col1 is not null"))

      Or share a sample sheet link below.

  3. Hey, thanks a lot for this, it’s very useful!

    Would you know how I can extract the top n% values from each group?

    I have a column A which contains the Group names and B some score values, and for each group name I’d like to be able to select the top 10%
    best scores.

    Have a great day,
    Lorraine

  4. Is there a way to get this to work on a different sheet/tab? I have 7 columns (A:G) and I’m able to get it to work on the same sheet/tab as my source data but not on a different one. I’m adding the name of the source data sheet but I’m getting errors. Thanks.

    • Hi, Jason,

      If your sheet name contains space, you should include the single quote surrounding the sheet name.

      Eg:

      Sheet Name: product summary
      Range: A2:B

      It should be referred to in the formula as;
      'product summary'!A2:B not product summary!A2:B

      If this doesn’t help, share your sheet link in reply which won’t be published (remove personal/confidential info from the sheet).

    • Hi, Bob,

      To combine values in columns A and B in ‘Sheet1’ and ‘Sheet2’, use this formula.

      =Query({Sheet1!A:B;Sheet2!A:B},"Select * where Col1 is not null")

      On this output, you can use my formula to extract first ‘n’ rows.

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.