Google Sheets: select top n daily values over entire year - google-sheets

I have a google sheets raw data set of 5 columns over a 2 year time period:
Col A: year (2017...2018)
Col B: month (1...12)
Col C: day of month (1...31)
Col D: hour of day (0...23)
Col E: hourly electricity consumption (0.00-9999.99)
What I'd like to extract is the top 3 hours of highest electricity consumption of each and every day (i.e. 3 points * 365 days/year * 2 years = 2190 of the rows).
I know how to either:
Get the top 3 consumption hours within a single day:
=QUERY(A1:E23, "select A,B,C,D,E order by E desc limit 3") for the first day
=QUERY(A24:E47, "select A,B,C,D,E order by E desc limit 3") for the second day, etc.
or get the highest (single) consumption hour of each and every day, without knowing during which hour it occurs:
=QUERY(A1:E, "select A,B,C,max(E) group by A,B,C")
How do I combine the two, so I still capture all info (all columns)?

I made some sample data on a tab called Data on this sheet.
Then I put this formula in cell B1 on the tab called MK.Help Top 3.
=FILTER(Data!A:E,COUNTIFS(Data!A:A,Data!A:A,Data!B:B,Data!B:B,Data!C:C,Data!C:C,Data!E:E,">="&Data!E:E)<=3)
Countifs() is useful for creating a sort of ranking.

Related

Spreadsheet: Sum of dynamic number of rows

I have a table in my Google Spreadsheet that looks like this :
Row
(A) Some day
(B) Some data
1
day 1
5
2
day 2
10
3
total
4
day 1
8
5
day 2
7
6
day 3
9
7
total
Where I can have multiple "day rows", but I don't know how many. It can be only 1 like it can be 20 "day rows". And I want the "total row" to automatically do a SUM of the "day rows" above.
Result expected :
Row
(A) Some day
(B) Some data
1
day 1
5
2
day 2
10
3
total
15
4
day 1
8
5
day 2
7
6
day 3
9
7
total
24
Where B3 is equal to SUM(B1:B2) and B7 is equal to SUM(B4:B6)
I am trying to do that without the App Script, just using Spreadsheet native functions.
I think I should be using the SUM function or the Query function, but I don't know how to dynamically get the right range. Do you have any idea how to do that ?
Thank you
In your example, column B would be a mixture of constants and formulas. That would require a script to deposit the formulas. However with an extra column, you can avoid scripts. In C2 enter:
=if(A2<>"Total","",sum($B$1:$B1)-sum($C$1:C1))
and copy downwards:
Basically we add column B and subtract any previous Totals in column C.
Another approach is to place the following single array formula in C1:
=ArrayFormula(IF(A:A="",, SUMIF(IF(ROW(A:A),ROW(A:A)), "<="&ROW(A:A),B:B) - SUMIF(IF(ROW(A:A), ROW(A:A)),"<="& VLOOKUP(ROW(A:A)-1, FILTER(ROW(A:A), A:A="total"), 1, TRUE), B:B)))
If you only want to see the values for the "total" rows, change the opening
IF(A:A=""
to
IF(A:A<>"total"
The short version of how it works is that a sum is made of all values up to the current row in B:B, and from that is subtracted any values up to the last listing of the word "total" in A:A.
paste in each cell in B column where A column = total
=INDEX(SUM(IFERROR(1*INDIRECT(ADDRESS(MATCH(INDEX(
COUNTIFS({"total";A:A}, {"total";A:A}, {"total";A:A}, "=total",
{ROW(A:A);ROW()}, "<="&{ROW(A:A);ROW()})-1, ROW()+1, 1),
COUNTIFS({"total";A:A}, {"total";A:A}, {"total";A:A}, "=total",
{ROW(A:A);ROW()}, "<="&{ROW(A:A);ROW()}), 0), 2)&":"&
ADDRESS(ROW()-1, 2)), 0)))

Need help creating a formula for dynamic average of last 4 weeks expenses

I am looking to create a spreadsheet that my staff fill out, it then gives me a master sheet with all the data, then I import dynamically to my financial spreadsheet telling me the average cost of my client over the last 30 days.
I am looking to create an AVERAGE formula of the last 30 days when Date = Today (Monday) (I want the weekday Monday as that's when staff hand in invoices)
Hope this makes sense, it's really tough!
Here's a video of me explaining my desired outcome
https://www.loom.com/share/3a9cb75052b246d1af2ba2f9ce9180a7
I've followed several guides & can't figure it out.
=ArrayFormula(iferror(query(average(if(today() - weekday(today(),3)-30)))))
I expected $90 average and I just get blank
You could use this formula:
=AVERAGE(VLOOKUP(TODAY()-WEEKDAY(TODAY(),2)+1,A:H,2,FALSE),VLOOKUP(TODAY()-WEEKDAY(TODAY(),2)-6,A:H,2,FALSE),VLOOKUP(TODAY()-WEEKDAY(TODAY(),2)-13,A:H,2,FALSE),VLOOKUP(TODAY()-WEEKDAY(TODAY(),2)-20,A:H,2,FALSE))
To break it down in to its component parts, the AVERAGE is taken from VLOOKUP results:
VLOOKUP(TODAY()-WEEKDAY(TODAY(),2)+1,A:H,2,FALSE)
The VLOOKUP is looking for the last Monday from the current date:
TODAY()-WEEKDAY(TODAY(),2)+1
Then
TODAY()-WEEKDAY(TODAY(),2)-6
and so on...
When using on your sheet, you will have to specify the column you want to reference in your look up, for colunm B (brand1) use: A:H,2,FALSE), for colunm C (brand2) use: A:H,3,FALSE), for colunm d (brand3) use: A:H,4,FALSE) and so on...
=INDEX(QUERY({INDIRECT("A2:D"&ROW()-1)},
"select avg(Col2),avg(Col3),avg(Col4)
where Col1 <= date '"&TEXT(TODAY(), "yyyy-MM-dd")&"'
and Col1 >= date '"&TEXT(TODAY()-30, "yyyy-MM-dd")&"'"), 2, )

Summarising a Pivot Table by Month Groups

I have a simple table with Monthly downloads data for the last 24 months. I want to build a simple Summary showing the following columns.
Number of Downloads for last Month
Downloads in the last 3 Months
Downloads in the last 6 Months.
Downloads in Last 6 Months (last year)
Downloads in Last 3 Months (last year)
Downloads in Last Month (last year)
I tried to group the data in the pivot table - but I can't seem to get it to work as Google Sheets doesn't allow overlapping Groups.
I tried using Calc fields - but again it doesn't seem to allow me to break it out by months.
Any Idea - how to do this or am I asking too much of Google Sheets?
Added a dummy set of data: https://docs.google.com/spreadsheets/d/1qhuXf__hvJMZjXcugBZyiq_lZBqFNTTkCHEvh9rcUK0/edit?usp=sharing
={QUERY(TRANSPOSE(QUERY(TRANSPOSE({$C$3:$O$5,$C$7:$O$9}),
"select Col2,Col3
where Col1>=date'"&TEXT(DATE(E13,1,1),"yyyy-mm-dd")&"'
and Col1< date'"&TEXT(DATE(E13,9,1),"yyyy-mm-dd")&"'")),
"select Col2+Col3+Col4+Col5+Col6+Col7+Col8+Col9
label Col2+Col3+Col4+Col5+Col6+Col7+Col8+Col9''"),
QUERY(TRANSPOSE(QUERY(TRANSPOSE({$C$3:$O$5,$C$7:$O$9}),
"select Col2,Col3
where Col1>=date'"&TEXT(DATE(E13,9,1),"yyyy-mm-dd")&"'
and Col1< date'"&TEXT(DATE(E13+1,1,1),"yyyy-mm-dd")&"'")),
"select Col2+Col3+Col4+Col5
label Col2+Col3+Col4+Col5''"),
QUERY(TRANSPOSE(QUERY(TRANSPOSE({$C$3:$O$5,$C$7:$O$9}),
"select Col2,Col3
where Col1>=date'"&TEXT(DATE(E13,12,1),"yyyy-mm-dd")&"'
and Col1< date'"&TEXT(DATE(E13+1,1,1),"yyyy-mm-dd")&"'")),
"select Col2")}
demo spreadsheet

Google sheets lookup and sum

I have a production schedule where cells are filled with text:
Day 1, Day 2 etc.
And I'd like these cells to be associated with values elsewhere in the same sheet.
For example. Day 1 would be 4 (which corresponds to 4 hours).
Then I have to sum these cells/values (Day 1 = 4 + Day 2 = 6) to get a total for each row.
I can't seem to find a way to do all this using one formula. Is it even possible?
Feel free to check out the sheet.
=SUMPRODUCT(IFERROR(VLOOKUP(B2:L2,Formulas!A:B,2,0)))

Google sheets compare 2 fields on a different sheet and return one based on criteria

I have 2 different Google Sheets. Master Sales & Projections. On Master Sales I have 2 columns, Expected & Billed which has data broken down by billing weeks.
I need Sheet 2 to grab the data from the cell that not 0 since we are trying to track total sales for the month (expected & actual) and then at the end of the month we would have the real billed numbers.
Potato example below
Sheet 1
A B
1 Expect Billed
2 $123 $145
3 $145 $155
4 $156 $0
5 $132 $0
What Sheet 2 should look like
A B
1 Week1 $145
2 Week2 $155
3 Week3 $156
4 Week4 $132
Assuming you will arrange for the week numbers, in B1 please try:
={query(Sheet1!A2:B5,"select B where B<>0",0);query(Sheet1!A2:B5,"select A where B=0",0)}

Resources