Summing query results based on another column in Google Spreadsheets - google-sheets

Hi I have this sample sheet: https://docs.google.com/spreadsheets/d/1TzKlEAF5IcZehmyyRUQ5ELE63Q-ZVfp9Nfx5-zvKAOc/edit?usp=sharing
I want to pull the data from the left chart. Then I want to sum Same companies revenues. Then I want to put that data dynamically to the right part. I also want to be able to check how much revenue I got from each company each month. I also want to see top 5 companies who made purchase in each month.
So for example: If Company A made 3 purchases in January I want to see sum of these purchases next to company name and sort companies from highest money to lowest for each month.
I wrote a function for that but it doesn't work.

I found the answer and wanted to share here in case anyone would need in the future. This answer belongs to Greg from Google Docs community. I will copy paste the exact answer here:
Hello, See sheet GE.Sheet1. I have added the formulas to demonstrate
your requests.
I want to pull the data from the left chart. Then I want to sum
Same companies revenues. Then I want to put that data dynamically to
the right part.
=QUERY(A:C,"select B, sum(C) where B is not null group by B", 1)
=QUERY(A:C,"select B, A, sum(C) where B is not null group by B, A", 1)
I also want to be able to check how much revenue I got from each
company each month. I also want to see the top 5 companies who made
a purchase in each month.
=QUERY(A:C,"select B, sum(C) where A = '" & M1 & "' and B is not null group by B, A order by sum(C) desc label sum(C) ''", 0)
A limit clause can be added but there is not enough data to test
against. Need more than 5 clients and multiple months.
bests,
GregE

Related

Automatically Combine Duplicate Value and Rank

In Google Sheets, I am trying to rank a table of people to find out who contributes the highest numbers.
Sometimes one person contributes multiple times so I need to sum them up before ranking.
The issue is the table will be updated with new names every few hours so I don't want to use SUMIF and manually add those new names. Is there a formula to automate that process? Thanks!
try in E2:
=INDEX(QUERY(A2:B;
"select A,sum(B)
where B is not null
group by A
order by sum(B) desc
label sum(B)''");; 1)

Get the Effective Price base on Date in Google Sheet

I have a problem getting the right "Price" for a product based on Effectivity date. I've checked on StackOverFlow but on Google Sheet is not mentioned
I need a table of Effective Price table after querying Price change history table
Please help me solve this problem
Thanks
Try for chosen codes
=query(A:C,"select B,C where A='"&E3&"' and C<=DATE'"&TEXT($E$1,"yyyy-MM-dd")&"' order by C desc limit 1",0)
other solution, by one formula for all codes at once
=arrayformula(iferror(vlookup(unique(A2:A),query(A2:C,"select A,B,C where C<=DATE'"&TEXT($E$1,"yyyy-MM-dd")&"' order by C desc ",0),{1,2,3},0)))
for all codes at once, including those that are out of bounds
=sort(arrayformula(iferror(vlookup(unique(A2:A),
{query(A2:C,"select A,B,C where C<=DATE'"&TEXT($D$1,"yyyy-MM-dd")&"' order by C desc ",0);
query(A2:C,"select A,' ','-' where A is not null ",0)},
{1,2,3},0))))
Formula in E3
=QUERY(A:C,"select A,max(C) where C <= date '"&TEXT(E1,"yyyy-MM-dd")&"' and A is not null group by A label A '',max(C) ''",1)
Formula in G3
=ArrayFormula(VLOOKUP(E3:E&F3:F,{A:A&C:C,B:B},2,FALSE))
If you want to have the same sequence of columns as yours, you need 3 formulas.

How to use data from drop down menus and two other criteria to populate a table?

I am trying to set up a spreadsheet to summarise the amounts of money I transfer between four bank accounts. In the attached spreadsheet, I will manually put in the transfers I make between the different accounts (i.e., Everyday, Holiday, Emergency, Investments) in columns A, B, C, and D. In column A, you double-click a cell to add in a date, while columns B and C use a drop down box to choose accounts.
What I would like to do is have the information in these four columns populate the table on the right.
I've named columns B and C as "OUT" and "IN", respectively. For example, in my sheet, the second row means that on the 8th April, I'd taken $500 OUT of the Everyday account and put it IN the Holiday account.
So for the table on the right, I'd like for it to give me the totals for all the money I've transferred IN and OUT for each specific account for each specific month. Do you know what formulae I could use to do this if it's possible?
An example of my spreadsheet is here: https://docs.google.com/spreadsheets/d/1azNw6m7f3x7WhwEP9WXolCzfTE9dccj9wjs0qPwD-FY/edit#gid=2056599600
I have tried to use VLOOKUP and SUMIFS.
paste this into H3 cell and drag to the right - then drag down:
=IFERROR(SUM(QUERY({$A:$D},
"select Col4
where Col"&IFS(H$2="OUT", 2, H$2="IN", 3)&"='"&IF(H$1="", G$1, H$1)&"'
and month(Col1)+1="&MONTH($G3&1), 0)))
and for totals row paste this in H15 cell and drag to the right:
=SUM(H3:H14)
demo spreadsheet

Getting a meaningful report from data

I'm fairly new to Google Sheets and know very basic about the functions. I'm trying to create a sheet to keep record of buy/sell of different items and then generate a report where i can see the profit and remaining item in stock , avg buy/sell price etc. Here's a link to sample sheet and what I've tried.
Please let me know if it can be achieved, if yes what I need to look for, any sample or link to learn in proper direction to achieve what I'm trying to get. Attaching screen shot to give a quick glimpse.
Query I used :-
=QUERY(A2:F9,"select A,B,Sum(C),Sum(E) group by A,B Label Sum(C) 'Total Quantity',Sum(E) 'Total Price'")
Thank you.
PS. I have it posted earlier in google group as well. I'll link it back there if I come to any solution.
I couldn't find a simple and neat way of achieving the layout that you wanted, but you can use Index to pull out a row and column from your query based on the row and column containing the formula like this (for the first column)
=if(int(rows($1:3)/3)<=countunique($A$2:$A$9),choose(mod(rows($1:3),3)+1,index(QUERY($A$2:$F$9,"select A,B,Sum(C),Sum(E) group by A,B Label Sum(C) 'Total Quantity',Sum(E) 'Total Price'") ,int(rows($1:3)/3)*2,1),"","Total"),"")
Then it works out whether it is the first, second or third row of a group in the report and uses the Choose function to put in the item name, a blank cell or the word "Total".
The second column works in the same sort of way
=if(int(rows($1:3)/3)<=countunique($A$2:$A$9),if(mod(rows($1:3),3)<2,index(QUERY($A$2:$F$9,"select A,B,Sum(C),Sum(E) group by A,B Label Sum(C) 'Total Quantity',Sum(E) 'Total Price'") ,int(rows($1:3)/3)*2+mod(rows($1:3),3),2),""),"")
The first Mod function is used to work out if it should show "b" or "s" from the query, or a blank, and the second Mod function works out whether it should be the "b" or "s" row of the query.
The third column is also similar
=if(int(rows($1:3)/3)<=countunique($A$2:$A$9),if(mod(rows($1:3),3)<2,index(QUERY($A$2:$F$9,"select A,B,Sum(C),Sum(E) group by A,B Label Sum(C) 'Total Quantity',Sum(E) 'Total Price'") ,int(rows($1:3)/3)*2+mod(rows($1:3),3),3),L11-L12),"")
The first Mod function is used to work out if it should show the quantity from the query, or the difference of the two previous rows, and the second Mod function works out whether it should be the "b" or "s" row of the query.
The fourth column is almost identical to the second one.
=if(int(rows($1:3)/3)<=countunique($A$2:$A$9),if(mod(rows($1:3),3)<2,index(QUERY($A$2:$F$9,"select A,B,Sum(C),Sum(E) group by A,B Label Sum(C) 'Total Quantity',Sum(E) 'Total Price'") ,int(rows($1:3)/3)*2+mod(rows($1:3),3),4),""),"")
(I couldn't work out how the Actual Profit should be calculated).

How to count # lates per student name?

sample attendance (google) sheet
I want count the number of total lates per student name on this sample sheet, e.g, how many lates Mary Love has (actual sheet has over 30,000 rows, 10 columns). If possible, this count needs to change as students have additional lates. I'd really appreciate any help someone might be able to provide. I thank you very much in advance.
In addition to previous post, a QUERY() function also seems a good option as it can output a 2D-array, containing the names and the counts in one formula (no need to drag down). As an example, try:
=query(A:G, "select A, count(B) where B ='Late' group by A ", 1)
If you want to limit the result tho those who had more then 3 late's, you can do:
=query(query(A:G, "select A, count(B) where B ='Late' group by A ", 1), "where Col2 > 3")
Also further filtering with date can be done very easily. However I think that may require some 'cleaning up' of the current data: I noticed a lot of different date formats in col D... :-)
For counting each of several students and to allow greater versatility I suggest a pivot table with Student Name for Rows, Late for Values with Summarise by: COUNTA and Late for Filter with Show: Late.
You can use the COUNTIFS function to get what you are looking for. This formula will count all matches for multiple criteria ranges and criteria (e.g. student name & late).
This is the syntax for the formula:
COUNTIFS(criteria_range1, criteria1, [criteria_range2, criteria2]…)
If you put Mary Love in cell I2, you could then put this in J2.
=countifs(B:B,"=late", A:A,I2)
Then as more rows are added it will automatically update the "lates". I would also suggest using named ranges, then you could replace B:B and A:A with the named range.
I would suggest using the =query function as documented below. If that doesn't meet your needs you could modify the above:
=if (countifs(B:B,"=Late", A:A,I2, D:D, ">="&N2) >= 3, "True", "False")
This will put "True" in the column if there are 3 or more lates, false if not. This will require you put the date you want to check against in column N2. Change N2 above if you want to use a different column.

Resources