How to write a conditional query formula? - google-sheets

I have a query in google sheets:
=QUERY('Depot Report'!D:Q, "SELECT I, J WHERE P ='"&AK4&"'")'
AK4 is a data validated list of numbers 1-40 which selects all I, J based on the value of AK4.
What I want is "all" in the list that will run the query:
=QUERY('Depot Report'!D:Q, "SELECT I, J")

Edit after your comments
The formula always work very well, but your problem in your sheet is:
Values in column P are not numbers because are formatted as text.
No problem. Solution is very easy.
SOLUTION
Select all column P and format from menu Automatic.
Also I changed formula a little for cell S12 for exactly what you need.
Only copy/paste formula now.
.
=QUERY('Depot Report'!D2:Q, "SELECT I, J WHERE 1=1"&IF(T4="All",""," AND P= "&T4&" "))
Use this formula
=QUERY('Depot Report'!D2:Q, "SELECT I, J WHERE 1=1"&IF(AK4="",""," AND P= "&AK4&" "))

Related

Query information not populating in Google Sheets

Here is a link to the doc
On sheet two cell A6 there is a query:
=QUERY('Events Summary'!1:970, "SELECT A,F,BG,M,BA,BB,BC,BD,BF,BE
WHERE F = date'"&text(Today(),"YYYY-MM-dd")&"'order by M DESC ",1)
The information for columns A, F, & M populates but the rest do not. Is this because they are out of their natural order?
it is empty because the source is empty:

Array formula in Google Sheets with filtering mechanism

I know that SUMIFS don't work with array formulas, but I hope there is a chance to work it out.
I have a formula like this, but it doesn't work:
={"Price 1"; ARRAYFORMULA(IF(ISBLANK($A2:$A),,(AG2:AG)*(SUM(IF(DB_Full_Inventory_Grouped_BUY!$A:$A=AQ2,IF(DB_Full_Inventory_Grouped_BUY!$C:$C=$A2,IF(DB_Full_Inventory_Grouped_BUY!$B:$B=$AZ2,$L2:$L)))))))}
I tried it with query inside array but it didn't work:
QUERY(DB_Full_Inventory_Grouped!$A:M,"select avg(L) where A='"&AP2:AP&"' AND B='"&$AZ2:AZ&"' AND C=date '"&TEXT(A2:A,"yyyy-mm-dd")&"'",1)
any ideas?
SOLVED:
I found here ways to join and concatenate columns working with array formulas and then ended up with something like this, where B, C, D are the columns with data, and I, J, K columns with criteria to lookup for, to sum up column E.
={"Total Units Available";
ARRAYFORMULA(
IF(ISBLANK(I2:I),,
SUMIF(
transpose(query(transpose(B2:B&C2:C&D2:D),,1)),
transpose(query(transpose(I2:I&J2:J&K2:K),,1)),
E2:E
)))
}

Google Sheet | Excel | Array Formula + CountIf + Partial Text Problem

I'm pretty new with ArrayFormula, have been trying but sometime the formula works, sometimes does not. What I'm trying to do is the combination of ArrayFormula, Countif for searching partial text.
As shown in the worksheet below, there are 10 subjects (column A), each subject has at least one of 4 samples (A,B,C,D) summarized as a string (column B). What I'm trying to do is to find which subject has sample A or B or C or D.
I have tried single formula for each sample, eg cell D3
=IF(COUNTIF($B3,"*"&$D$2&"*")>0,$A3,"")
it returns the correct results. However, when I try arrayformula in cell I3,
=arrayformula(IF(COUNTIF($B3:B,"*"&$D$2&"*")>0,$A3:A,""))
The answers are weird. For example: Subjects (Gamma, Zeta, Eta, Theta) who don't have the sample "A" are shown to have sample "A". And this applies to sample B,C,D too
Not sure what went wrong in here. Here is the link to the worksheet
I wouldn't use Countifs or an array formula. Use filter instead. Put this formula in cell i3.
=Filter(if(REGEXMATCH(B3:B,$D$2),A3:A,""),B3:B<>"")
try:
=INDEX(QUERY(IFERROR(TRIM(SPLIT(FLATTEN(IF(IFERROR(SPLIT(B3:B, ","))="",,
SPLIT(B3:B, ",")&"×"&A3:A)), "×"))),
"select max(Col2) where Col2 is not null group by Col2 pivot Col1"))
or use in row 2 if you want to sort it as in your example:
=INDEX(IFNA(VLOOKUP(A2:A, QUERY(IFERROR(TRIM(SPLIT(FLATTEN(
IF(IFERROR(SPLIT(B3:B, ","))="",,SPLIT(B3:B, ",")&"×"&A3:A)), "×"))),
"select Col2,max(Col2) where Col2 is not null group by Col2
pivot Col1 label Col2'Subjects'"), {2,3,4,5}, 0)))
You can accomplish all four columns of results with a single formula.
Delete all formulas from I3:L3.
Place the following formula into I3:
=ArrayFormula(IF(REGEXMATCH(B3:B,I2:L2),A3:A,))
In plain speech, this read "If anything in B3:B matches a value found in I2:L2, return A3:A in the matching columns(s) at the matching row(s); if not, return null."

Google Sheets query where other sheet equals

I have a workbook with two sheets. I want to query a sheet called Farming that has rows of numbers associated with different objects which I want to sum over.
I want to use query so that the two different sheets can be sorted and filtered without breaking.
I have got this far:
=query(Farming!A2:Z1000, 'select F+G+H+I+J+K+L where "Farming"!B="B7"', 0)
Where the column B on each sheet is the unique reference number for the objects.
However, I'm getting a syntax error and I'm not sure where to go from here.
Thanks for your help!
It's unclear to me whether you're getting B7 from the Farming page or the sheet the formula is on. If it's the former, the first function works. If it's the latter, the second works. These examples are based on text rather than numbers being the value in cell B7. If B7 is a number you don't have to do the quotes.
=query(Farming!A2:Z1000, "select F, G, H, I, J, K, L where B="&"'"&Farming!B7&"'", 0)
=query(Farming!A2:Z1000, "select F, G, H, I, J, K, L where B="&"'"&B7&"'", 0)
Example with the value in B1.
Try
=query(Farming!A2:Z1000, "select F+G+H+I+J+K+L where B='"&B7&"'", 0)
Remarks
The select statement in query
should be quote (") enclosed rather than single quote/apostrophe (') enclosed.
it could reference fields from the data argument by using column names (A, B, C, ) or aliases like Col1, Col2, etc. when the data isn't a reference.
String values inside the SQL statement should be single quote/apostrophe enclosed

Using QUERY with IMPORTRANGE

According to the Google Help Section I should be able to reference a column in a QUERY by using the "column identifiers ... the one or two character column letter (A, B, C, ...)" - or in this case, G.
The goal of my query is simply to pull information into a new spreadsheet from columns H, J, and K of a different spreadsheet if G is equal to a certain name; in this case, William.
My query:
=QUERY( Survey!G2:K , "select G, H, J, K where G = 'William'" )
works when I use it to call information from a sheet in the same spreadsheet. The problem arises when I try to use this QUERY with IMPORTRANGE. I have used both of these formulas:
=QUERY(IMPORTRANGE("key","'Survey!G2:K'"),"SELECT G, H, J, K WHERE G='William'")
=QUERY(IMPORTRANGE("key","'Survey!Col7:Col11'"),"SELECT Col7, Col8, Col10, Col11 WHERE Col7='William'")
and both return errors. I have included a link to the error that appears for the first QUERY The second error says the same thing, with Col7 replacing G in the text.
What should I be calling the columns in the QUERY?
Is this error due to a problem in my IMPORTRANGE overall?
Please include a link to your sheet.
The Col notation should only go inside the QUERY, not the IMPORTRANGE.
Try this:
=query(importrange("Survey!G2:K"),"Select * where Col7 = 'William'")
This may not be but shows signs of being lack of authorsation. Try IMPORTRANGE on its own and see whether you need to grant access (once off) to the 'other' spreadsheet.

Resources