How to Exclude Hidden Rows in Google Sheets QUERY

Published on

The QUERY function in Google Sheets can’t identify hidden rows. However, you can select only visible rows using the QUERY function with an easy workaround in Google Sheets.

This means when you use a QUERY formula on already filtered data (or data containing hidden rows), it will only pull the visible rows. This is not possible in the usual case!

In this tutorial, you will learn how to add this powerful feature to a QUERY formula using a workaround approach. For this purpose, we will use a virtual helper column (an additional column that is not physically available in the dataset).

Virtual Helper Column Formula for QUERY Use

Hidden rows can be a nightmare for many spreadsheet users as they can produce unwanted results. Several powerful functions, such as SUMIF, QUERY, and SUMPRODUCT, fall short of handling hidden or filtered-out rows.

The only effective way to deal with hidden rows is by using the SUBTOTAL function and its associated function numbers.

However, the SUBTOTAL function has a limitation: it doesn’t expand automatically with an ARRAYFORMULA, which reduces its utility for functions that utilize array arguments, such as QUERY.

In our virtual helper column concept, we will of course use the SUBTOTAL function, but to expand it, we will use a Lambda helper function. Let’s dive into it.

Steps:

We have our sample data in A1:D, which contains item names, names of the salespersons, sales quantity, and sales amount in columns A, B, C, and D, respectively.

We will hide 2-3 rows and see how to omit those rows while manipulating this data using the QUERY function.

Virtual helper column formula for excluding hidden rows in Google Sheets QUERY

1. Select a Non-Blank Column:

Consider any column in the range that doesn’t contain blank cells. For example, we will consider column A here (there might be blank rows at the end, but that is not an issue).

2. Starting Point:

The data starts at row #1. So, in cell E1, enter the following formula:

=SUBTOTAL(103, A1)

This returns the count of values in cell A1, which will be 1. Next, we’ll convert it into a custom lambda function and expand it using a lambda helper function.

3. Create a Custom Formula Using LAMBDA:

First, create a custom formula using the LAMBDA function:

=LAMBDA(r, SUBTOTAL(103, r))

It might return an #N/A error in cell E1, which you can ignore. This formula follows the syntax LAMBDA([name, …], formula_expression), where ‘r’ is the name to be used in the formula expression, which is the SUBTOTAL formula.

4. Use the Custom Function with the MAP Function:

Use this custom function within the MAP lambda helper function as follows:

=MAP(A1:A, LAMBDA(r, SUBTOTAL(103, r)))

This follows the syntax: MAP(array1, [array2, …], lambda)

This formula in cell E1 will create a physical helper column in E1:E. However, when using QUERY, we will horizontally append this formula with the ‘data’ to avoid using E1:E, the physical helper column.

So the ‘data’ in the QUERY formula will be: {A1:D, MAP(A1:A, LAMBDA(r, SUBTOTAL(103, r)))}

Let’s see how to omit hidden rows in QUERY in the examples below. Before that, let me shed some light on the logic.

The Logic Behind Including Only Visible Rows in the QUERY Function

When you apply Data > Create a filter and filter out any row, manually hide any row, or hide rows by any means such as applying a Slicer or row grouping, the virtual helper column will have the value 0 in those rows. In all other rows with content in column A, the values will be 1.

So, in the QUERY function WHERE clause, we will apply a condition such as "WHERE Col5 = 1" which means the row is visible.

Note: We are using the range A1:D, which contains 4 columns, so the virtual helper column will be the fifth column.

This is the logic behind excluding hidden rows in the QUERY function in Google Sheets.

Example of Excluding Hidden Rows in the QUERY Function

Here are a couple of formulas that demonstrate how to handle hidden rows in Google Sheets QUERY:

=QUERY({A1:D, MAP(A1:A, LAMBDA(r, SUBTOTAL(103, r)))}, "SELECT Col1, Col2, Col3, Col4 WHERE Col5=1", 1)

This formula returns columns 1 to 4 where the fifth column (Col5) is equal to 1, representing visible rows.

=QUERY({A1:D, MAP(A1:A, LAMBDA(r, SUBTOTAL(103, r)))}, "SELECT Col1, SUM(Col4) WHERE Col5=1 GROUP BY Col1", 1)

This formula selects the first column (Col1) and calculates the sum of the fourth column (Col4). It includes only rows where the fifth column (Col5) is equal to 1, indicating visible rows. The results are grouped by the values in the first column.

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

4 COMMENTS

    • Hi, David Holcomb,

      Unfortunately, it’s not possible because of the SUBTOTAL.

      But we can make the process a little easier because of the availability of the FORMULATEXT function. We can use this function in another column instead of step#2 “Show formula” option.

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.