Auto increment by condition in Google Sheet - google-sheets

I have 2 columns with ID1 AND ID2:
ID1
ID2
1
1
1
2
2
1
2
2
2
3
3
1
3
2
I want to get an auto-increment formula for ID2 - when ID1 changes, the ID2 should start from the beginning.

For a solution using arrayformula, you could try in B2
=Arrayformula(if(A2:A="",,countifs(A2:A, A2:A, row(A2:A), "<="&row(A2:A))))

Try below COUNTIFS() formula.
=COUNTIFS($A$2:$A2,A2)

Related

How to count the number of times a specific column is the maximum value in a row?

I have a spreadsheet that looks like this, which tracks attendance and order. On day 1, the order was [Alice, Bob, Catherine, Dave]. On day 2, the order was [Bob, Dave, Catherine], and Alice was absent:
Date
Alice
Bob
Catherine
Dave
10/1
0
1
2
3
10/2
x
0
2
1
10/3
3
1
2
0
10/4
1
0
x
x
10/5
0
x
1
2
I am trying to write a formula to get the total number of times each attendee went last. In other words, I want to count the number of times a name in a column is the MAX value for each date row, ignoring any x's. Ideally, I would like a single formula that I could place in a single cell. If successful the resulting table would look like this:
Attendee
# of times they went last
Alice
2
Bob
0
Catherine
1
Dave
2
What's the best way to accomplish this?
Find the MAX BYROW, then compare the max to each of the Attendees using REDUCE+OFFSET. If equal, create a SUM:
=LAMBDA(
max,
REDUCE(
{"Attendee","#times"},
B1:E1,
LAMBDA(
a,c,
{a;c,SUMPRODUCT(OFFSET(c,1,0,5)=max)}
)
)
)(BYROW(B2:E6,LAMBDA(r,MAX(r))))
try:
=INDEX(QUERY(BYROW(B2:INDEX(E:E, MAX((A:A<>"")*ROW(A:A))),
LAMBDA(x, TEXTJOIN(, 1, IF(x=MAX(x), B1:E1, )))),
"select Col1,count(Col1) group by Col1 label count(Col1)''"))
with Bob:
=SORTN({QUERY(BYROW(B2:INDEX(E:E, MAX((A:A<>"")*ROW(A:A))),
LAMBDA(x, TEXTJOIN(, 1, IF(x=MAX(x), B1:E1, )))),
"select Col1,count(Col1) group by Col1 label count(Col1)''");
TRANSPOSE({B1:E1;(B1:E1="")*1})}, 9^9, 2, 1, 1)
One easy way is to add a column with the name of the last attendee. If, when you say you want a single formula, the reason is to don't show other cells, you can hide the column. Then you count how many times each name appears. This should be the result:
Date
Alice
Bob
Catherine
Dave
last
Attendee
# of times they went last
10/1
0
1
2
3
Dave
Alice
2
10/2
x
0
2
1
Catherine
Bob
0
10/3
3
1
2
0
Alice
Catherine
1
10/4
1
0
x
x
Alice
Dave
2
10/5
0
x
1
2
Dave
Formula in cell G2 (under "last"), copied to the other cells of the column:
=INDEX(B$1:E$1,1,MATCH(MAX(B2:E2), B2:E2, 0))
It searches, only in that line, the maximum value. Gets the column and returns the correspondent name from the first line.
Formula in I2 (under "Attendee"):
=TRANSPOSE(B1:E1)
Transposes all names from the first line to the column.
Formula in J2, copied to the other cells of the column:
=COUNTIF(G$2:G,I2)
Counts how many times the name appears in column G.

How can I increment a repeating number until 195 on Google Sheets?

How can I have it increment a number that repeats 12 times, so that it looks like this:
1
1
1
1
1
1
1
1
1
1
1
1
2
2
2
2
2
2
2
2
2
2
2
I've been trying to tweak this formula, but with no success:
=ARRAYFORMULA(TRANSPOSE(SPLIT(QUERY(REPT(1&",",12), , 999^99), ",")))
Thanks.
try:
=INDEX(FLATTEN(TEXT(SEQUENCE(12), SEQUENCE(1, 12,,))))
Try
=arrayformula(INT(sequence(12*195,1,0,1)/12)+1)
sequence
Try-
=ARRAYFORMULA(ROUNDUP(SEQUENCE(195)/12,0))
Try this if you would like to tweak your original formula:
=ARRAYFORMULA(TRANSPOSE(SPLIT(QUERY(REPT(sequence(12)&",",12), , 999^99), ",")))

How to sum rows in pairs in google sheets?

I have a column like this:
A B C
1 Column
2 1
3 0
4 1
5 2
6 0
7 2
8 3
9 1
I want to be able to sum each pair of two rows with one or two formulas that I can drag down. So hard coded, my formulas would look like this:
A B C
1 Column
2 1 =SUM(A2:A3)
3 0 =SUM(A4:A5)
4 1 =SUM(A6:A7)
5 2 =SUM(A8:A9)
6 0
7 2
8 3
9 1
Thanks in advance.
Here is what I ended up doing. I added two reference columns then performed a SUMIFS() with another column matching every other reference column.
A B C D E
1 REFERENCE1 REFERENCE2 Column FORMULA REFMATCH
2 1 =ROUNDDOWN(A2) 1 =SUMIFS(C:C, B:B, E2) 1
3 1.5 =ROUNDDOWN(A3) 0 =SUMIFS(C:C, B:B, E3) 2
4 2 2 1 2 3
5 2.5 2 2 4 4
6 3 3 0
7 3.5 3 2
8 4 4 3
9 4.5 4 1
Try this:
=arrayformula( query( query( iferror( if( {1,1,0}, floor( mod(row(A:A)-{1,1},{10^99, 2}), {2,1} ), transpose( split( regexreplace( query( transpose( query( transpose(A2:A9 & char(9)), , 50000 ) ), , 50000 ), "\s+$", "" ), char(9) & " ", ) ) ) ), "select max(Col3) where Col3 is not null group by Col1 pivot Col2", 0 ), "select Col1 + Col2 offset 1 label Col1 + Col2 '' ", 0 ) )
This is an array formula that creates the whole result table in one go. It does not require helper columns.
Try this:
=ArrayFormula(ARRAY_CONSTRAIN(FILTER(A2:A,ISEVEN(ROW(A2:A))) + FILTER(A2:A,ISODD(ROW(A2:A))),ROUND(COUNTA(A2:A)/2),2))
This is an array formula, so it does not get dragged. That is, this one formula produces all results.
Simply put, this adds the values in all even rows to the values in all odd rows.
Since values are paired, ARRAY_CONSTRAIN just limits the return to half the rounded number of available values.
Could work adding a column "A" of pairs and putting this formula in the 3rd column =IF(A1=A2,"",SUMIF($A$1:$A$8,A1,$B$1:$B$8))
A B C
1 7 =IF(A1=A2,"",SUMIF($A$1:$A$8,A1,$B$1:$B$8))
1 8
2 9
2 34
3 2
3 4
4 5
4 6
drag down and should remain like this:
A B C
1 7
1 8 15
2 9
2 34 43
3 2
3 4 6
4 5
4 6 11
Isn't exactly what you want with just one formula, but it could work with one formula and one column added.

Rolling count with array formula in 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<>""))

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