QUERY: sum two columns and order by one in Google Sheets - google-sheets

I have the following data set:
Country Points Bonus_Points
---------------------------------------
United States 1 50
Brazil 3 50
France 7 30
United States 2 25
And now would like to query that data set and create a list with the following outcome where bonus points and points get summed up and the list is getting ordered by bonus points.
Country Points Bonus_Points
---------------------------------------
United States 3 75
Brazil 3 50
France 7 30
With
=QUERY(A1:C,"select A, B, sum(C) group by A, B order by sum(C) desc", 1)
I'm able to sum one column but somehow unable to manage to get the sum for B/points as well.
Any ideas?

See if this helps
=QUERY(A1:C,"select A, sum(B), sum(C) group by A order by sum(C) desc label sum(B) 'Points', sum(C) 'Bonus_Points'", 1)

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

Stock Balance Inventory ArrayFormula in Google Sheets

How to create an Inventory Stock Balance for each row in Sheets with the help of array-formulas.
Sample:
Item
Operation
Quantity
Balance (Formula)
a
buy
500
500
b
buy
100
100
a
sell
110
390
a
buy
1000
1390
a
sell
800
590
b
buy
200
300
a
sell
390
200
b
sell
50
250
b
sell
150
100
The desired result is the balance for each row, the balance for the current stock item.
Try this formula-
=INDEX(BYROW(A2:A,LAMBDA(x,IF(x="",,SUM(FILTER(C2:C,A2:A=x,B2:B="buy",ROW(A2:A)<=ROW(x))))))-BYROW(A2:A,LAMBDA(x,IF(x="",,SUM(IFERROR(FILTER(C2:C,A2:A=x,B2:B="sell",ROW(A2:A)<=ROW(X))),0)))))
If you want to omit zero 0 values then use-
=BYROW(A2:A,LAMBDA(z,IF(z="",,INDEX(BYROW(A2:A,LAMBDA(x,IF(x="",,SUM(FILTER(C2:C,A2:A=x,B2:B="buy",ROW(A2:A)<=ROW(x))))))-BYROW(A2:A,LAMBDA(x,IF(x="",,SUM(IFERROR(FILTER(C2:C,A2:A=x,B2:B="sell",ROW(A2:A)<=ROW(X))),0)))),ROW(z)-1))))
Please see πŸ‘€sample file.
You can use the new functions:
LAMBDA + SCAN + BYROW
Here's the function to get inventory for all items as a string:
=SCAN("",lambda(item,op,s,mbuy, INDEX(if(item="",,SUBSTITUTE(item," ","_")&"🦊"& IF(op=mbuy,1,-1)*s)))(A4:A12,B4:B12,C4:C12,"buy"),LAMBDA(ini,s,if(s="",ini,
SUBSTITUTE(QUERY(BYROW(QUERY({if(ini="",{"",""}, INDEX(SPLIT(TRANSPOSE(SPLIT(ini,"🐺")),"🦊"))) ; split(s,"🦊")},"select Col1, sum(Col2) group by Col1 label sum(Col2) ''"),LAMBDA(a,join("🦊",a)&"🐺")),,2^99)," ",""))))
The output is:
a🦊500🐺
a🦊500🐺b🦊100🐺
a🦊390🐺b🦊100🐺
a🦊1390🐺b🦊100🐺
a🦊590🐺b🦊100🐺
a🦊590🐺b🦊300🐺
a🦊200🐺b🦊300🐺
a🦊200🐺b🦊250🐺
a🦊200🐺b🦊100🐺
It is a string representation of the balance
Next step is to count the balanse for adjacent item:
=LAMBDA(items,operations,quantities,buy_key,BYROW({items,SCAN("",lambda(item,op,s,mbuy, INDEX(if(item="",,SUBSTITUTE(item," ","_")&"🦊"& IF(op=mbuy,1,-1)*s)))(items,operations,quantities,buy_key),LAMBDA(ini,s,if(s="",ini,
SUBSTITUTE(QUERY(BYROW(QUERY({if(ini="",{"",""}, INDEX(SPLIT(TRANSPOSE(SPLIT(ini,"🐺")),"🦊"))) ; split(s,"🦊")},"select Col1, sum(Col2) group by Col1 label sum(Col2) ''"),LAMBDA(a,join("🦊",a)&"🐺")),,2^99)," ",""))))},LAMBDA(r,if(index(r,1)="",,VLOOKUP(substitute(index(r,1)," ","_") , if(index(r,2)="",{"",""}, INDEX(SPLIT(TRANSPOSE(SPLIT(index(r,2),"🐺")),"🦊"))) , 2, )))))(A4:A12,B4:B12,C4:C12,"buy")
The parameters are in the end. They are:
items,
operations,
quantities,
buy_key
buy_key is text "buy" in our case.
You can also build a Named Funtion:
The formula will show tips like this:
Notes
Please think of it as a great opportunity and I'm sure we'll be also able to calculate other comlicated things like FIFO/LIFO prices for each item.

How to get unique values from a pair of columns in Google Sheets?

I have a table in Google Sheets with a structure similar to this:
Player
Hometown
Points
Amy
Mapleton
25
Amy
Mapleton
50
Amy
Mapleton
150
Bret
Jersey
10
Colin
Hocksfield
5
Colin
Hocksfield
15
David
Mapleton
200
Ella
Benning
70
Fred
Hocksfield
20
George
Jersey
50
Harold
Dover
30
Except there's about 330 unique hometown values.
I'm trying to get a count of the hometowns that have the most number of players, which would look something like this based on the data above:
Hometown
Players
Mapleton
2
Hocksfield
2
Jersey
2
Benning
1
Dover
1
(There are only 2 players from Mapleton, even though there are 4 "point records" for Mapleton.)
Can somebody help me with a query that would return how many players there are in each hometown, that I can use in a Google Sheet?
You may use QUERY() function like-
=QUERY(UNIQUE(A2:B),
"select Col2, count(Col1)
where Col2 is not null
group by Col2
order by count(Col1) DESC
label Col2 'Hometown', count(Col1) 'Playes'")
Use this
=ArrayFormula({
$B$1,$A$1; UNIQUE(B2:B), IF(UNIQUE(B2:B)="",,
COUNTIF(QUERY(UNIQUE($A$2:$B)," Select Col2"),"="&UNIQUE(B2:B)))})

How to SUM rows filtered by multiple criteria

Google Sheets question.
I have the following sheet (named "x") containing expenses:
Date Sum Category
1/Jan/2017 100 red
2/Jan/2017 200 blue
3/Jan/2017 10 red
4/Jan/2017 20 blue
1/Feb/17 1 red
2/Feb/17 2 blue
I need to compute monthly totals, per category:
Month Red Blue
Jan/17 110 220
Feb/17 1 2
My current solution is to place in each result cell something like:
=SUM(IFERROR(FILTER(x!$B:$B, MONTH(x!$A:$A)=MONTH($A2), x!$C:$C="red")))
I am asking if there is a better way. I want to have a single result formula working over an array (maybe an ArrayFormula?!) instead of placing and customizing my formula in each cell.
Any ideas?! Thanks!
Well this is the basic idea of using pivot tables but not quite there because I can only get the month as a number so far
query(A:C,"select month(A)+1,sum(B) where C<>'' group by month(A) pivot C label month(A)+1 'Month Number'")
EDIT
The month number can be changed to a month name using an array formula and VLOOKUP but the formula is getting a bit long (my table of month numbers and names is in columns I & J)
=arrayformula({vlookup(query(query(A:C,"select month(A)+1,sum(B) where C<>'' group by month(A) pivot C label month(A)+1 'Month Number'"),"select Col1"),I:J,2,false)})
and you've still got to pick up the other two columns - this is the whole thing
=arrayformula({vlookup(query(query(A:C,"select month(A)+1,sum(B) where C<>'' group by month(A) pivot C label month(A)+1 'Month Number'"),"select Col1"),I:J,2,false),query(query(A:C,"select month(A)+1,sum(B) where C<>'' group by month(A) pivot C label month(A)+1 'Month Number'"),"select Col2,Col3")})

Google Sheets filter formula get specific columns, plus between two dates

I am building somewhat of an invoice report tool where I would like to pull all of Client X's orders between the dates set in A1 and A2, but I only need 6 of the 35 columns.
So far I am able to use the =filter formula with ease but I get all the columns of course, and I have no idea how to only filter based on the dates in A1 and A2.
If my sample data is below, I want to be able to pull all records for Client 1, between the dates set in A1 and A2 and only show Col1, Col3, and Col 6 in my "report"
Row Col1 Col2 Col3 Col4 Col5 Col6
1 Client 1 $45.00 01/15/2016 123 Main Street 404-989-9999 John
2 Client 1 $75.00 01/17/2016 123 Main Street 404-989-9999 John
3 Client 3 $15.00 01/18/2016 456 Park Street 404-989-9999 Sue
4 Client 4 $35.00 01/18/2016 111 East Street 404-989-9999 Chris
5 Client 5 $95.00 01/19/2016 789 North Street 404-989-9999 Tim
6 Client 3 $65.00 01/20/2016 456 Park Street 404-989-9999 Sue
7 Client 3 $25.00 01/21/2016 456 Park Street 404-989-9999 Sue
8 Client 1 $45.00 01/22/2016 123 Main Street 404-989-9999 John
One way to achieve that would be the use of query()
=query(A2:F, "select A, C, F where A='Client 1' and C >= date'"&text(A1, "yyyy-mm-dd")&"' and C <= date '"&text(B1, "yyyy-mm-dd")&"' " ,0)
Change ranges to suit.
Another way (with filter) would look something like this:
=filter({A2:A, C2:C, F2:F}, A2:A="Client 1", C2:C >= A1, C2:C <= B1)
I hope that helps ?

Resources