How to combine queries in Google sheets? - google-sheets

I have data that arrives as so in a sheet from a google form. It's to manage pieces of our product that we send to a painter.
in/out model_1 model_2
-------------------------
in 10 0
out 5 0
in 10 10
in 2 5
out 2 12
I want something like so
model IN OUT
-------------------------
model_1 22 7
model_2 15 12
I managed to get the first column with a query with something along those lines
SELECT SUM(B), SUM(C) WHERE A="in"
Then I added a TRANSPOSE.
How to add to the query a second column where A="out" ?
This is the real query :
=TRANSPOSE(QUERY('RĂ©ponses au formulaire 2'!A1:W; "SELECT SUM(E), SUM(F), SUM(G), SUM(H), SUM(I), SUM(J), SUM(K), SUM(L), SUM(M), SUM(N), SUM(O), SUM(P), SUM(Q), SUM(R), SUM(S), SUM(T), SUM(U), SUM(V), SUM(W) WHERE D = 'DEPOT'";1))
Hope you understand what I mean. Maybe another approach is best?
But for now I'm kinda stuck but not really happy with my solution of adding a second query as it add a column of labels sum_a sum_b....

Would this do it?
=TRANSPOSE(QUERY('RĂ©ponses au formulaire 2'!A1:W; "SELECT D,SUM(E), SUM(F), SUM(G), SUM(H), SUM(I), SUM(J), SUM(K), SUM(L), SUM(M), SUM(N), SUM(O), SUM(P), SUM(Q), SUM(R), SUM(S), SUM(T), SUM(U), SUM(V), SUM(W) WHERE D = 'DEPOT' OR D = '2nd Option' group by D";1))
Change 2nd Option with your desired value

you could try this formula:
=transpose({{B1:G1};BYCOL(FILTER(B2:G,A2:A="in"),lambda(row,sum(row)));BYCOL(FILTER(B2:G,A2:A="out"),lambda(row,sum(row)))})

Related

How to calculate a sum conditionally based on the values of two other columns

Feels like this may be a basic, but I cannot find a way to do this with sumifs.
I've got four columns in one table, representing the workload of an employee like this:
Job
Employee # 1
Employee # 2
Workload
Job 1
Bob
Jane
5
Job 2
Bob
2
Job 3
Jane
Susan
3
Job 4
Susan
2
I'd like to output total workflow results to a second sheet for each employee based on a specialized formula. In English, the forumla would be:
Calculate the total workload for each employee.
- For each job that includes that includes employee named "X" and no assigned teammate, use the job's corresponding workload value.
- For each job that includes that includes employee named "X" and has an assigned teammate, reduce the corresponding workload value by 50%.
So with the given table above, I'd want an output like this:
Employee Name
Workload
Bob
4.5
Jane
4
Susan
3.5
Math:
Bob = ((job_1 / 2) + job_2)
Jane = ((job_1 / 2) + (job_3 / 2))
Susan = ((job_3 / 2) + job_4)
Does anyone know how I can accomplish this?
Functions like sumifs seem to only let me set criteria to sum or not sum a value. But I cannot find a clear way to sum only 50% of a value based on a condition in a separate column.
=LAMBDA(name,SUMIFS(D2:D5,B2:B5,name,C2:C5,"")+SUMIFS(D2:D5,B2:B5,name,C2:C5,"<>")/2+SUMIFS(D2:D5,C2:C5,name)/2)("Bob")
The math is
SUM D, if B is Bob and C is empty and
SUM half of D if B is Bob and C is not empty and
SUM half of D, if C is Bob.
Or the same logic via query:
=QUERY(
{
QUERY(B1:D5,"Select B,sum(D) where C is null group by B");
QUERY(B1:D5,"Select C,sum(D)/2 where C is not null group by C");
QUERY(B1:D5,"Select B,sum(D)/2 where C is not null group by B")
},
"Select Col1, sum(Col2) where not Col1 contains 'Employee' group by Col1"
)
However, note that we're assuming title contains Employee and no other names contain Employee.
Employee # 1
sum sum Workload
Bob
4.5
Jane
4(Incorrect in the question)
Susan
3.5
Use this formula
=ArrayFormula({ "Employee Name", "Workload";
QUERY(
QUERY({LAMBDA(a,b,c,d,k, {b,a,{d/k};c,a,{d/k}} )
(A2:A,B2:B,C2:C,D2:D,
BYROW(B2:C, LAMBDA(c, IF(COUNTA(c)=0,,IF(COUNTA(c)=1,1,2)))))},
"Select (Col1),sum(Col3) Group by Col1" ,0), "Where Col1 <> '' ",0)})
Used formulas help
ARRAYFORMULA - QUERY - LAMBDA - BYROW - IF - COUNTA - SUM

How to compare two cells against two columns and count when both cells appear in the columns in any order

I haven't been able to find something like this. Hoping I can get an answer in Google Sheets! I have a two columns with values pulled from the same list of 5 items. (Each row is an item that has up to two 'types' that come from a 'types list' with 5 values)
For this example:
Types
A
B
C
D
E
Example Column 1
Example Column 2
A
B
B
A
B
C
B
D
E
D
B
I want to create another table like below that will count up how many times a particular combination occurs regardless of order. Results would appear as below:
Type 1
Type 2
Count
A
B
2
B
C
1
B
E
0
D
E
1
I need this formula to count any time both values appear in either column together.
Really hoping I can get this into a simple table format, because I have another table where I will need to do similar that will be more complex so I'm hoping to avoid longer form counting tables if it can be avoided.
Any help would be much appreciated!
try:
=ARRAYFORMULA(TRIM(QUERY(IF(A:A<B:B, A:A&" "&B:B, B:B&" "&A:A),
"select Col1,count(Col1) where Col1 <> ' 'group by Col1
order by count(Col1) desc label count(Col1)''")))
Use this formula
=ArrayFormula(
{ "Type1","Type 2", "Count";
SPLIT(UNIQUE(FILTER(QUERY({IF(A2:A<B2:B, A2:A&" "&B2:B, B2:B&" "&A2:A)}), LEN(QUERY({IF(A2:A<B2:B, A2:A&" "&B2:B, B2:B&" "&A2:A)}))>2)), " "),
COUNTIF(QUERY({IF(A2:A<B2:B, A2:A&" "&B2:B, B2:B&" "&A2:A)}), "="&
UNIQUE(FILTER(QUERY({IF(A2:A<B2:B, A2:A&" "&B2:B, B2:B&" "&A2:A)}), LEN(QUERY({IF(A2:A<B2:B, A2:A&" "&B2:B, B2:B&" "&A2:A)}))>2)))})

Google Sheets - How to sum columns in a query formula based off different drop down filters

I'm brand new to google sheets and have been trying for a couple days to get this to work. I currently am using a query to pull my data from the school data tab then using drop down menus to look at the data by school and in between a window of dates. I'm trying to get it so when I change the window of dates of the school that and will produce a sum of that column at the bottom of each column for the dates and school i have selected that will change with whatever I have picked in the drop downs. Following different tutorials keeps giving me the same problem, that being when I add the sum query it will add up the whole column and doesn't change based on the school selected. Would really appreciate any help
https://docs.google.com/spreadsheets/d/1Zd3nvo5EpY-8Qh_ElEv5JuiuF7vwvNsQXdpdfvt_Rmw/edit#gid=1096123326
I have edited your sheet to reflect this solution:
In School Data column W, I changed the formula in W2 to
=arrayformula(if(len('School Data'!A2:A),mmult(n('School Data'!C2:R),transpose(sign(column('School Data'!C:R)))),))
This sums up all the needed rows.
Then, I edited your QUERY formula in Sheet2 cell B11:
=query('School Data'!A1:AL999,"SELECT V, W
where V is not null
AND V >= date """&text(C6,"yyyy-mm-dd")&"""
AND V <= date """&text(C7,"yyyy-mm-dd")&"""
AND A ="""&F6&"""
LABEL W 'Total # of Phone Contacts Attempted'")
I simply replaced your SUM() selections with column W, which already takes all the sums.
Summing all the columns in the query:
={query('School Data'!A1:AL999,"SELECT V, SUM(C), SUM(D), SUM(E), SUM(F), SUM(G), SUM(H), SUM(I), SUM(J), SUM(K), SUM(L), SUM(M), SUM(N), SUM(O), SUM(P), SUM(Q), SUM(R)
where V is not null
AND V >= date """&text(C6,"yyyy-mm-dd")&"""
AND V <= date """&text(C7,"yyyy-mm-dd")&"""
AND A ="""&F6&"""GROUP BY V
LABEL SUM(C) 'Total # of Phone Contacts Attempted'");"Total",query('School Data'!A1:AL999,"SELECT SUM(C), SUM(D), SUM(E), SUM(F), SUM(G), SUM(H), SUM(I), SUM(J), SUM(K), SUM(L), SUM(M), SUM(N), SUM(O), SUM(P), SUM(Q), SUM(R)
where V is not null
AND V >= date """&text(C6,"yyyy-mm-dd")&"""
AND V <= date """&text(C7,"yyyy-mm-dd")&"""
AND A ="""&F6&"""
LABEL SUM(C) '', SUM(D) '',SUM(E) '',SUM(F) '',SUM(G) '',SUM(H) '',SUM(I) '',SUM(J) '',SUM(K) '',SUM(L) '',SUM(M) '',SUM(N) '',SUM(O) '',SUM(P) '',SUM(Q) '',SUM(R) ''")}

INDEX MACTH [Google Sheets]

I have a table with the result of a test. The table is structured as follows:
student score_description score_value
Juan test 1 5
Peter
Brian test 2 8
Jose test3 10
I need to convert this into a kind of pivot table using only formulas. For this, I thought of using the INDEX & Match but it doesn't work. At the end, I should see something like this:
student test1 test2 test3
Juan 5
Peter
Brian 8
Jose 10
Thanks a lot!
Try
=QUERY(A:C,"select A,sum(C) where A is not null group by A pivot B ")
or
=QUERY(A:C,"select A,sum(C) where B is not null group by A pivot B ")

Sum column in google excel using 2 conditions

I have a table with 4 columns in a google spreadsheet [timestamp, item, amount, category] and would like to get all rows that belong to a specific month and a specific category and sum the amount column.
So far I have tried
=QUERY(Sheet1!A:D,"SELECT SUM(C) WHERE MONTH(A) = 1 AND D = 'some text' GROUP BY A",1)
and
=IF(AND(EQ(Sheet1!D2:D,B17),MONTH(Sheet1!A2:A)=1),SUM(Sheet1!C2:C))
Where B17 is a text that I want a comparison to be made.
In Google spreadsheets, try
=sumproduct(Sheet1!D2:D=B17,MONTH(Sheet1!A2:A)=1, Sheet1!C2:C)
and see if that works?
If you'd prefer query, you can try
=QUERY(Sheet1!A:D,"SELECT SUM(C) WHERE MONTH(A) = 0 AND D = '"&B17&"' label SUM(C) ''",1)
Note that MONTH() in query is zero-indexed: so January would be month 0.
You can change this one:
=IF(AND(EQ(Sheet1!D2:D,B17),MONTH(Sheet1!A2:A)=1),SUM(Sheet1!C2:C))
with
=sum(filter(Sheet1!C2:C,(Sheet1!D2:D=B17)*(MONTH(Sheet1!A2:A)=1)))

Resources