Grouped average within ARRAYFORMULA only for past dates - google-sheets

I need to average the values that share the same ID and have been published in the same or previous months. I have to use ARRAYFORMULA due to the volume of rows.
This is an iteration of a question I asked last week: How to use AVERAGEIFS within ARRAYFORMULA, which I've tried to adapt for this, unsuccessfully.
As it can be a little confusing, I have created this file with dummy data. Although year and month were included in the ID at the beginning, I have split them into columns to make filtering easier.
Note that February average includes January values with the same ID, March includes January, February and March, etc. When the year changes, the average restarts. I suppose it could be done by comparing the values of the year (equals) and month (equals or lower), but I don't know how to insert the comparator into the last-day formula.
Thank you all for your time and effort.

try:
=ARRAYFORMULA(IFNA(VLOOKUP(A2:A&B2:B&C2:C; QUERY({A2:A&B2:B&C2:C\D2:D};
"select Col1,avg(Col2)
where Col2 is not null
group by Col1
label avg(Col2)''"; 0); 2; 0)))
or if months does not matter use:
=ARRAYFORMULA(IFNA(VLOOKUP(A2:A&B2:B; QUERY({A2:A&B2:B\D2:D};
"select Col1,avg(Col2)
where Col2 is not null
group by Col1
label avg(Col2)''"; 0); 2; 0)))

Related

I need a Google Sheets formula that will count the dates and display total per month

I need a formula that will count the dates in column A and show how many of each month.
Example of the data in Column A
12/1/21
12/10/21
12/29/21
12/30/21
1/11/22
1/12/22
5/2/22
The returned data would be (or similar - I just need to know the total per month over the years)
12/21 - 4
1/22 - 2
5/22 - 2
and so on.
Here is what I go so far.
=ArrayFormula(month('tab name'!A2:A10))
but this list the months and not counts them.
use:
=INDEX(QUERY(TEXT(A1:A, "mmm e"),
"select Col1,count(Col1)
where Col1 <> 'Dec 1899'
group by Col1
label count(Col1)''"))

Sheets: Find first and last occurrence of a value in a range

I have been struggling with the Google Sheets query for several hours and maybe getting confused how to combine HLookup and VLookup (or any other function) in a way that can find the first and last occurrence of a value in a sheet based on the date header above it.
Here is an example sheet for reference which is very clear, but I will try explain verbally as well ... https://docs.google.com/spreadsheets/d/1rBVM7EtW3IREundWs_f2ftic-h4fEB97u4k4sZyIFNY/edit#gid=0
Given that I have a 2d range of cafeteria locations serving food on certain day (so the Y-axis headers of the table are cateteria locations and the X-axis headers are dates and the value is the name of the food served that day such as "Pizza") ... I want to have another table below that has a lookup for the first and last date that the food was offered. In my reference sheet I denoted that by Yellow highlight.
It seems like something that should be doable in a spreadsheet tool; unless it is impossible and I am not realizing it. Is such an operation possible?
delete range B10:C and use:
=INDEX(IFNA(VLOOKUP(A10:A, QUERY(SPLIT(FLATTEN(B1:E1&"×"&B2:E8), "×"),
"select Col2,min(Col1),max(Col1) group by Col2", ), {2,3}, 0)))
See if this helps
=query(ArrayFormula(split(flatten(text(B1:E1, "yyyy-mm-dd")&"~"&B2:E5), "~")), "Select Col2, min(Col1), max(Col1) where Col2 <> '' group by Col2 label Col2 'Food', min(Col1) 'First Offered', max(Col1) 'Last Offered' format min(Col1) 'yyyy-mm-dd', max(Col1) 'yyyy-mm-dd'", 0)
Change range to suit.

Counting automatic timestamps per interval in Google Sheets

I have a form my employees use to record completed tasks. I rely on the automatic timestamps generated by the form for accuracy. Here is where I need help:
I used COUNTIFS to count how many tasks were completed per hour. Since the form receives responses regularly within the shift, the formula does its work accurately. The problem is I have to change my formula to match the date. And I have 15 employees doing tasks. It is a pain in the neck to have to change the formula per interval (a total of 8 per day) for each of the 15 employees. I was wondering if there is an easier way?
Here is the example of the formula I use for 1 employee, for 1 interval. Add 7 more of these to get the other hourly intervals (3,4,5,6 PM etc):
=countifs(Time, ">=08/21/2021 2:00:00 PM", Time, "<=08/21/2021 2:59:00 PM", Name, "=John Smith")
And also imagine I need to change the date also. It is a very long process, and I hope someone has a simplified solution.
I don't think I can separate the timestamp by date and time to make it easier, because the form updates when an employee enters a completed task. I need the data available and in view all the time, that's why I chose this route. Any help will be much appreciated.
If your timestamps are in column A, and employee names in column B, use the following formula:
=query({arrayformula(text(A1:A,"yyyy-MM-dd")),arrayformula(text(A1:A,"HH")),A1:B},"select Col1, Col2, Col4, count(Col4) where Col4 is not null group by Col1, Col2, Col4 order by Col1, Col2, Col4 label Col1 'date', Col2 'hour', Col4 'employee', count(Col4) 'tasks completed'",1)
You can check an example at (make a copy of the sheet):
https://docs.google.com/spreadsheets/d/17g7Z8fARbWee0-y1evMhhLY5qVSiq2U-VMu_C2SNmXU/copy

Sum based on multiple row + header criteria

I have a problem with summing up my values from a data set, that's structured like this:
The goal is to sum the revenues separated by company and split by month, so the result is output in this way
I have tried it with some =sumifs + index/match and =sumproduct solutions, but can't seem to make it work.
Here's the sample file:
https://docs.google.com/spreadsheets/d/16xOoPCHDtcSRRojCkwcBorUc5dstgkXFPR6M_d5uY2U/edit#gid=0
On the "revenues" tab, in cell B4, try using the formula:
=SUMIFS(indirect(address(1,match(A4,Overview!$3:$3,0)-1,,,"Overview")&":"&address(1000,match(A4,Overview!$3:$3,0)-1)),Overview!A1:A1000,">="&B$2,Overview!A1:A1000,"<="&B$3)
To break it down, this bit helps figure out which revenue column to use by matching the name of the company and then taking the column before that:
match(A4,Overview!$3:$3,0)-1
This bit creates an address "Overview!$G$1":
address(1,match(A4,Overview!$3:$3,0)-1,,,"Overview")
This bit creates the 2nd part of the address i.e.":$G$1000":
"&":"&address(1000,match(A4,Overview!$3:$3,0)-1)
And the rest is a SUMIFS where it sums the revenue column for dates after the 1st of the month and before the last date of the month.
Be careful: your data is for 2020 and your summary table is using dates in 2021.
Reference:
SUMIFS
ADDRESS
MATCH
INDIRECT
use in A4:
=ARRAYFORMULA(QUERY(QUERY({SPLIT(FLATTEN(IF(
FILTER(Overview!G7:1000, MOD(COLUMN(Overview!G7:1000)+2, 3)=0)="",,
TEXT(Overview!A7:A25, "m")&"×"&
FILTER(Overview!G7:1000, MOD(COLUMN(Overview!G7:1000)+2, 3)=0)&"×"&
FILTER(Overview!G3:3, MOD(COLUMN(Overview!G3:3)+1, 3)=0))), "×");
SEQUENCE(12), SEQUENCE(12, 2,,)},
"select Col3,sum(Col2)
where Col1 is not null
group by Col3
pivot Col1", 0),
"offset 2", 0))

Total by day in google sheets for a given month and year

I have the following table:
What I'm trying to do is to write a formula that totals by each unique day in the month and year given in B1 and C1 thus the output should be:
I'm not sure how to start with this, googling seems to suggest the use of SUMIF.
Sheet is here
try:
=QUERY(FILTER({A5:B, TIMEVALUE(C5:C)}, MONTH(A5:A)=MONTH(B1&1), YEAR(A5:A)=D1),
"select Col1,sum(Col3)
where Col1 is not null
group by Col1
label sum(Col3)''
format sum(Col3)'[hh]:mm'", 0)

Resources