67

I'm using FILTER to extract rows from a range, and want to take only certain columns. For example, I filter by D, but want only columns B,C in reverse order. I tried to use QUERY:

=QUERY(filter(B:D,D:D>=2), "select C,B") - Error: can't analyze query string for function QUERY parameter 2: NO_COLUMNC
=QUERY(filter(B:D,D:D>=2), "select *") - shows me all columns, so QUERY should work...

How do I QUERY the results of FILTER? Any other way to achieve this?

1

4 Answers 4

110

When you are QUERYing a computed array, you need to use the Colx notation rather than column letters:

=QUERY(FILTER(B:D,D:D>=2),"select Col2, Col1")

which incidentally can be achieved by just using the QUERY:

=QUERY(B:D,"select C, B where D >= 2")

or just FILTER:

=FILTER({C:C,B:B},D:D>=2)

Sign up to request clarification or add additional context in comments.

2 Comments

For a big sheet, is any of the last 2 solutions fastest, or is it internally the same execution ?
@arno With the disclaimer that I am simply an end user that takes a bit of interest in Sheets. I believe the answer to your second part is no, in that it seems QUERY farms the processing out to the Google Visualisation API, where FILTER is processed in the Sheets engine. Again, my opinion/best guess from the info out there. Answering the first part - anecdotally, I think QUERY has better performance for big data sets, and again I would guess this is because it uses the same API that is designed to process big data sets in other contexts.
15

There is a more straightforward way.

=CHOOSECOLS(FILTER(B:D,D:D>=2), 1,3) 

Where 1 and 3 are column indexes

1 Comment

This is a great way to make the columns dynamic. Learned something new today!
9

Although using the Query function is simple and straight-forward. There's another way to achieve this output using the Filter function

You can nest the original FILTER function inside another FILTER function and specify an array of 1's and 0's mentioning which column you need and which you don't.

=Filter( FILTER(B2:E6,D2:D6>10900) , {1,0,0,1} ) 

Comments

7

How about using arrays on the go?

=FILTER({B:B, D:D},D:D>=2)

In this way you select columns in place, and they won't change if new column would be added.

1 Comment

This is missing C

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.