How to use ARRAYFORMULA in query where clause? - google-sheets

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

Related

Max Sum of row within date range - Google Sheets

I am using Google Sheets.
I have names in first column, dates in first row, and numbers for each name/date.
I want to return the name and sum of numbers for the person who's sum of numbers is the largest (within date range.)
A cell formula for this would be better than a script.
It sounds straight forward but am going in circles and would appreciate help. Thanks Newman
Thank you.
try:
=INDEX(QUERY(SPLIT(FLATTEN(A3:A&"♦"&FILTER(B3:F, B2:F2>="2021-10-07"*1, B2:F2<="2021-10-13"*1)), "♦"),
"select Col1,sum(Col2) where Col2 is not null group by Col1 order by sum(Col2) desc label sum(Col2)''"))
and for just 1 record:
=INDEX(QUERY(QUERY(SPLIT(FLATTEN(A3:A&"♦"&FILTER(B3:F, B2:F2>="2021-10-07"*1, B2:F2<="2021-10-13"*1)), "♦"),
"select Col1,sum(Col2) where Col2 is not null group by Col1 order by sum(Col2) desc label sum(Col2)''"), "limit 1", 0)

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)

I want to sum value of of third column based on other two columns value in google sheets and col1 and col2 contains multiple duplicate values

https://docs.google.com/spreadsheets/d/1_MeiySJHI8OD84BPDOj_z57My-TXbs2ey4AOkQP3zug/edit?usp=sharing
I am sharing the link of sheet on which I want solution on that sheet also I have tried to explain you the question.
I have three columns I want sum of third col3 value based on first two column value
if col1 = 2 and col2 = 40; Then I should get the sum of all value in col3 in which col1 = 2 and col2 = 40
if the col1 contains value of 2 and col2 contains 40 than sum of col3 value only those col3 value in which col1=2 and col2=40
so if any of the column value changes than sum of col3 also sum according to that.
use:
={"result"; INDEX(IF(COUNTIFS(A2:A&" "&B2:B, A2:A&" "&B2:B, ROW(A2:A), "<="&ROW(A2:A))=1,
IFNA(VLOOKUP(A2:A&" "&B2:B,
QUERY({A2:A&" "&B2:B, C2:C},
"select Col1,sum(Col2) where Col1 is not null group by Col1 label sum(Col2)''"), 2, 0)), ))}
In cell K4 I entered
={"Result Col"; ArrayFormula(if(len(G5:G),if(match(G5:G&H5:H,G5:G&H5:H,0)+4=row(G5:G),sumif(G5:G&H5:H,G5:G&H5:H,I5:I),),))}
I used the same formula (with different range) in E1 (for what I assume to be your 'real' data).
See if that helps?
The simpliest solution is to use a pivot table, see inside your spreadsheet. If you want the total inside the original data, and take advantage of this simple solution, you can then recall the total by GETPIVOTDATA

Google Sheets Formula to Count Occurrences of Unique Values in Columns

I'm looking for a formula, =query possibly, that will turn this:
Into this:
I used the following formula successfully to get the AFTER picture, but I can't get the counts. I tried using concatenate but there isn't a way to do that to each item in the array. Maybe there is a way to do this using a query?
=transpose(split(join(",",unique(C1:C98)),","))
And here's my flavour of it, which also needs to be dragged to the right, to fill the other columns:
=ARRAYFORMULA(QUERY(
{UNIQUE(A$2:A) & " - " & COUNTIF(A$2:A,UNIQUE(A$2:A))},
"where Col1<>' - 0'",0))
Yes, you can use query to get the unique values and counts then join the two columns
=ArrayFormula(index(query(A2:A,"select A,count(A) where A is not null group by A label count(A) ''"),0,1)&"-"&index(query(A2:A,"select A,count(A) where A is not null group by A label count(A) ''"),0,2))
Apologies, in order to drag it across you would have to put:
=ArrayFormula(index(query({A2:A},"select Col1,count(Col1) where Col1 is not null group by Col1 label count(Col1) ''"),0,1)
&"-"&index(query({A2:A},"select Col1,count(Col1) where Col1 is not null group by Col1 label count(Col1) ''"),0,2))
Try the following in cell A2 and drag to the right.
=ARRAY_CONSTRAIN(ArrayFormula(IFERROR(CONCAT(UNIQUE(A2:A99)&" - ",
ArrayFormula(COUNTIFS(A2:A99,UNIQUE(A2:A99)))))),COUNTUNIQUE(A2:A99),1)
Also using a query
=ArrayFormula(index(query({A2:A},"select Col1,count(Col1) where Col1 is not null group by Col1 label count(Col1) ''"),0,1)&"-"&
index(query({A2:A},"select Col1,count(Col1) where Col1 is not null group by Col1 label count(Col1) ''"),0,2))
The only difference is that using the first formula you maintain the names within the order they appear, while using the query they get sorted alphabetically.
Functions used:
ARRAY_CONSTRAIN
ArrayFormula
IFERROR
CONCAT
UNIQUE
COUNTIFS
COUNTUNIQUE

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