Query returning #VALUE - google-sheets

My query:
=Query(importrange(B5 ,"Sheet1!A1:G700"),"select Col3 WHERE Col4 = '"&Q145&"'",QUERY(importrange(B5 ,"Sheet1!A1:G700"),"select Col2 WHERE Col3 = '"&Q145&"'", 0))
is returning:
#VALUE! Query parameter 3 expects number values`. But W106 is a text and cannot be coerced to a number.
I have the cell format set to automatic, I've also tried setting it to plain text. Kind of lost, it works without the second query statement, unfortunately, this query is used on multiple sheets with a different number of columns so this is my attempt and making this query work with both formats.

it isn't clear what exactly you are after so try:
=QUERY({IFERROR(QUERY(IMPORTRANGE(B5, "Sheet1!A1:G700"),
"select Col3 where Col4 = '"&Q145&"'", 0),"");
IFERROR(QUERY(IMPORTRANGE(B5, "Sheet1!A1:G700"),
"select Col2 where Col3 = '"&Q145&"'", 0),"")},
"where Col1 is not null", 0)

Related

How to count values if option checked is selected in one cell

I have a spreadsheet where users fill out a form, they have multiple options they can pick by clicking the checkboxes.
The responses sheets looks like this
I want to filter the results by option, so my main goal is to get the total amount of options selected:
I've been trying with COUNTIF but it's not working because I'm not selecting the specific response
=COUNTIF('Form Responses 1'!C2:F4,"*"&$B$1&"*")
I'm assuming I need to add a VLOOKUP but I'm not sure how to match it with the option
Here's a spreadsheet to play around with it
try:
=INDEX({QUERY(TRIM(SPLIT(FLATTEN(A2:A&"×"&SPLIT(B2:B, ",")), "×")),
"select Col1,count(Col1) where Col2 is not null group by Col1 pivot Col2 label Col1 'Person'");
{"Total", TRANSPOSE(MMULT(TRANSPOSE(QUERY(QUERY(TRIM(SPLIT(FLATTEN(A2:A&"×"&SPLIT(B2:B, ",")), "×")),
"select count(Col1) where Col2 is not null group by Col1 pivot Col2"), "offset 1", )*1),
SEQUENCE(COUNTUNIQUE(TRIM(FLATTEN(SPLIT(TEXTJOIN(",", 1, B2:B), ",")))), 1, 1, )))}})
Perhaps a formula like this:
=IFNA(QUERY({$A$2:$A$6, $B$2:$B$6}, "Select 1 where Col2 contains '"&B$9&"' and Col1 = '"&$A10&"' order by Col1 label 1 ''"), 0)
This outputs a 1 if the referenced cell contains the specified text, and a 0 otherwise. See the docs for the Query Function and the Query Language for more information.

Google Sheets Query return autoincrement in results

I need to return a two column table from query where the first column shows the position order and then full name. So essentially in MySQL form it would be an autoincrement but I cannot get it to work. I'm using =arrayformula(QUERY({G4:G18, arrayformula(row(G4:G18)) & ". " & H4:H18&" "&I4:I18, J4:J18}, "SELECT Col2, Col3 WHERE Col1 = 'Yes' ORDER BY Col3 ASC LABEL Col2 '', Col3 ''")) which I realize the row(G4:G18) is just going to return the row number but I've tried everything else I can think of and can't get it to work. Any help is greatly appreciated. Note: I want to keep this in query form versus filter for various reasons. thanks.
Sample sheet to see in action
I have come up with a solution but to do it, I need to have 2 queries. 1 that returns the Full name with incrementing numbers, and the other one which returns the Person ID. Please see screenshots below:
1st query(for Full names with incrementing numbers):
=INDEX(arrayformula(ifna(arrayformula(row(G4:G18)-3) & ". " & QUERY({G4:G18, H4:H18&" "&I4:I18, J4:J18}, "SELECT Col2, Col3 WHERE Col1 = 'Yes' ORDER BY Col3 ASC LABEL Col2 '', Col3 ''"), "")), 0, 1)
2nd query(for the Person ID)
=INDEX(arrayformula(QUERY({G4:G18, H4:H18&" "&I4:I18, J4:J18}, "SELECT Col2, Col3 WHERE Col1 = 'Yes' ORDER BY Col3 ASC LABEL Col2 '', Col3 ''")), 0, 2)
They are basically the same query, but I split them into two in order to concatenate an incrementing value to the Full names. I tried doing it using only 1 query but what happens is that the incrementing value will also be seen in the Person ID column(eg. 1. 2, 2. 3, 3. 5). Please let me know if this solution solves your problem.

Query Col1 > today()

When I use the format that I left below, returns in error, I know that something is missing adjusting in relation to WHERE AND... But I could not fit to supply the error.
I would like some help knowing what I missed.
"select Col1,Col2,Col3 where Col1 is not null Order by Col1, Col2 AND Col1 > date'"&TEXT(today()-1,"yyyy/mm/dd")&"'"
The date and time for Col1 and Col2 are like this:
With this formula, I hope I can filter the imported data only for those that have the date of the current day or tomorrow and that today's games are higher than the current time.
Link to spreadsheet:
https://docs.google.com/spreadsheets/d/15T4UPVtEHv43DLomKcTmdaPuGWMsBUoO7bLvlOhab4k/edit?usp=sharing
Formula set in Página1 G2
it may look like you can combine parameters as you like but there is a specific order that needs to be followed:
respectively joining operator like and needs to be proceeded by parent parameter where:
Try:
"select Col1, Col2, Col3 where Col1 >='"&TEXT(TODAY()-1,"yyyy-mm-dd")&"' and Col1 is not null Order by Col1, Col2"
This returns 78 records

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