Insert daily totals in data table - google-sheets

My data table looks like this:
Date
Fruits
Inventory
Rotted
Jan 1
Apple
100
1
Jan 1
Kiwi
100
2
Jan 1
Pear
100
1
Jan 2
Apple
90
5
Jan 2
Kiwi
80
3
Jan 2
Pear
70
1
Jan 3
Apple
80
5
Jan 3
Kiwi
70
3
Jan 3
Pear
50
1
The goal: Track total pieces of fruit I have over time.
Problem: I'm getting the data fed directly from a CSV on a server to my Google Sheet and I want to count the total pieces of fruit for each day on the Google Sheet by adding a new row each day that says "all fruit".
How can I insert daily totals in the data table like shown below?
Date
Fruits
Inventory
Rotted
Jan 1
Apple
100
1
Jan 1
Kiwi
100
2
Jan 1
Pear
100
1
Jan 1
TOTAL
300
4
Jan 2
Apple
90
5
Jan 2
Kiwi
80
3
Jan 2
Pear
70
1
Jan 2
TOTAL
240
8
Jan 3
Apple
80
5
Jan 3
Kiwi
70
3
Jan 3
Pear
50
1
Jan 3
TOTAL
200
8
I am currently fetching the data table to a Google Sheet with this formula:
=IMPORTDATA("https://xxx.xxx/fruits.csv")
Is there a way I can automatically add a "all fruits total" row to the dataset on Google Sheet daily?

You can get a mini report through Insert > New sheet and this formula in cell A1 of the new sheet:
=query(
importdata("https://xxx.xxx/fruits.csv"),
"select Col1, sum(Col3), sum(Col4)
where Col1 is not null
group by Col1",
1
)
To get full report that includes all data and the totals, try this:
=query(
{
importdata("https://xxx.xxx/fruits.csv");
query(
importdata("https://xxx.xxx/fruits.csv"),
"select Col1, 'TOTAL', sum(Col3), sum(Col4)
where Col1 is not null
group by Col1
label Col1 '', 'TOTAL' '', sum(Col3) '', sum(Col4) '' ",
1
)
},
"order by Col1",
1
)

Related

Add cell on different sheet when the adjacent is equal to another cell

I have 2 sheet2 on the same file:
the first collect the answers of different trial judge and the second should make the total
Like this:
First sheet
Name q1 q2 q3 total judge_id
Bob 1 5 8 14 1
Jeff 2 4 2 8 1
Bob 3 1 4 8 2
Bob 5 3 2 10 3
Jeff 6 1 8 15 3
Second sheet
judge_id 1 2 3 tot
Bob 14 8 15 37
Jeff 8 # 15 23
How can I sum only the row on a 'person' in particolar?
There is a fast way to do it without open Google Script?
try:
=ARRAYFORMULA({QUERY({A2:F},
"select Col1,sum(Col5) where Col1 is not null group by Col1
pivot Col6 label Col1'judge_id'"), {"tot"; MMULT(QUERY(QUERY({A2:F},
"select sum(Col5) where Col1 is not null group by Col1 pivot Col6"),
"offset 1", )*1, SEQUENCE(COUNTUNIQUE(F2:F), 1, 1, ))}})
Here's my take on it:
={
QUERY(A1:F,"SELECT A, SUM(E) WHERE A IS NOT NULL GROUP BY A PIVOT F LABEL A 'Judge ID'"),
QUERY(A1:F,"SELECT SUM(E) WHERE A IS NOT NULL GROUP BY A")
}

sum the numbers in the second Column according to data in the first column

A B C
1 Timestamp Hours Worked Total Hours
2 1/2/2022 17:33:41 6
3 1/3/2022 19:59:41 2 January =
4 1/7/2022 13:20:51 1
5 1/9/2022 12:49:02 3 February =
6 1/15/2022 12:04:21 3
7 2/16/2022 15:58:10 9 March =
8 2/22/2022 11:57:31 3
9 2/24/2022 5:45:12 5
10 2/2/2022 17:33:41 4
11 2/3/2022 19:59:41 2
12 2/7/2022 13:20:51 1
13 3/9/2022 12:49:02 3
14 3/15/2022 12:04:28 3
15 3/16/2022 15:58:10 7
16 3/22/2022 11:57:31 3
17 3/24/2022 5:45:12 5
I would like to sum the hours by date/month. I can sum the hours by
column, =sum(b2:b). But how do I sum the hours in Column B by
date/month of column A?
In another post, a member referenced the 'Google Sheets function list'.
I have found that to be very helpful on other things I have been doing.
But I am still at a lose for this problem.
John
You could use the query function with SQL:
=QUERY({(data)}, "select sum(Col2) group by Col1 label Col1 'Date', Col2 'Sum'")
That would return a new table, and you can adjust it by changing the second parameter, representing the SQL string. You can view Google's docs for the query function here. In addition you can find their docs for the query language here.
The simplest solution, without any formula, is to build a pivot table.
use:
=ARRAYFORMULA(REGEXREPLACE(TO_TEXT(QUERY({TEXT(A2:A, "yyymm\×mmmm"), B2:B},
"select Col1,sum(Col2) where Col2 is not null group by Col1 label sum(Col2)''")), "(.*×)", ))

Calculate average from a list with duplicate entries

I have 2 google sheets I'm working off. The master and a copy to create a 'dashboard' for analytics.
Master Sheet
name
quantity
price/quantity
RozMo
10
1.75
Tam
3
3.65
Gurba
36
12
Tam
30
0.55
RozMo
25
0.75
RozMo
5
0.50
RozMo
2
0.35
Gurba
150
8.75
Dashboard Sheet - Desired Output
name
quantity
price/quantity
RozMo
42
0.939
Tam
33
0.831
Gurba
186
9.379
Dashboard Sheet - This is how far I've got
name
quantity
price/quantity
RozMo
42
Tam
33
Gurba
186
Formulae used
To get the unique names
=UNIQUE('Master Sheet'!$A$2:$A)
To get quantity
=SUMIFS('Master Sheet'!$B$2:$B,'Master Sheet'!$A$2:$A,A2)
How do I populate the third column?
See how this works for you (I cannot test it, since you did not provide access to the spreadsheet):
=ArrayFormula(QUERY({'Master Sheet'!A2:C,'Master Sheet'!B2:B*'Master Sheet'!C2:C},"Select Col1, SUM(Col2), SUM(Col4)/SUM(Col2) WHERE Col1 Is Not Null GROUP BY Col1 LABEL Col1 'name', SUM(Col2) 'quantity', SUM(Col4)/SUM(Col2) 'price/qty' FORMAT SUM(Col4)/SUM(Col2) '0.000'"))
This one formula should produce all headers and results, formatted according to your full "desired result." If not, share a link to your spreadsheet (or a copy of it).

Concatenating from table to string, sum amounts

Consider the Google Sheets table below:
A B C D
1 category subcategory company amount
2 health care diagnostics AA 100
3 health care diagnostics AB 50
4 materials mining BA 75
5 financials banks CA 30
6 financials insurers CB 35
7 financials banks CC 10
8 financials banks CD 40
9 financials hedge fund CE 5
10 health care equipment DA 50
I would like to list, per subcategory, the companies in it, and the amount spent:
A B C
1 category companies amount
2 health care AA AB DA 200 <--- 100 + 50 + 50
3 materials BA 75
4 financials CA CB CC CD CE 120
Column A I will type myself / hardcode. But what formula in column B and C will give this result, depending on the value in A?
try:
=ARRAYFORMULA(IFNA(VLOOKUP(E3:E, QUERY({QUERY({A:D},
"select Col1,sum(Col4)
where Col1 is not null
group by Col1
label sum(Col4)'amount'", 1), {"companies";
TRIM(FLATTEN(QUERY(TRANSPOSE(QUERY(QUERY({A:D, C:C},
"select max(Col3)
where Col1 is not null
group by Col1
pivot Col5"),
"offset 1", 0)),,9^9)))}},
"select Col1,Col3,Col2", 1), {2,3}, 0)))
where E3:E are your hardcoded values

Get Max value from range (multiple sheets) grouped by Name

I have 3 sheets that have the exact same format
Sheet1
A B C D
George 10 2 8
Nick 15 89 0
Mike 13 1 50
Lucas 9 -5 12
Sheet2
A B C D
Nick 1 9 5
Mike 1 10 6
George 11 22 5
Lucas 10 5 2
Panos 55 0 1
Sheet3
A B C D
Panos 0 9 1
George 1 2 5
Nick 7 2 1
Lucas 1 5 1
I want to query the range {'Sheet1'!A1:D5; 'Sheet2'!A1:D5; 'Sheet3'!A1:D5}
And get something like MAX(Col2:Col4) Group By Col1
Which would return something like:
George 22
Nick 89
Mike 50
Lucas 12
Panos 55
I tried:
=sort(query({'Sheet1'!A1:D5; 'Sheet2'!A1:D5;'Sheet3'!A1:D5}, "select Col1, MAX(Col2:Col4) Group by Col1 Label MAX(Col2:Col4) '' " ),2, FALSE)
and
=sort(query({'Sheet1'!A1:D5; 'Sheet2'!A1:D5;'Sheet3'!A1:D5}, "select Col1, MAX(MAX(Col2),MAX(Col3), MAX(Col4)) Group by Col1 " ),2, FALSE)
Both didn't work. Any ideas?
Please try:
=query(sort(transpose(query({Sheet1!A1:D5;Sheet2!A1:D5;Sheet3!A1:D5},"select max(Col2), max(Col3), max(Col4) pivot Col1"))),"select Col1, max(Col2) group by Col1 label(Col1) ''")
To sum up your question, It requires finding the MAX across the columns to the right as well as down. As such, QUERY does NOT have such 2D function.
So, Use a Helper column E&F in each sheet:
Max of B&C:
E2:
=ARRAYFORMULA(IF(B2:B>C2:C,B2:B,C2:C))
Max of B,C&D:
F2:
=ARRAYFORMULA(IF(D2:D>E2:E,D2:D,E2:E))
Now, Use Query:
Query:
=ARRAYFORMULA(QUERY({Sheet1!A2:F;Sheet2!A2:F;Sheet3!A2:F}, "Select Col1,max(Col5) where Col1 is not null group by Col1 order by max(Col5) desc"))
Notes:
Change ranges to suit
You could also simply use MAX for each row without the ARRAYFORMULA
Theoretically, For a single cell solution, You could enter this formula to find the max of 3 real numbers
Another approach perhaps a bit simpler but needing two queries
=sort(unique(({Sheet1!A1:A5;Sheet2!A1:A5;Sheet3!A1:A5})))
to get the names starting in (say) F2
Then this to get the maximum values for each name in (say) G2 and pulled down
max(query({Sheet1!A$1:D$5;Sheet2!A$1:D$5;Sheet3!A$1:D$5},"select max(Col2),max(Col3),max(Col4) where Col1='"&F2&"'"))

Resources