How to sum with weeknum condition in google sheets - 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)})

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)

Google Sheets, Unsure as why I'm getting this error both quires work

={ QUERY(B2:D41, "SELECT B,C,D WHERE D <> ''", 0); QUERY(B42:B54, "SELECT B WHERE B <> ''",0)}
"In ARRAY_LITERAL, An Array Literal was missing values for one or more rows"
As I mentioned both quires work, and when I alter the first the be
=QUERY(B2:D41, "SELECT B,C,D WHERE C <> ''", 0)
where is doesn't have a value to output I get #VALUE! then the 2nd query output.
Thank you for any help.
When you combine two arrays vertically using an { array expression }, both arrays must be the same width.
Similarly, when you combine two arrays horizontally using an { array expression }, both arrays must be the same height.
To avoid the issue, use this pattern:
=arrayformula(
{
query(B2:D41, "where D <> '' ", 0);
query( { B42:B54, iferror(C42:D54 / 0) }, "where Col1 <> '' ", 0)
}
)
the first query has 3 columns and the second query has 1 column - that's the reason why you getting that array error because you are trying to stack 3-columned array on top of 1-columned array
if your intention was to put it next to each other formula should be:
={QUERY(B2:D41, "SELECT B,C,D WHERE D <> ''", 0),
QUERY(B42:B81, "SELECT B WHERE B <> ''", 0)}
if your intention was to put it under each other formula should be:
={QUERY(B2:D41, "SELECT B,C,D WHERE D <> ''", 0);
QUERY(B42:B54, "SELECT B,' ',' ' WHERE B <> '' label ' ''', ' '''", 0)}

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

I want to use this query in an arrayformula

=iferror(QUERY(importorders!A:H,“Select count(A) where C = ‘Thailand Tour’ and month(H) = “&MONTH(A3)-1&“and year(H) = “&year(A3)&” label count(A) ‘’“,1),0)
It’s basically just counting to see how many orders I had in each month.
https://docs.google.com/spreadsheets/d/1Of6cdFYaOzCFwPdZ4ABItD6dghMjHhafRWmDJWaznbg/edit#gid=711075203
use this in B3 cell:
=ARRAYFORMULA(IFNA(VLOOKUP(A3:A, QUERY({importorders!A2:C,
EOMONTH(importorders!H2:H, -1)+1},
"select Col4,count(Col1)
where Col3 = 'Thailand Tour'
and Col4 is not null
group by Col4
label count(Col1)''", 0), 2, 0), 0))
Try
=query(A2:H,"select Year(H), Month(H)+1, C, Count(H) where C is not null and C like 'Thailand%' group by Year(H), Month(H)+1,C label C'Tour',Year(H)'Year',Month(H)+1'Month'")
This data applied to a pivot table might provide a more digestible analysis.
Report Extract

Resources