QUERY returns 0 - google-sheets

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

Related

Arrayformula to replace sumifs based on criteria - grouping data into categories/types

In this spreadsheet
Cols G and H give the total of each Account Type, from the data (A1:D)
If sum Dr - sum Cr > 0, then only this is shown in col G.
=if(sumifs(C:C,A:A,F2)-sumifs(D:D,A:A,F2)>0, sumifs(C:C,A:A,F2)-sumifs(D:D,A:A,F2),"")
If sum Cr - sum Dr > 0, then only this is shown in col H
=if(sumifs(D:D,A:A,F2)-sumifs(C:C,A:A,F2)>0, sumifs(D:D,A:A,F2)-sumifs(C:C,A:A,F2),"")
I am looking for an array formula which replaces these summifs formula, so that if either new Account Types (e.g. Type E, Type F etc) are added or new rows of data are added, then the formula would automatically calculate the sum Dr or sum Cr, instead of having to copy the formula down
try this in cell G2:
=BYROW(F2:F,LAMBDA(fx,IF(fx="",,LAMBDA(cx,dx,{IF(cx-dx>0,cx-dx,),IF(dx-cx>0,dx-cx,)})(SUMIF(A:A,fx,C:C),SUMIF(A:A,fx,D:D)))))
-
Use query(), like this:
=arrayformula(
lambda(
aggregated,
lambda(
account, balance,
{
"Account Type", "Dr", "Cr";
account,
if( balance >= 0, balance, iferror(1/0) ),
if( balance < 0, -balance, iferror(1/0) )
}
)(
query(aggregated, "select Col1", 0),
query(aggregated, "select Col2", 0)
)
)(
query(
A3:D,
"select A, sum(C) - sum(D)
where A is not null
group by A
label sum(C) - sum(D) '' ",
0
)
)
)
You can set a QUERY that finds both entire columns like this:
=QUERY(A2:D,"SELECT A,SUM(C)-SUM(D),SUM(D)-SUM(C) where A is not null group by A",1)
And check with LAMBDA if there are negative values and change them to null:
=LAMBDA(a,INDEX(IF(a<0,,a)))(QUERY(A2:D,"SELECT A,SUM(C)-SUM(D),SUM(D)-SUM(C) WHERE A is not null group by A",1))
If the values in your result table are just for display, you can use the QUERY format clause to hide negative values, such that the whole thing can be generated within a single QUERY:
=QUERY(A2:D,"select A,sum(C)-sum(D),sum(D)-sum(C) where A is not null group by A label sum(C)-sum(D) 'Dr',sum(D)-sum(C) 'Cr' format sum(C)-sum(D) '0;',sum(D)-sum(C) '0;'",1)

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

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