Rolling count with array formula in Google Sheets - google-sheets

I have the dataset below. Col1 is given data and Col2 is the rolling count of the previous 5 rows of Col1 (inclusive).
Date Col1 Col2
01/04/20 2 1
02/04/20 1 2
03/04/20 4 3
04/04/20 3
05/04/20 3
06/04/20 5 3
07/04/20 2 3
08/04/20 2
09/04/20 2
10/04/20 1 3
11/04/20 2
12/04/20 1
13/04/20 1
14/04/20 1
15/04/20 1 1
Is there a way to use arrayformula to do this rather than inputting a count formula into every cell in Col2 going down?

You can use Countifs with a condition on the rows:
=ArrayFormula(filter(countifs(B2:B,">0",row(B2:B),"<="&row(B2:B),row(B2:B),">"&row(B2:B)-5),A2:A<>""))
assuming the numbers are positive
To include any number, you can use:
=ArrayFormula(filter(countifs(isnumber(B2:B),true,row(B2:B),"<="&row(B2:B),row(B2:B),">"&row(B2:B)-5),A2:A<>""))
If you wanted to show rows corresponding to future dates as blanks, you could add an If statement:
=ArrayFormula(filter(if(A2:A>today(),"",countifs(isnumber(B2:B),true,row(B2:B),"<="&row(B2:B),row(B2:B),">"&row(B2:B)-5)),A2:A<>""))

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)''"))

Formula to create a row for every value of a comma-delimited list - GSheets

I am trying to write a formula that will take a set of columns and pair them into separate rows with a comma-delimited list. In SQL, I would do this with a left join, but I am not sure how to leverage GSheets functions for this.
Here is what I mean:
Source Data
Col1
Col2
CommaDelim
Col1Val1
Col2Val1
1,2,3
Col1Val2
Col2Val2
1
Col1Val3
Col2Val3
1,2
Col1Val4
Col2Val4
1,2,3,4
Desired Output
Col1
Col2
CommaDelim
Col1Val1
Col2Val1
1
Col1Val1
Col2Val1
2
Col1Val1
Col2Val1
3
Col1Val2
Col2Val2
1
Col1Val3
Col2Val3
1
Col1Val3
Col2Val3
2
Col1Val4
Col2Val4
1
Col1Val4
Col2Val4
2
Col1Val4
Col2Val4
3
Col1Val4
Col2Val4
4
try:
=INDEX(QUERY(SPLIT(FLATTEN(A1:A&"×"&B1:B&"×"&SPLIT(C1:C, ",")), "×"),
"where Col3 is not null"))

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)''")), "(.*×)", ))

How to "counta" if some cell greater than 1 in Google Docs?

I have this data in my Google Spreadsheet
Col1 Col2
1 X
1
2 A
2 B
I want to make a formula to count Not Empty cell from Col2 but with condition that Col1 is greater than 1
Anyone know how to do it?
countifs can count based on multiple criteria.
Using it we can count only when:
Col1 is ">1"
and
Col2 is not empty (using wildcard "*" matching)
=countifs(A1:A4, ">1", B1:B4, "*")

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