IFS statement with multiple nested QUERY functions - google-sheets

With a fair amount of help from SO folks, I managed to put together about 15 QUERY tabs used to extract necessary data for reporting.
In order to make it tidy, I'd like to enclose all these queries in an IFS statement referring to a Data Validation cell and run the QUERY by simply changing the value from the validation cell.
I managed to get it running with no errors, however, I get a single cell output.
What I am using for this is:
=IFS('Validation'!$A$1 = "Giberish",
{QUERY('All'!$A$5:$AP,
"SELECT AJ, '" & IDBlah & "', 'Blah'
WHERE AJ IS NOT NULL
LABEL '" & IDBlah & "' 'ID',
'Blah' 'BlahBlahBlah'",1)},
'Validation'!$A$1 = "GiberishGiberish",
{QUERY('All'!$A$5:$AP,
"SELECT AJ, 'WCaa', 'Blah'
WHERE AJ IS NOT NULL
LABEL 'WCaa' 'BlahBlah',
'Blah' 'BlahBlahBlah'",1);
QUERY('Upload'!$A$2:$AL,
"SELECT A, 'WCaa', 'Blah'
WHERE A IS NOT NULL
LABEL 'WCaa' '',
'Blah' ''",0)} )
I tried to enclose it all in an ARRAYFORMULA, but I get the same single cell output, with no error.
What is the correct approach?
Let me know if you need dummy data, but have in mind that I have corporate security policies that restrict me from sharing any sheet to anyone outside of the company.

you will need to use IF instead of IFS like:
=IF(Validation!$A$1 = "Giberish", {QUERY(All!$A$5:$AP,
"SELECT AJ,'"&IDBlah&"','Blah'
WHERE AJ IS NOT NULL
LABEL '"&IDBlah&"''ID','Blah''BlahBlahBlah'",1)},
IF(Validation!$A$1 = "GiberishGiberish", {QUERY(All!$A$5:$AP,
"SELECT AJ,'WCaa','Blah'
WHERE AJ IS NOT NULL
LABEL 'WCaa''BlahBlah','Blah''BlahBlahBlah'",1);
QUERY(Upload!$A$2:$AL,
"SELECT A, 'WCaa','Blah'
WHERE A IS NOT NULL
LABEL 'WCaa''','Blah'''",0)}, ))
when IFS is in a combination of ARRAYFORMULA it gains a special skill which requires an array to be inputted in order to output an array again.
https://webapps.stackexchange.com/a/124685/186471

Related

Using Query to select column based on the headers

I have a dynamic table where columns are moved every now and then. Therefore, I would like to reference my column name in my query. Unfortunately I do not know so well and the internet raises more questions.
My formula looks like this:
`=query('X Source'!A:AP, "select D, E, AA, AM, X, A where "&if(month(now())=1,"(month(A)<11)","(month(A) <=month(now())-2)")&" and (V like 'C & G' or V like 'SAS' or V like 'SXS D' or V like 'DIR') Order By A desc")
D = Cinter
E = Cluster
AA = Creation Date
AM = Change Ow
X = Title
A = Date`
Do you have any idea ? I would like not to write a script.
I have already tried with the function filter to bypass but there I get no further because of the filtering after month.
`={FILTER('X Source'!AA:AA, 'X Source'!V:V="SAS",'X Source'!X:X<>"%BY SB%",'X Source'!X:X<>"%SB ONLY%", month('X Source'! AA:AA)=month(today())-1);FILTER('X Source'!AA:AA,'X Source'!V:V="SXS D",'X
Source'!X:X<>"%BY SB%",'X Source'!X:X<>"%SB ONLY%"`
You can define the following Named Function. (Data > Named functions > Add new function)
Name:
BETTERQUERY(range, better_query, headers)
Definition
=QUERY({range},IF(IFERROR(SPLIT(better_query,"`")=better_query,1),better_query,
REGEXREPLACE(REDUCE(better_query,REGEXEXTRACT(better_query,REGEXREPLACE(
REGEXREPLACE(better_query,"([()\[\]{}|\\^$.+*?])","\\$1"),"`(.*?)`","`($1)`")),
LAMBDA(acc,cur,SUBSTITUTE(acc,cur,IFNA("Col"&MATCH(cur,INDEX(range,1),0),cur)))),
"`(Col\d+)`","$1")),headers)
And then use it like this:
=BETTERQUERY('X Source'!A:AP',"select `Cinter`, `Cluster`, `Creation date` ...",1)
For more information on how this works see How to Use Column Names in QUERY
You could use MATCH to find the numbers of columns, and grab your range in curly brackes so you can refer them as Col1,Col2,Col3 instead of A,B,C
Just to make it more dinamic and you could change your range, I wrapped it in LAMBDA. With the headers I matched all your values and joined them with comma. 'date' only matched that colum. Column V I had my doubts about if it was a mistake when you said which columns was located at which headers. Please change "Title" in ""Col"&MATCH("Title",INDEX(range,1),0)" to the actual desired header title (that now is in V column) so it matches correctly:
=LAMBDA(range,
LAMBDA(headers,date,title,query({range}, "select "&headers&" where "&if(month(now())=1,"(month("&date&")<11)","(month("&date&") <=month(now())-2)")&" and ("&title&" like 'C & G' or "&title&" like 'SAS' or "&title&" like 'SXS D' or "&title&" like 'DIR') Order By "&date&" desc"))(
JOIN(",",INDEX("Col"&MATCH({"Cinter","Cluster","Creation Date","Change Ow","Title","Date"},INDEX(range,1),0))),
"Col"&MATCH("Date",INDEX(range,1),0),
"Col"&MATCH("Title",INDEX(range,1),0)
))
('X Source'!A:AP)
In my dummy example with random columns, the inside part of the query would look like this:
"select Col7,Col3,Col15,Col20,Col11,Col12 where (month(Col12)<11) and
(Col11 like 'C & G' or Col11 like 'SAS' or Col11 like 'SXS D' or Col11
like 'DIR') Order By Col12 desc"
UPDATE with other Locale
=LAMBDA(range; LAMBDA(headers;date;pl;query({range}; "select "&headers&" where "&if(month(now())=1;"(month("&date&")<11)";"(month("&date&") <=month(now())-2)")&" and ("&pl&" like 'C & G' or "&pl&" like 'SAS' or "&pl&" like 'SXS D' or "&pl&" like 'DIR') Order By "&date&" desc";1))( JOIN(",";INDEX("Col"&MATCH({"Cinter";"Cluster ";"Creation Date";"Change Owner";"Title";"Date"};INDEX(range;1);0))); "Col"&MATCH("Date";INDEX(range;1);0); "Col"&MATCH("PL";INDEX(range;1);0) )) ('X Source'!A1:AS)

Try to use query to vlookup and filter a set of raw data

I try to run this query:
=Query({'raw_13-19'!A:G},"Select * where C ={'Keyword'!G2:G19}", 1)
But it keeps returning:
Unable to parse query string for Function QUERY parameter 2: PARSE_ERROR: Encountered " <ID> "C "" at line 1, column 16. Was expecting one of: "(" ... "(" ...
I'm not quite sure why is this happening. Can anyone help?
Since the range is within {}, you need to use the other select notation 'Col1,Col2,Col3' instead of 'A,B,C'.
Also {'Keyword'!G2:G19} won't work, but you should use textjoin() instead.
Try:
=Query({'raw_13-19'!A:G},"Select * where Col3 matches '"&textjoin("|",1,Keyword!G2:G19)&"' ", 0)
If you have a header column on raw_13-19, then change , 0 at the end to , 1.
NOTE: the above will work with text fields, but if you have any issues, please share a mockup sheet so we can see the actual data scenario.

Arrayformula - Cell contains text (Google Sheets)

I have the formula written below. On the condition where Column E = "EPIs", there is a second condition to verify if Column G contains the string ("CC", "Hemo" or "odonto").
But that second condition is not returning correctly the 'contains the specified string' part.
It gets true for Col E = "EPIs", but false for the second cases.
How can I make it work?
=ARRAYFORMULA(
IF(B3:B="";"";
IF(M3:M="";"Verificar";
IF(M3:M="Realizado - Sem Certificado";"Verificar";
IF(M3:M="Manutenção";"Verificar";
IF(M3:M="Verificar";"Verificar";
IF(M3:M="Verificar se está ativo";"Verificar";
IF(M3:M="Não realizado";"Verificar";
IF(M3:M="Desativado";"DESATIVADO";
IF(E3:E="EPIs";
(IF(G3:G="*CC*";IF(DATE(YEAR(M3:M);MONTH(M3:M)+6;DAY(M3:M))>TODAY();IF(DATE(YEAR(M3:M);MONTH(M3:M)+4;DAY(M3:M))>TODAY();"OK";"Realizar CQ");"Vencido");
IF(G3:G="*hemo*";IF(DATE(YEAR(M3:M);MONTH(M3:M)+6;DAY(M3:M))>TODAY();IF(DATE(YEAR(M3:M);MONTH(M3:M)+4;DAY(M3:M))>TODAY();"OK";"Realizar CQ");"Vencido");
IF(G3:G="*odonto*";IF(DATE(YEAR(M3:M)+2;MONTH(M3:M);DAY(M3:M))>TODAY();IF(DATE(YEAR(M3:M)+2;MONTH(M3:M)-2;DAY(M3:M))>TODAY();"OK";"Realizar CQ");"Vencido");
IF(DATE(YEAR(M3:M)+1;MONTH(M3:M);DAY(M3:M))>TODAY();IF(DATE(YEAR(M3:M)+1;MONTH(M3:M)-2;DAY(M3:M))>TODAY();"OK";"Realizar CQ");"Vencido")
))));
IF(E3:E="Odonto Intra";IF(DATE(YEAR(M3:M)+2;MONTH(M3:M);DAY(M3:M))>TODAY();IF(DATE(YEAR(M3:M)+2;MONTH(M3:M)-2;DAY(M3:M))>TODAY();"OK";"Realizar CQ");"Vencido");
IF(DATE(YEAR(M3:M)+1;MONTH(M3:M);DAY(M3:M))>TODAY();IF(DATE(YEAR(M3:M)+1;MONTH(M3:M)-2;DAY(M3:M))>TODAY();"OK";"Realizar CQ");"Vencido") ) ))))))))))
The formula you wrote is quite a bit more convoluted than you needed it to be.
I think using EDATE() and LOOKUP() you can achieve the desired effect more easily.
=ARRAYFORMULA(IF(M3:M="";;LOOKUP(TODAY()-EDATE(M3:M;IF(REGEXMATCH(G3:G;"Hemo|CC");6;IF(REGEXMATCH(G3:G;"Odonto");24;12)));{-9^9;-60;0};{"OK";"Realizar";"Vencido"})))

How can I repeat a query with multiple columns N times with a join who only take ONE column?

I am using this:
=IFERROR(QUERY(Opgivelser!A:E; "select D,A,B where E = TRUE");"Vælg fordybelsesområder")
for this:
And it works as intended but I also want to repeat it i.e twice or based on a cells value i.e D2.
I have tried to to use this:
=ArrayFormula(TRANSPOSE(SPLIT(JOIN(","; IFERROR(REPT(IFERROR(QUERY(Opgivelser!A:E; "select D,A,B where E = TRUE"); "Vælg fordybelsesområder") & ","; D2))); ",")))
But that one prompts an error about ONE row/column.
What will I have to change? Or is there an even more simple or logical syntax for this?
try:
=ARRAYFORMULA(SPLIT(TRANSPOSE(SPLIT(REPT(QUERY(TRANSPOSE("♠"&
QUERY(TRANSPOSE("♦"&IFERROR(QUERY(Opgivelser!A:E;
"select D,A,B where E = TRUE"); "Vælg fordybelsesområder"))
;;9^9));;9^9); F1); "♠")); "♦"))

Query should exclude headers

I'm trying to get just one result from this query:
=QUERY('Registro Clinico'!A1:AA1000; "select A where(B='B12')")
where B12 contains 17.555.829-2.
I need it to return the ID of the patients, but it's returning the header name "ID".
You can exclude headers by supplying FALSE for the headers parameter. (Documentation)
=QUERY('Registro Clinico'!A1:AA1000; "select A where (E='"&B12&"')"; FALSE)
^^^^^
You had other problems as well:
The Query language does not support references into the spreadsheet in the way you had tried to write it.
"...where (B='B12')..."
Instead, you need to concatenate text segments using the & operator:
"...where (B='" & B12 & "')..."
^^^ ^^^
The column in 'Registro Clinico' that contains 17.555.829-2 is E, not B.
This should do the trick:
=QUERY('Registro Clinico'!A1:AA1000,"select A where E='" & 'Registro Clinico'!B12 & "'", FALSE)

Resources