Google Sheets Query to combine data from 3 sheets with different criteria - google-sheets

I need to combine 3 sets of data vertically, using a Query. The first 2 data sources ('SOURCE 1'!A:T and EG:EZ) have the same conditions and I was able to combine them with no problem, but the 3rd data source ('SOURCE 3'!A:T) has different conditions and when I add this source I result in an error. The column headers will be identical in the output.
This is the formula containing ONLY the first 2 data sources, that works:
=QUERY({'SOURCE 1'!A:T;EG:EZ},"Select Col1,Col2,Col3,Col4,Col5,Col6,Col7,Col8,Col9,Col10,Col11,Col12,Col13 where Col10 >= date '"&TEXT(B1,"yyyy-mm-dd")&"' and Col10 <= date '"&TEXT(D1,"yyyy-mm-dd")&"' and Col20 = 'YES' and Col1 starts with 'APP-' order by Col2,Col10,Col1,Col9 label Col11 'NAME'",1)
When adding in the 3rd data source, below are the 3 formulas I've tried, but all result in the error:
In ARRAY_LITERAL, an Array Literal was missing values for one or more rows.
Attempt 1:
={QUERY({'SOURCE 1'!A:T;EG:EZ},"Select Col1,Col2,Col3,Col4,Col5,Col6,Col7,Col8,Col9,Col10,Col11,Col12,Col13 where Col10 >= date '"&TEXT(B1,"yyyy-mm-dd")&"' and Col10 <= date '"&TEXT(D1,"yyyy-mm-dd")&"' and Col20 = 'YES' and Col1 starts with 'APP-' order by Col2,Col10,Col1,Col9 label Col11 'NAME'",1);
QUERY('SOURCE 3'!A:T,"Select Col1,Col2,Col3,Col4,Col5,Col6,Col7,Col8,Col9,Col10,Col11,Col12,Col13 where Col1 is not null")}
Attempt 2:
={QUERY('SOURCE 1'!A:T,"Select Col1,Col2,Col3,Col4,Col5,Col6,Col7,Col8,Col9,Col10,Col11,Col12,Col13 where Col10 >= date '"&TEXT(B1,"yyyy-mm-dd")&"' and Col10 <= date '"&TEXT(D1,"yyyy-mm-dd")&"' and Col20 = 'YES' and Col1 starts with 'APP-' order by Col2,Col10,Col1,Col9 label Col11 'NAME'",1);
QUERY(EG:EZ,"Select Col1,Col2,Col3,Col4,Col5,Col6,Col7,Col8,Col9,Col10,Col11,Col12,Col13 where Col10 >= date '"&TEXT(B1,"yyyy-mm-dd")&"' and Col10 <= date '"&TEXT(D1,"yyyy-mm-dd")&"' and Col20 = 'YES' and Col1 starts with 'APP-' order by Col2,Col10,Col1,Col9 label Col11 'NAME'");
QUERY('SOURCE 3'!A:T,"Select Col1,Col2,Col3,Col4,Col5,Col16,' ',Col8,Col9,Col10,Col11,Col12,Col13 where Col1 is not null")}
Attempt 3:
={QUERY('SOURCE 1'!A:T,"Select A,B,C,D,E,F,G,H,I,J,K,L,M where J >= date '"&TEXT(B1,"yyyy-mm-dd")&"' and J <= date '"&TEXT(D1,"yyyy-mm-dd")&"' and T = 'YES' and A starts with 'APP-' order by B,J,A,I label K 'NAME'",1);
QUERY(EG:EZ,"Select A,B,C,D,E,F,G,H,I,J,K,L,M where J >= date '"&TEXT(B1,"yyyy-mm-dd")&"' and J <= date '"&TEXT(D1,"yyyy-mm-dd")&"' and T = 'YES' and A starts with 'APP-' order by B,J,A,I");
QUERY('SOURCE 3'!A:T,"Select A,B,C,D,E,P,' ',H,I,J,K,L,M where A is not null")}
Any advice would be much appreciated. Thank you!

If you write QUERY(EG:EZ,"Select A,B,C,D,E,F,G,H,I,J,K,L,M you can't select A... within EG:EZ!
I suggest to change 'SOURCE 1'!A:T by {'SOURCE 1'!A:T} , idem for EG:EZ and then you will be able to use only Col1, Col2 etc. in your formula.
Try
={QUERY({'SOURCE 1'!A:T},"Select Col1,Col2,Col3,Col4,Col5,Col6,Col7,Col8,Col9,Col10,Col11,Col12,Col13 where Col10 >= date '"&TEXT(B1,"yyyy-mm-dd")&"' and Col10 <= date '"&TEXT(D1,"yyyy-mm-dd")&"' and Col20 = 'YES' and Col1 starts with 'APP-' order by Col2,Col10,Col1,Col9 label Col11 'NAME'",1);
QUERY({EG:EZ},"Select Col1,Col2,Col3,Col4,Col5,Col6,Col7,Col8,Col9,Col10,Col11,Col12,Col13 where Col10 >= date '"&TEXT(B1,"yyyy-mm-dd")&"' and Col10 <= date '"&TEXT(D1,"yyyy-mm-dd")&"' and Col20 = 'YES' and Col1 starts with 'APP-' order by Col2,Col10,Col1,Col9 label Col11 'NAME'");
QUERY({'SOURCE 3'!A:T},"Select Col1,Col2,Col3,Col4,Col5,Col16,' ',Col8,Col9,Col10,Col11,Col12,Col13 where Col1 is not null")}
if there is a lack of data in one of the three queries, you will also get an error, in that case, add iferror(query______,{"","","",...} on each query

Related

Query with multiple cell reference for multiple conditions with a select all option in Google Sheets

I have a data sheet 'All Data'!A2:AX. I am working on a dashboard where I want to filter the data using cell references by multiple conditions like gender, Nature of disability, Age less and greater than, and educational qualification. I have been able to achieve that through the below formula
These are the way I have tried to use drop downs used in the formula
= query(importrange("URL","'All Data'!A3:AX"),
"Select Col2, Count(Col24)
Where Col3 = '"&'WMR FY'!$B$3&"'
AND
(Col14 = '" &'WMR FY'!T3&"'
OR Col14 = '" &'WMR FY'!U3&"'
OR Col14 = '" &'WMR FY'!V3&"'
OR Col14 = '')
AND
(Col11 = '" &'WMR FY'!T2&"'
OR Col11 = '" &'WMR FY'!U2&"'
OR Col11 = '" &'WMR FY'!V2&"'
OR Col11 = '" &'WMR FY'!W2&"'
OR Col11 = '" &'WMR FY'!X2&"'
OR Col11 = '" &'WMR FY'!Y2&"'
OR Col11 = '')
AND Col24 = '"&'WMR FY'!U4&"'
AND Col12 >= "&'WMR FY'!U5&"
AND Col12 <= "&'WMR FY'!U6&"
Group by Col2
Order by Count(Col24)
Label Col2 'Center Code', Count(Col24) '"&'WMR FY'!U10&"'",0)
Here Col2 is the center code as there are multiple centers
Col24 is the nature of disability column from source data
Col11 is the Qualification
Col14 is the gender
Col24 = Nature of disability
Col12 = Age
'All data'A3:AX is the source data
With the above formula I am able to populate the data based on selections. However
what I was trying to achieve was this - If I keep one section for example cells for gender blank, it should populate all the genders in line with my other selections of qualification, nature of Disability, etc. Instead if I leave Gender blank I am getting blank cells.
I tried to use an if statement but not sure if it will do the job.
FYI : AND Col3 = '"&'WMR FY'!$B$3&"' is just taking reference of a finacial year.
Sorry if I have asked the question properly. This is the first time I am asking a question as normally I have been able to find solutions online. I am looking for a non appscript soltion.
Please let me know if you want me to create a dummy sheet and I will try to create it at the earliest.
I thought I found a solution but was not able to make it work. Heres the link to the solution I found
Google QUERY Formula to include "All" from a dropdown
try:
=QUERY(IMPORTRANGE("URL", "All Data!A3:AX"),
"select Col2,count(Col24)
where 1=1 "&
IF('WMR FY'!$B$3="",," and Col3 = '"&'WMR FY'!$B$3&"'")&
IF('WMR FY'!T3&'WMR FY'!U3&'WMR FY'!V3="",," and Col4 matches '"&TEXTJOIN("|", 1, 'WMR FY'!T3, 'WMR FY'!U3, 'WMR FY'!V3)&"'")&
IF('WMR FY'!T2&'WMR FY'!U2&'WMR FY'!V2&'WMR FY'!W2&'WMR FY'!X2&'WMR FY'!Y2="",," and Col11 matches '"&TEXTJOIN("|", 1, 'WMR FY'!T2, 'WMR FY'!U2, 'WMR FY'!V2, 'WMR FY'!W2, 'WMR FY'!X2, 'WMR FY'!Y2, "^$")&"'")&
IF('WMR FY'!U4="",," and Col24 = '"&'WMR FY'!U4&"'")&
IF('WMR FY'!U5="",," and Col12 >= "&'WMR FY'!U5)&
IF('WMR FY'!U6="",," and Col12 <= "&'WMR FY'!U6)&"
group by Col2
order by count(Col24)
label Col2'Center Code',count(Col24)'"&'WMR FY'!U10&"'", 0)

Why is this Query statement not displaying "text" formatted phone numbers" on Google Sheets?

This formula is returning eveything right,but the phone numbers, which are preceded by an apostrophe. If I happen to delete the apostrophe, it displays the numbers, but since there may be a 0 at the beginning, it has to come as text.
Here's the formula:
=iferror(QUERY('Form Responses'!$A$4:$P; "select N, B, C, D, E, F, G, H, I, J, K, L, A where N >= 1 and todate(A)=date '"&TEXT(B3;"yyyy-mm-dd")&"' order by N Asc");"Pick a date")
Here's how the data looks:
...and this is the result right now:
Here's the link to an example:
https://docs.google.com/spreadsheets/d/1vxCwfcXRrpuvmkhpRsFrKQSq5aMjkdqlUYY-6_UknuE/edit?usp=sharing
Appreciate your time/help - as usual!
try:
=INDEX(SUBSTITUTE(IFERROR(QUERY(
{'Form Responses'!A4:P\ TEXT('Form Responses'!E4:E; "\×0")};
"select Col14,Col2,Col3,Col4,Col17,Col6,Col7,Col8,Col9,Col10,Col11,Col12,Col1
where Col14 >= 1
and todate(Col1) = date '"&TEXT(B3; "yyyy-mm-dd")&"'
order by Col14 asc"); "Pick a date"); "×"; ))
update:
try:
=ARRAYFORMULA(TEXT(SUBSTITUTE(IFERROR(QUERY(
{'Form Responses'!A4:P\ TEXT('Form Responses'!E4:E; "\×0")};
"select Col14,Col2,Col3,Col4,Col17,Col6,Col7,Col8,Col9,Col10,Col11,Col12,Col1
where Col14 >= 1
and todate(Col1) = date '"&TEXT(B3; "yyyy-mm-dd")&"'
order by Col14 asc"); "Pick a date"); "×"; );
{"#"\"#"\"#"\"#"\"\0#"\"#"\"#"\"#"\"#"\"#"\"#"\"#"\"mm/dd/yyyy"}))

Google Sheets query not working when referencing a cell

I use the query function to extract data from my tables in google sheets. But one of the conditions must be a reference to another cell. I do it like this:
=QUERY({$A2:$B,ArrayFormula(Month($C$2:$C)),$D2:D},"Select SUM(Col4) Where (Col3 = '"&H2&"' ) label sum(Col4) ''",0)
The above result is #N/A. (The field H2 above contains the number 10).
If I replace the '"&H2&"' with the actual number, like below, I get output:
=QUERY({$A2:$B,ArrayFormula(Month($C$2:$C)),$D2:D},"Select SUM(Col4) Where (Col3 = 10 ) label sum(Col4) ''",0)
Any help is appreciated. Thanks in advance.
Single quotes are unnecessary in case of numbers:
=QUERY(
{
$A2:$B,
ARRAYFORMULA(Month($C$2:$C)),
$D2:D
},
"SELECT SUM(Col4)
WHERE Col3 = " & H2 & "
LABEL SUM(Col4) ''",
)
Or you could use MONTH inside QUERY:
=QUERY(
{A:D},
"SELECT SUM(Col4)
WHERE MONTH(Col3) = " & H2 - 1 & "
LABEL SUM(Col4) ''",
)
MONTH in QUERY is 0-based, so there is H2 - 1.

Sum of matrix values, conditional, with infinite rows and columns

I have 2 sheets: "Planning" and "Utilization" (example)
Planning: Employees assigned to projects for each week. Some projects are not fixed but need to be simulated (checkbox = true).
Utilization: Shows the utilization of each employee for each week. Only rows with:
a) no checkbox in Planning!A2:A
b) rows with checkbox checked
c) rows with project name in Planning!B2:B are to be considered.
I'd like to have a formula in Utilization!B2 that would calculate the sums for Utilization!B2:E4. With infinite rows and columns in Planning sheet.
try:
=QUERY(QUERY({Planning!A:H};
"select Col3,sum(Col5),sum(Col6),sum(Col7),sum(Col8)
where not Col1 = FALSE
and Col3 is not null
group by Col3"; 0);
"offset 1"; 0)
UPDATE:
=ARRAYFORMULA(QUERY(QUERY({Planning!A:Z};
"select Col3,"&
TEXTJOIN(","; 1; IF(Planning!E1:1="";;"sum(Col"&COLUMN(E:Z)&")"))&"
where not Col1 = FALSE
and Col3 is not null
group by Col3"; 0);
"offset 1"; 0))
FIX for 'get' error:
=ARRAYFORMULA(QUERY(QUERY({Planning!A:D\Planning!E:Z*1};
"select Col3,"&
TEXTJOIN(","; 1; IF(Planning!E1:1="";;"sum(Col"&COLUMN(E:Z)&")"))&"
where not Col1 = FALSE
and Col3 is not null
group by Col3"; 0);
"offset 1"; 0))
For infinite rows and Columns, I like to use OFFSET() so you might try this formula in A1 on a new tab.
=ARRAYFORMULA({QUERY(QUERY({Planning!A:D\N(OFFSET(Planning!E1;;;ROWS(Planning!E:E);COUNTA(Planning!E1:1)))};"select Col3, "&TEXTJOIN(",";TRUE;"SUM(Col"&SEQUENCE(COUNTA(Planning!E1:1);1;5)&")")&" where Col2 is not null group by Col3";0);"offset 1";0)})

Google sheets query select when certain values

I am trying to make the formula bellow work as follows: Get the values on A when B matches the values on E (for the sample it would be 1,2,4,5,7,8).
=QUERY({IMPORTRANGE("1Cg9gx2IhBUYwElNXprNJnihixbMLJcK1DKzIClQDNwc";"DATA!A2:C")};"select Col1 Where Col3 >= date '"&TEXT(TODAY;"yyyy-mm-dd")&"'and Col3 <= date '"&TEXT(TODAY;"yyyy-mm-dd")&"'and Col2="&(E1:E2)&"";0)
sample data:
A B C E
1 X 27/02/2019 X
2 Y 27/02/2019 Y
3 Z 27/02/2019
4 X 27/02/2019
5 Y 27/02/2019
6 Z 27/02/2019
7 X 27/02/2019
8 Y 27/02/2019
How can I accomplish that?
Try for the "where" clause:
where Col2='"&E3&"' or Col2 ='"&E2&"'
QUERY can't handle array/range inside quoted part
TODAY needs to be with () like TODAY()
today it's 28. february (at least where my sheet lives)
=QUERY(IMPORTRANGE("1Cg9gx2IhBUYwElNXprNJnihixbMLJcK1DKzIClQDNwc"; "DATA!A2:C");
"select Col1
where Col3 >= date '"&TEXT(TODAY(); "yyyy-MM-dd")&"'
and Col3 <= date '"&TEXT(TODAY(); "yyyy-MM-dd")&"'
and (Col2 = '"&E1&"'
or Col2 = '"&E2&"')"; 0)
or try like this if you have array/range of values:
=QUERY(FILTER(IMPORTRANGE("1Cg9gx2IhBUYwElNXprNJnihixbMLJcK1DKzIClQDNwc"; "DATA!A2:C100");
COUNTIF(E1:E100; "="&
IMPORTRANGE("1Cg9gx2IhBUYwElNXprNJnihixbMLJcK1DKzIClQDNwc"; "DATA!B2:B100")));
"select Col1
where Col3 >= date '"&TEXT(TODAY(); "yyyy-MM-dd")&"'
and Col3 <= date '"&TEXT(TODAY(); "yyyy-MM-dd")&"'"; 0)

Resources