Making an ArrayFormula from QUERY - google-sheets

How do I convert this:
=if(ISNA(sum(QUERY(importrange("1QNYU1Kb1rtx-n4tMRamHJuvYt_9kGMfhGZZs_opuJr0","Transfer!C$2:E"),"SELECT Col3 WHERE Col1 = '" & Sheet1!B$2:B & "' ",0))),"0",sum(QUERY(importrange("1QNYU1Kb1rtx-n4tMRamHJuvYt_9kGMfhGZZs_opuJr0","Transfer!C$2:E"),"SELECT Col3 WHERE Col1 = '" & Sheet1!B$2:B & "' ",0)))
into the array formula in Google Sheets?

in my opinion, it would be pretty much an overkill if it would be possible. instead I recon to do this:
use side columns (or create a new sheet in PRC Records spreadsheet) and paste this formula there:
=QUERY({Transfer!A1:H}, "select Col3, sum(Col5)
where Col3 is not null
group by Col3
label sum(Col5)'FG Qty'", 1)
and then use this formula in J2 cell over on the MO Listing spreadsheet:
=ARRAYFORMULA(IF(LEN(B2:B), IFERROR(VLOOKUP(B2:B,
IMPORTRANGE("1QNYU1Kb1rtx-n4tMRamHJuvYt_9kGMfhGZZs_opuJr0",
"Transfer!L2:M"), 2, 0), 0), ))

Related

Google Sheets - Using INDIRECT (or alternative) within a query

I am currently using this formula:
=SORT(QUERY(G:Z, "select K,count(K) where K is not null group by K label count(K)''"),2,TRUE)
I have to replace all instances of "K" with a column letter that is written in cell B2, so I can quickly change the entire formula by simply entering the letter in the cell.
Any advice?
Thanks
Use string concatenation:
=SORT(QUERY(E:K, "select " & D1 &" ,count(" & D1 &") where " & D1 &" is not null group by " & D1 &" label count(" & D1 &")''"),2,TRUE)
as already suggested here use:
=INDEX(QUERY({INDIRECT(
ADDRESS(1, MATCH(B1, CHAR(ROW(A65:A99)), 0), 4)&":"&
ADDRESS(100000, MATCH(B1, CHAR(ROW(A65:A99)), 0), 4))},
"select Col1,count(Col1) where Col1 is not null
group by Col1 label count(Col1)''"))
demo sheet

Query Formula using Google Sheets - count data from multiple columns and return results in a single column

I am trying to calculate a count of how many times a course is listed in different columns and return the count in a list of all the courses found in those columns.
I have a spreadsheet that shows the start of my formula...
query(UNIQUE({C2:C21; F2:F21;I2:I21}),"select C,F,I, count(C), count(F), count(I) where B is not null group by C,F,I")
Clear cells A23, A24 and B23 and then try in A23
=query({C2:C21; F2:F21;I2:I21},"select Col1, count(Col1) where Col1 <> '' group by Col1 label Col1 'Fall Courses', Count(Col1) 'Count'")
and see if that helps?
If it does, you can repeat the same logic for 'winter' and 'spring'.
Alternatively, you can also try this single formula
=query(ArrayFormula(split(transpose(split(textjoin("~", true, regexreplace(C1:K1, "(Winter|Spring|Fall)", "_$1")&"_"&C2:K21), "~")), "_")), "Select Col3, Count(Col3) where Col3 <>'' group by Col3 pivot Col2", 0)

How to use several conditions with QUERY + IMPORTRANGE in 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&"'"))

Multiple IMPORTRANGE

I have a column (A1:A) with URLs of the Sheets I want to IMPORTRANGE. The problem is it's open end and I can't just do
={IMPORTRANGE(A1,"Sheet1!A1:A");IMPORTRANGE(A2,"Sheet1!A1:A")}
as the list of URLs is open end (indefinite). Since I cannot use ARRAYFORMULA with IMPORTRANGE, I was wondering if there is an alternative to it. I'd also like to have a URL next to IMPORTRANGE data, so I know what URL the data shown came from. Maybe someone has a clue.
https://docs.google.com/spreadsheets/d/1YhLJpy_p39pDvTKfjyOKQMN0W_TDz95E9XsmtOJV-GQ
Maybe it would be better to use script for that. IMPORTRAGE not working inside ARRAYFORMULA is usually solved with importing using scripts.
In case of formulas with import failure tolerance and source markings here are a few solutions.
Solution 1 - many QUERY, same number of IMPORTRANGE
Try this:
=QUERY(
ARRAYFORMULA(
{
IFERROR(QUERY(IMPORTRANGE(A2; B2); "SELECT '" & A2 & "', Col1 WHERE Col1 IS NOT NULL"); {""\ ""});
IFERROR(QUERY(IMPORTRANGE(A3; B3); "SELECT '" & A3 & "', Col1 WHERE Col1 IS NOT NULL"); {""\ ""});
IFERROR(QUERY(IMPORTRANGE(A4; B4); "SELECT '" & A4 & "', Col1 WHERE Col1 IS NOT NULL"); {""\ ""})
}
);
"WHERE Col2 IS NOT NULL
LABEL Col1 'Source',
Col2 'Imported Data'"
)
Solution 2 - one QUERY, twice the IMPORTRANGE
You can use IFERROR in case import will go wrong.
Here is a possible solution for you (IFERROR and source URLs) (one formula in E1):
=QUERY(
ARRAYFORMULA(
{
IFERROR({IF(SEQUENCE(ROWS(IMPORTRANGE(A2; B2))); A2)\ IMPORTRANGE(A2; B2)}; {""\ ""});
IFERROR({IF(SEQUENCE(ROWS(IMPORTRANGE(A3; B3))); A3)\ IMPORTRANGE(A3; B3)}; {""\ ""});
IFERROR({IF(SEQUENCE(ROWS(IMPORTRANGE(A4; B4))); A4)\ IMPORTRANGE(A4; B4)}; {""\ ""})
}
);
"WHERE Col2 IS NOT NULL
LABEL Col1 'Source',
Col2 'Imported Data'"
)

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

Resources