How to use several conditions with QUERY + IMPORTRANGE in google sheets? - google-sheets

I'm trying to get unique values by using this formula:
=UNIQUE(QUERY(IMPORTRANGE("https://docs.google.com/spreadsheets/here goes link","Income!2:8000"),"Select Col15,Col16, Col26 where Col3=" & "'" & B2 & "'"))
And I'm getting the result that I need. The only problem is that I'd like to add an additional condition to it and have no idea how to do it.
I'd like this formula to filter values not only based on Col3 but based on another one, for example, Col3 = B2 and Col2 = A2.
Does anyone have any idea how to do it?
Thank you in advance!

Update: based on the sheet and the data format you have shared, this formula should work:
=UNIQUE(QUERY(IMPORTRANGE("https://docs.google.com/spreadsheets/d/1pXFIWzeHcJPQJtjUqOdgi2FusNGEUoeGlA5rLGNuKLQ/edit#gid=0","data!2:500"),"Select Col15, Col16, Col26 where Col3 = '"&B2&"' and Col2 = date'"&TEXT(DATEVALUE(A2), "yyyy-mm-dd")&"'")
You can use and with where to check for multiple conditions.
=UNIQUE(QUERY(IMPORTRANGE("https://docs.google.com/spreadsheets/here goes link","Income!2:8000"),"Select Col15, Col16, Col26 where Col3 = '"&B2&"' and Col2 = '"&A2&"'"))

Related

Query Importrange does not return any output

quick question, please
I want to do a query import range formula in Google Sheets with three criteria:
1: E-mail
2: Number of the week (e.g: 2)
3: The data column
Ive already allowed the sheets to share information but I'm getting a "Query completed with an empty output." message, even thought the cell has an imput.
Formula: =QUERY(IMPORTRANGE("linkofthesheet";"Indicadores!A9:L123");" SELECT Col3 WHERE Col1 = '"&B4&"' AND Col2 = '"&C10&"'")
I was trying before to use a vlookup together with it but a friend told me it would be redundant. Please, what do you guys think it could be the issue?
2: Number of the week (e.g: 2)
If cell C10 contains a number, you should leave the 'quotes' out, like this:
=query( importrange("...put sheet id here..."; "Indicadores!A9:L123"); "select Col3 where Col1 = '" & B4 & "' and Col2 = " & C10, 0)

Problem with range inside QUERY for rebuilding multicolumn table to Col1(key),Col2(values from all other columns)

I want to rebuild my table of Col1 (key), Col2, Col3, Col4, Col5 to table of Col1 (Key), and Col2 (which include all accordance values from Columns 2,3,4,5...), please check example:
https://docs.google.com/spreadsheets/d/1je3uc1DHitzzsFK6Xag_ld531p7ozeheO4PwpCfZjQA
If I use the following plain text as range in Query:
=QUERY({A2:A7\B2:B7;A2:A7\C2:C7;A2:A7\D2:D7;A2:A7\E2:E7},"Select * where Col2 >0")
it works well. But I can't make that range to be dynamic inside query, I tried to put range (based on formulas or even as a text) in some cell and then pull it as follows
=QUERY(A1,"Select * where Col >0")
or
=QUERY({indirect(A1)},"Select * where Col2 >0")
but it doesn't work.
You can use this formula where you will need to change only one range in case there will be more columns:
=SORT(SPLIT(QUERY(FLATTEN(IF(B3:E = "",, A3:A & "♥" & B3:E)), "WHERE Col1 IS NOT NULL"), "♥"))
Or you could place A3:A and B3:E ranges in some cells as parameters and use INDIRECT you you need that:
=SORT(SPLIT(QUERY(FLATTEN(IF(INDIRECT(N4) = "",, INDIRECT(N3) & "♥" & INDIRECT(N4))), "WHERE Col1 IS NOT NULL"), "♥"))
When a raw data set may expand, I always recommend that the raw data set remain by itself in a sheet. For now, let's assume that you have only your existing raw data set in the 'Sample' sheet.
In a new blank sheet, place this formula:
=ArrayFormula(QUERY(SPLIT(FLATTEN(FILTER(Sample!A3:A,Sample!A3:A<>"")&"|"&FILTER(FILTER(INDIRECT("Sample!B3:"&ROWS(Sample!A:A)),Sample!A3:A<>""),Sample!B2:2<>"")),"|"),"Select * WHERE Col2 Is Not Null",0))
This formula is similar to what #kishkin offered. However, the FILTER inclusions automatically expand to include all parts of the raw data where there is something in Column A and a header added in B3:B. That is, this formula requires zero maintenance if set up as I've described.

Google Sheets IMPORTRANGE and QUERY

I'm new to Sheets and the query function as of last night, so don't know the terminology etc - sorry!
I have two workbooks, a master and a template. I need the templete to pull specific data from the master when Col2 matches a particular number. I have it working great if i manually input the number, but don't want to have to do this everytime I give a copy of the template to a new job. This number in Col2 is unique to each of the templates I give to a new job, and the number will already be manually input into cell J16 of the template. I want the code to pull the number from J16 and search rather than me telling it what the number is everytime. The code I have that works with the manual input number, in this case 4032 is below. Thanks in advance for any help.
Vince.
=QUERY(IMPORTRANGE("1yTp3SajbKovHf_Wjx7CHO9FsIPeQGvmdMytQIkr9gog","Invoices"),"select Col1, Col5, Col11 where Col2 = 4032")
Do you want to get the number (any) from J16 into your query?
Use:
=QUERY(IMPORTRANGE("file_id","Invoices"),"select Col1, Col5, Col11 where Col2 = " & J16)
or better:
=QUERY(IMPORTRANGE("file_id","Invoices"),a1)
where a1 is:
="select Col1, Col5, Col11 where Col2 = " & J16

How to use ARRAYFORMULA in query where clause?

In cell C1:C of my table I got 6 rows with ticket id's. I like to search different spreadsheets to search for those ticket id's and calculate the total hours spent on that ticket.
I have it working using the following formula:
=QUERY({IMPORTRANGE("SPREADSHEETID";"B3:B")\ARRAYFORMULA(TO_PURE_NUMBER(IMPORTRANGE("SPREADSHEETID";"F3:F")-IMPORTRANGE("SPREADSHEETID";"E3:E")))};"SELECT SUM(Col2) WHERE Col1 = '"&C1&"' GROUP BY Col1 LABEL SUM(Col2) ''")
In this example, C1 is where the ticket ID can be found.
Now I thought I could just wrap QUERY in a ARRAYFORMULA and use C1:C instead of just C1 but that won't work. Now I could just copy and paste the above formula in every cell but there must be a cleaner way.
ANSWER
I used the following formula to make it work, thanks to Max's answer below.
=QUERY(
{
IMPORTRANGE("SPREADSHEETID";"B3:B")\
ARRAYFORMULA(
TO_PURE_NUMBER(
IMPORTRANGE("SPREADSHEETID";"F3:F") - IMPORTRANGE("SPREADSHEETID";"E3:E")
)
)
};
"
SELECT Col1, SUM(Col2)
WHERE Col1 = '" & JOIN("' OR Col1 = '";FILTER(C:C; C:C <> "")) & "'
GROUP BY Col1
LABEL SUM(Col2) ''
")
Sample formula is:
=QUERY({A:B},"select * where Col1 = '"&JOIN("' or Col1 = '",FILTER(D2:D,D2:D<>""))&"'")
No, one cannot create an array of query strings and use arrayformula(query(...)) to run them all at once.
Alternative: instead of
SELECT SUM(Col2) WHERE Col1 = '"&C1&"' GROUP BY Col1 LABEL SUM(Col2) ''
use the query
SELECT Col1, SUM(Col2) GROUP BY Col1
elsewhere on the sheet, and then use vlookup to look up the sum for each value of Col1 that you want. vlookup can be used inside of arrayformula like this:
=arrayformula(vlookup(C1:C10, E:F, 2, 0))
looks up each of values in C1..C10 in the column E (exact match required) and returns the corresponding value in column F (2nd column of the searched range).

COUNTIF with more than one matching string in Google Spreadsheets

I'm wondering how I can use COUNTIF with more than one matching string in Google Spreadsheets.
The following is the important part, I would like to count in the spreadsheet those records that contain "BAIRRO NOVO" and "Externo". These strings appear in different columns, not in the same cell.
=COUNTIF(IMPORTRANGE("10OAEb2fBfvAqCdp1yyuTBQ4NErtxtOyJ29whFkvVqaw";"Data!B:P");"BAIRRO NOVO")
This could be done with countifs but only if the data was placed in the sheet: i.e., importrange(...) is done and then countifs refers to its columns like
=countifs(C:C, "BAIRRO NOVO", G:G, "Externo")
To get this count without putting all the data in the current spreadsheet, use query
=query(importrange(...), "select count(Col1) where Col2 = 'BAIRRO NOVO' and Col7 = 'Externo'", 1)
Here Col1, Col2, ... are columns of the imported range; in your case Col2 is C, for example, because the range begins with B. The last parameter "1" is the number of header rows the queried range has: typically 0 or 1.
To make sure the output is a single cell, without a header like "count(something)", add an empty label as follows.
=query(importrange(...), "select count(Col1) where Col2 = 'BAIRRO NOVO' and Col7 = 'Externo' label count(Col1) ''", 1)
To have the query output 0 instead of #N/A when nothing is found, wrap it in iferror(..., 0):
=iferror(query(importrange(...), "select count(Col1) where Col2 = 'BAIRRO NOVO' and Col7 = 'Externo' label count(Col1) ''", 1), 0)

Resources