Pull in month value dynamically from another cell using query() - google-sheets

I have two sheets of data, the second sheet is "DATA".
The primary sheet I'm using, I have a =query() to query the other sheet based on a month number. From my research, I found this - https://www.benlcollins.com/spreadsheets/query-dates/ - which by itself, does work, but within query(), doesn't work.
Data:
1/1/18 2/1/18 3/1/18 4/1/18
Broken function:
=IFERROR(QUERY(DATA!$A:$L, "Select sum(G) WHERE B contains lower('"&$A4&"') AND MONTH(A)+1='"&TEXT(DATEVALUE(C1),"m")&"' label sum(G) ''"),"$0")
Working function:
=IFERROR(QUERY(DATA!$A:$L, "Select sum(G) WHERE B contains lower('"&$A4&"') AND MONTH(A)+1=4 label sum(G) ''"),"$0")
Problem...is that I want it to be dynamic.
When I use
'"&TEXT(DATEVALUE("1/1/2000"),"yyyy-mm-dd")&"'
as a function, it renders as a "4" which is what the query() is looking for.

What about something like
=IFERROR(QUERY(DATA!$A:$L, "Select sum(G) WHERE B contains lower('"&$A4&"') AND MONTH(A)+1= "&MONTH(C1)&" label sum(G) ''"),"$0")

Related

Embed text from other sheets inside QUERY()

=QUERY(IMPORTRANGE(Data),"SELECT Col1,Col2,Col3,Col4")
I have a simple formula of the format above. The data inside importrange() is in another Google Sheets file.
However, within this particular file I have the QUERY() formula in, I would like to refer to cells in another sheet and use these to create columns inside query.
Before the Col1 inside QUERY() above, I need two separate columns:
PO, Supplier
Where PO is (in normal formula format): "PO-"&Control!A6&"-"&Control!A5 (this is basically text string "PO-" combined with Control cell A6, text string "-" and Control cell A5)
and supplier is simply Control!A6.
How can I amend these two so they are columns within QUERY()?
example could be:
=QUERY(IMPORTRANGE(Data),
"select Col1,Col2,Col3,Col4
where Col1 = 'PO-"&Control!A6&"-"&Control!A5&"'
and Col2 = '"&Control!A6&"'", )

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

=Query Funtion to Pull data from Another Sheet based on cell reference

i am looking for some help to pull the data in Sheets based on another sheet called "ServicesOffered"
here is the sheet link
The idea is simple: If Cell contains Yes then it should add data to WebDev sheet or SEO sheet.
Tried many different formulas but failed to understand how to do this.
Need to have a column list or * after the Select; also shouldn't have curly brackets round the range or else it will treat it as an array and expect Col1, Col2, Col3 etc. in the Select instead of A, B, C etc.
=QUERY(
ServicesOffered!A2:C,
"SELECT * WHERE C = 'Yes' ",1)

IMPORTRANGE with condition

Using Google Sheets I want, within the same document, to import data from one sheet to another using IMPORTRANGE with conditions.
I have tried unsuccessfully:
=IF(IMPORTRANGE("https:URL","Inc Database!B2:B300")="permanent",IMPORTRANGE("htps://URL","Inc Database!A2:A300"),"")
and
=QUERY(IMPORTRANGE("https:/URL", "Inc Database!A2:A300"),"SELECT Col1 WHERE Col1 <> 'permanent'")
and
=FILTER(IMPORTRANGE("URL","Inc Database!A1:A250"),IMPORTRANGE("URL","Inc Database!B1:B250"="venture permanent"))
I want the function to say: Import any values from range A that meet criterion "permanent" in range B.
A | B
_________|_________
Name |type
---------|-------
Henry |Permanent
William |Intern
John |Permanent
I have put a few examples in the following spreadsheet:
e.g. =QUERY(IMPORTRANGE("https://docs.google.com/spreadsheets/d/1LX7JfbGvgBTfmXsYZz0u5J7K0O6oHJSnBowKQJGa9lY/edit#gid=0", "Inc Database!A2:B300"),"SELECT Col1 WHERE not(Col2 = 'Permanent') ")
You need a single quote around the reference to the sheet/tab since there is a space in the name. Using your example:
IMPORTRANGE("https:/URL", "'Inc Database'!A2:A300")
But this will only import column A, so you cannot check against column B
Then use the Query. If you want everything where B is 'Permanent' then you want (untested):
=QUERY(IMPORTRANGE("https:/URL", "'Inc Database'!A2:B"),"SELECT Col1 WHERE Col1 = 'Permanent'")
This will:
Import all of the rows, starting at A2 from the main data sheet to use in the Query().
Via Query, return only those where Col2 (B) contains 'Permanent'

Using sum within google spreadsheet query results in the word "sum" in many cells

I am trying to get the some of all values in row B that contain a certain value in row A. Pretty simple problem I guess.
Here is my query:
=QUERY('Sheet1'!$A$16:D, "Select sum(D) Where C contains '"&C5&"' ", -1)
But what that gives me is the actual word "sum" in all the fields where C contains the value.
So I get a lot of "sum" in almost all my rows.
Did the "sum" statement change for queries in google spreadsheets?
It looks like you are using more than one query formula: apparently there is a column with query, each referring to a cell such as C5. In this case there is no room for column label "sum" that the formula wants to insert: the output must be a single cell. Solution: change the column label to empty string with label sum(D) ''.
=QUERY('Sheet1'!$A$16:D, "Select sum(D) Where C contains '"&C5&"' label sum(D) ''", -1)

Resources