Google sheets query select when certain values - google-sheets

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)

Related

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

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

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"}))

How to sum with weeknum condition in google sheets

i have data and want to sum it with weeknum in every name condition, but i can't do that it always error parse or empty value.
this my query
=query(A1:D8,"select sum(D) where D='"&G2&"' and B='"&H1&"' label sum(D)'' ")
=query(A1:D8,"select sum(D) where D='"&G2&"' and weeknum(A)=weeknum('"&H1&"') label sum(D)'' ")
this my link
https://docs.google.com/spreadsheets/d/1AHnu-IrFQuh7scbtoQriB_7frhvLUYfO9gIX2yqiXBY/edit?usp=sharing
use:
=QUERY(A:D, "select C,sum(D) where B = "&H1&" group by C label sum(D)''", 0)
if you want 0 values use:
=ARRAYFORMULA({UNIQUE(FILTER(C2:C, C2:C<>"")),
IFNA(VLOOKUP(UNIQUE(FILTER(C2:C, C2:C<>"")),
QUERY(A:D, "select C,sum(D) where B = "&H1&" group by C label sum(D)''", 0), 2, 0), 0)})

QUERY returns 0

I have a main data sheet (Main) with several columns, the relevant ones being column A, a date (displayed as dd.mm.YYYY) and column D, a numeric value. In a second sheet, I'm using the query function to build the sum over all these numeric values for a specific month using the following expression:
=SUM(QUERY(Main!$A$1:$D$2364; "SELECT D WHERE MONTH(A) = 1"; 1))
MONTH(A) returns a number between 0 and 11 and the above expression works perfectly well for January (0) to October (9). However, as soon as I try to compare MONTH(A)
with a number > 9, the sum shows zero despite there being numerous entries in both November and December.
What I get is this:
=SUM(QUERY(...; "SELECT D WHERE MONTH(A) = 10"; 1)) equals =SUM(QUERY(...; "SELECT D WHERE MONTH(A) = 1"; 1))
and
=SUM(QUERY(...; "SELECT D WHERE MONTH(A) = '10'"; 1)) equals 0
Can anyone help me out with this one?
try:
=SUM(IFERROR(QUERY(Main!A2:D; "SELECT D WHERE MONTH(A) = 10"; 0)))
or:
=SUMPRODUCT(IFERROR(QUERY(Main!A2:D; "SELECT D WHERE MONTH(A) = 10"; 0)))
or:
=ARRAYFORMULA(SUM(IFERROR(QUERY(Main!A2:D; "SELECT D WHERE MONTH(A) = 10"; 0)*1)))

How to make 1-100 score out of two datasets in Google Sheets?

I have two datasets:
1) The number of days passed since the product was released
2) Total Number of Positive Reviews
What I am trying to archive is a 1-100 score, since the product that was released one year ago and has +1000 positive reviews is not the same with a product that was released a 1-2 month ago and already has +300 positive reviews. In a nutshell, depending on "days passed till now" - the value of "total amount of positive reviews" should drop giving 1-100 rating to filter out the most valuable products.
I'm not even sure how to approach this problem, please help me out.
There would be around 1000 products that I'd need to score, so I thought there might be a predefined formula for this in Google Sheets.
Example: https://docs.google.com/spreadsheets/d/1Y3q7URy5s8B5x3HVIzjaDzIZN3FRRvoduJAXobw1UOk/edit#gid=1382498268
this will filter out all products with:
1000+ reviews if older than 356 days
300+ reviews if not older than 60 days
={A1:D1; QUERY(
{IFERROR(QUERY(A2:D, "select * where D >= 1000 and C > 365", 0), {"","","",""});
IFERROR(QUERY(A2:D, "select * where D >= 300 and C < 60 ", 0), {"","","",""})},
"select * order by Col4 desc", 0)}
if you still want that "score" column use this:
=ARRAYFORMULA(IFERROR(VLOOKUP(A2:A,
{{QUERY(A2:D, "select A where C >= 365 order by D desc", 0),
ROW(INDIRECT("A1:A"&COUNTA(
QUERY(A2:D, "select A where C >= 365 order by D", 0))))};
{QUERY(A2:D, "select A where C < 365 and C > 0 order by D desc", 0),
ROW(INDIRECT("A1:A"&COUNTA(
QUERY(A2:D, "select A where C < 365 and C > 0 order by D", 0))))}},
2, 0), ))
if you want no more then 100 use:
=ARRAYFORMULA(IF(IFERROR(VLOOKUP(A2:A,
{{QUERY(A2:D, "select A where C >= 365 order by D desc", 0),
ROW(INDIRECT("A1:A"&COUNTA(
QUERY(A2:D, "select A where C >= 365 order by D", 0))))};
{QUERY(A2:D, "select A where C < 365 and C > 0 order by D desc", 0),
ROW(INDIRECT("A1:A"&COUNTA(
QUERY(A2:D, "select A where C < 365 and C > 0 order by D", 0))))}},
2, 0), )>100, ,
IFERROR(VLOOKUP(A2:A,
{{QUERY(A2:D, "select A where C >= 365 order by D desc", 0),
ROW(INDIRECT("A1:A"&COUNTA(
QUERY(A2:D, "select A where C >= 365 order by D", 0))))};
{QUERY(A2:D, "select A where C < 365 and C > 0 order by D desc", 0),
ROW(INDIRECT("A1:A"&COUNTA(
QUERY(A2:D, "select A where C < 365 and C > 0 order by D", 0))))}},
2, 0), )))

Resources