Array cell reference in where clause of query - google-sheets

Google Sheets sample file:
https://docs.google.com/spreadsheets/d/1WqqiysWdYMAop5uQXZnisuX291wY2PFmAMEKZhya2IM/edit?usp=sharing
I am trying to calculate the average value of each person for each month. My two variables to match are the names and month of the date. Currently I am able to get the correct values if I am applying the formula to each cell using this formula:
=IFERROR(QUERY(Sheet2!A:C,"Select avg(C) where A = '"&A2&"' and month(B) = month(date '"&TEXT(DATE(2021,MONTH(DATEVALUE($A$1&"1")),1),"yyyy-mm-dd")&"') label avg(C) '' "),"-")
What I want to do is be able to do an arrayformula instead so I will only need to input the formula once and wont have to worry about the number of names entered. I tried using this formula:
=ARRAYFORMULA(IFERROR(QUERY(Sheet2!A:C,"Select avg(C) where A = '"&A2:A&"' and month(B) = month(date '"&TEXT(DATE(2021,MONTH(DATEVALUE($A$1&"1")),1),"yyyy-mm-dd")&"') label avg(C) '' "),"-"))
, but it only calculates the average for that row.
If possible, the solution should also be using query since in the data I will be using it on I am not able to use sumifs/averageifs for some reason.

try:
=QUERY('data source'!A2:C,
"select A,avg(C)
where month(B)+1 = "&MONTH(A1&1)&"
group by A
label avg(C)''")
=ARRAYFORMULA({SORT(UNIQUE(FILTER('data source'!A2:A, 'data source'!A2:A<>""))),
IFNA(VLOOKUP(SORT(UNIQUE(FILTER('data source'!A2:A, 'data source'!A2:A<>""))),
QUERY('data source'!A2:C, "select A,avg(C) where month(B)+1 = "&
MONTH(A1&1)&" group by A label avg(C)''"), 2, 0), "-")})

Related

Finding the first value in a row and getting the address using ArrayFormula

in this example table, I want to get the first "failed" or "passed" from result 1, 2, and 3 column. I already made a formula to get the first mentioned value using:
=IF(C2=C2,CELL("address",INDEX(D2:F2,MATCH(C2,D2:F2,0))),)
which works fine (the column result is from that formula)
But when I'm trying to use ArrayFormula on the formula, it only shows the first row value only. my ArrayFormula is like this:
=ArrayFormula(IF(C2:C4=C2:C4,CELL("address",INDEX(D2:F2,MATCH(C2:C4,D2:F2,0))),))
i think its because the INDEX and MATCH doesn't update as it goes down, any idea how to fix this?
Sheets link: https://docs.google.com/spreadsheets/d/1oFTZHGd9PKpfZ9QXWvTshorEOGFxmD1cpeeQ9bIOYh8/edit?usp=sharing
You could use a query to get the minimum column matching the value in column C for each row in D to F:
=ArrayFormula(lambda(a,address(index(a,,1),index(a,,2)))
(query(split(flatten(if(C2:C="",,if(D2:F=C2:C,row(C2:C)&"|"&column(D2:F),))),"|"),"select Col1,min(Col2) where Col2 is not null group by Col1 label min(Col2) ''")))
Or alternatively you can use a formula similar to your original one with Byrow:
=ArrayFormula(if(C2:C="",,byrow(C2:F,lambda(r,CELL("address",INDEX(r,1+MATCH(index(r,,1),index(r,,2):index(r,,columns(r),0))))))))

Query or filter based on ANY column in the source sheet matching a given cell

I'd like a formula to return all the matching values from column A if ANY of columns B-AZ equal the query value. Said differently:
=query(DATA!A:Z, "select A Where 'DATA!B:AZ' = C2").
Formulas I cobbled together, but don't work:
=query('Inv by shelf'!A:AZ,"Select A WHERE '"&C1&"' = '"&TEXTJOIN("|",1,'Inv by shelf'!$B:$AZ)&"'",1)
=filter('Inv by shelf'!A2:A,'Inv by shelf'!B:AZ = C1)
TIA!
Try FILTER() formula with MMULT().
=FILTER('Inv by shelf'!A2:A,MMULT(ArrayFormula(--('Inv by shelf'!B2:Z=C1&"")),SEQUENCE(COLUMNS('Inv by shelf'!B2:Z2),1,1,0)))
Functions references.
MMULT
SEQUENCE
FILTER
See you workbook sheet harun24hr.

How to get "0"-value instead of empty/blank in Google Sheets Query

I am currently trying to sum some numbers from another sheet in Google Sheets with the QUERY-function.
The data is from a CSV export, so I'd rather not change the data input (which is in another sheet).
I have the following query:
=QUERY(Orders!A:P;"SELECT A,C, P+C WHERE A > 1 label P+C 'Total'";1)
My problem is, that column P will either be empty or have a numeric value.
The problem is, that the "P+C" will void when P is blank.
Therefore I want the value to only return the value of "C" in the columns when P is empty - and else return P+C.
Is that even possible?
Try
=QUERY({Orders!A:O\arrayformula(if(Orders!P:P="";0;Orders!P:P))};"SELECT Col1,Col3, Col16+Col3 WHERE Col1 > 1 Label Col16+Col3 'Total' ";1)
How to get "0"-value instead of empty/blank in Google Sheets Query
multiply it by 1:
=INDEX(QUERY(Orders!A:P; "SELECT A,C,P+C WHERE A > 1 label P+C 'Total'"; 1)*1)
if you got errors from multiplication use:
=INDEX(IFERROR(QUERY(Orders!A:P; "SELECT A,C,P+C WHERE A > 1 label P+C 'Total'"; 1)*1;
QUERY(Orders!A:P; "SELECT A,C,P+C WHERE A > 1 label P+C 'Total'"; 1))

Google sheets query functions: how to do arithmetic on columns with non-constants, i.e. dividing not by a constant but by another cell?

I understand how to do arithmetic on a column using a query function as long as I'm using a fixed constant. However, if I try to do the same using a cell reference instead of a constant, I get an error.
I've tried making the cell a named range and referring to that name hoping it would act like a constant but I still get errors. There must be a way to do this. Here are examples of things that work:
=QUERY(FebMarket,"SELECT (C/5)") //This divides column C by 5
=QUERY(FebMarket,"SELECT AVG(C)") //This gives me the average of column C
=QUERY(FebMarket,"SELECT AVG(C) LABEL AVG(C) ''")
//average of col C without a header
However, if I do any of the following I get an error:
=QUERY(FebMarket,"SELECT C/(AVG(C) LABEL AVG(C) '')")
=QUERY(FebMarket,"SELECT C/(AVG(C))")
=QUERY(FebMarket,"SELECT C/AVG(C)")
=QUERY(FebMarket,"SELECT C/avgC") // where 'avgC' is a named range given to a cell where I calculated the average of column C separately
=QUERY(QUERY(C1:C,
"select C / "&AVERAGE(C1:C), 0),
"select Col1
where Col1 is not null
label Col1 '' ", 0)
As far as I know you get the same result in all dialects of SQL. You can do one of two things in a single query:
Get the result of an aggregate (like AVGE) over the entire dataset
e.g.
=QUERY(FebMarket,"SELECT AVG(C)")
in which case you can only select aggregates in the query
or
Get an aggregate for each one of a set of groups defined by one or more grouping variables.
e.g.
=QUERY(FebMarket,"SELECT GroupVariable,AVG(C) group by groupVariable")
in which case you can select grouping variables and aggregates in the query.
Neither of these help.
If you google something like 'SQL divide column by its average' you will probably get an answer using a subquery but at time of writing these are not available in Google Sheets.
So (although you could do this shorter by other means) the solution using queries would have to be
=ArrayFormula(query(A:A,"select A where A is not null")/query(A:A,"select avg(A) label avg(A) ''"))

How to importrange dynamically based on dropdown/variable

I'm trying to dynamically retrieve all rows where a status = [Dynamically chosen value] from a drop down list.
below you can see my current formula:
=query(IMPORTRANGE("https://example.com", "Tracker!A1:V"), "select * where Col7 = 'B1' ")
Here is an example where you can see what I'm trying to do (Status is A1, and A2 is the formula grabbing all rows from another sheet)
If I select the "A1" in the formula is brings up "In Progress" so it appears to be referencing the correct value, but it doesnt work unless I explicitly write the status into the formula as a text object..
As noted below:
to use cell references, you have to escape them using '"&cell&"'
QUERY(IMPORTRANGE("your_url","your_sheet!your_range"),"Select * where Col7 = '"&A1&"'",0))

Resources