How to sum rows in pairs in google sheets? - 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.

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

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

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