ArrayFormula with sum of previous rows - google-sheets-formula

I have an ArrayFormula to calculate a value for each row, and for each 6th row I want it to calculate the sum of the previous 5 instead.
Example sheet: https://docs.google.com/spreadsheets/d/18g2bOOBqsUgmy3ZXINOl6hcaMXf-uYJv7PGft247FjU/edit?usp=sharing
I have tried several routes, including google script, but keep banging my head against limitations of ArrayFormula.

You need make group by rows
My E.g
Cell A2 (Name groups):
=ArrayFormula(IF(B2:B<>"",FLOOR((ROW(A2:A)-2)/5)+1,""))
Column B (Your Data)
Cell E2 (Result):
=QUERY({QUERY({A2:B},"select Col1,sum(Col2) where Col1>0 group by Col1");
QUERY({A2:B},"select Col1,Col2 where Col1>0")},
"select Col2 where Col1>0 order by Col1,Col2 label Col2 ''")
Function References
Query

Related

Oracle counting distinct rows using two columns merged as one

I have one table where each row has three columns. The first two columns are a prefix and a value. The third column is what I'm trying to get a distinct count for columns one/two.
Basically I'm trying to get to this.
Account
Totals
prefix & value1
101
prefix & value2
102
prefix & value3
103
I've tried a lot of different versions but I'm basically noodling around this.
select prefix||value as Account, count(distinct thirdcolumn) as Totals from Transactions
It sounds like you want
SELECT
prefix||value Account,
count(distinct thirdcolumn) Totals
FROM Transactions
GROUP BY prefix, value
The count(distinct thirdcolumn) says you want a count of the distinct values in the third column. The GROUP BY prefix, value says you want a row returned for each unique prefix/value combination and that the count applies to that combination.
Note that "thirdcolumn" is a placeholder for the name of your third column, not a magic keyword, since I didn't see the actual name in the post.
If you want the number of rows for each prefix/value pair then you can use:
SELECT prefix || value AS account,
COUNT(*) AS totals
FROM Transactions
GROUP BY prefix, value
You do not want to count the DISTINCT values for prefix/value as if you GROUP BY those values then different values for the pairs will be in different groups so the COUNT of DISTINCT prefix/value pairs would always be one.

Google Sheets Formula - Get Total from filtered dates per row (undefined number of columns)

I have this data in Google Sheets where in I need to get the total of the filtered data columns per row. The date columns are not fixed (may increase over time, I already know how to handle this undefined number of columns). What my current challenge encountered is how can I efficiently get a summary of totals per user based on filtered date columns.
My data is like this:
My expected result is like this:
My current idea is this:
Here is a sample spreadsheet for reference:
https://docs.google.com/spreadsheets/d/1_dByPabStGQvh94TabKxwFeUyVaRFnkBCRf4ioTY5jM/edit?usp=sharing
This is a method to unpivot the data so you can work with it
=ARRAYFORMULA(
QUERY(
IFERROR(
SPLIT(
FLATTEN(
IF(ISBLANK(A2:A),,A2:A&"|"&B1:G1&"|"&B2:G)),
"|")),
"select Col1, Sum(Col3)
where
Col2 >= "&DATE(2022,1,1)&" and
Col2 <= "&DATE(2022,1,15)&"
group by Col1
label
Col1 'Person',
Sum(Col3) 'Total'"))
Basically, its creating an output of User1|44557|8 -- it then FLATTENs it all and splits by the pipe, which gives you three clean columns.
Run that through a QUERY to SUM by the person between the dates and you get what you're after. If you wanted to use cell references for dates, simply replace the dates with the cell references.
To expand the table, change B1:G1 and B2:G2 to match the width of the range.

Count rows since last occurrence of value , excluding blank cells from the count

I want to count the number of rows since the last occurrence of a specific value, while excluding rows where the cell is a blank or empty value. I have provided a sample of fake data to try to explain what I'm asking. So, with the sample data, on every occurrence of "Apple", I would like to show the number of valued rows since the last occurrence of "Apple". I currently have a formula like this:
=ROW()-MAX(FILTER(ROW(INDIRECT("B1:B"&ROW()-1)),INDIRECT("B1:B"&ROW()-1)=INDIRECT("B"&ROW())))
The formula gives me the number of rows since the last occurrence, but does not exclude the blank cells, and I'm wanting to figure out a way to get this result without counting the rows with blank cells in the column.
Sample Data
Assuming values are in range A2:A and we want the results in column B, use this formula in cell B2 and then manually extend down:
=IF(A2="Apple",IFERROR(MATCH("Apple",QUERY({A$2:A2,SEQUENCE(ROWS(A$2:A2))},"select Col1 where Col1 is not null order by Col2 desc offset 1"),0),0),)
Explanation
The QUERY retrieves all values above the current one (e.g. for row 7 it will be A2:A6) in reverse order (A6, A5, ..., A2) and excluding blanks.
MATCH returns the position of the first occurrence of the value in interest in the result of the QUERY.
I was not able easily to make this formula auto-extend using ARRAYFORMULA or MAP(LAMBDA), so it has to be manually extended.

Expanding query with where or filtering in formula in google sheets

I have this formula which works great: =ArrayFormula(QUERY(TRANSPOSE(TRIM(SPLIT(JOIN(";",'TAB'!I2:I),";"))&{"";""}),"select Col1, count(Col2) group by Col1 label count(Col2) ''",0))
It takes values in column that are separated with ; and counts unique entries and plots everything in a table.
Question: I would like to add a filter/condition so that it would plot only the values what have specific entries in another column. Like A or B ir C, but not all values.
I have tried: =ArrayFormula(QUERY(TRANSPOSE(TRIM(SPLIT(JOIN(";",'TAB'!I2:I),";"))&{"";""})&'RAW-TODOS'!F2:F,"select Col1 * where 'TAB'!F2:F ='A'or 'B' or 'C', count(Col2) group by Col1 label count(Col2) ''",0))
but probably because of obvious reasons it did not work. Please help me with this one.
Thank you in advance.
try:
=INDEX(QUERY(TRIM(FLATTEN(SPLIT(FILTER('RAW-TODOS'!B:B,
REGEXMATCH('RAW-TODOS'!A:A, "NEW|IN PROGRESS")), ";"))),
"select Col1,count(Col1)
where Col1 is not null
group by Col1
label count(Col1)''"))
Input the criteria in J1, which is referred by my formula.
leave it blank to match all rows
for single value, simply input it, i.e. NEW
for multiple values, join them with |, i.e. NEW|IN PROGRESS
=ArrayFormula(QUERY(SPLIT(FLATTEN('RAW-TODOS'!A2:A&"♦"&TRIM(SPLIT('RAW-TODOS'!B2:B,";"))),"♦"),CONCATENATE("select Col2,count(Col2) where Col2 is not null",IF(J1="",," and Col1 matches '"&J1&"'")," group by Col2 label count(Col2) ''"),0))

How can I select two columns in a query and order them by a third column?

Basically, is there a way to do this?
select B,C,SUM(I) group by B,C order by L
I've added the row numbers to L, but if there's a way to get row numbers in the formula, I'd love to know. Thanks.
try perhaps:
=ARRAY_CONSTRAIN(QUERY(A:L,
"select B,C,sum(I),L
where B is not null
group by B,C,L
order by L
label sum(I)''", 0), 999^99, 3)
This formula will group by 2 columns and sort by the 3-d column.
=ARRAY_CONSTRAIN(QUERY(
FILTER({B:B,C:C,I:I,VLOOKUP(B:B&C:C,{B:B&C:C,L:L},2,)},B:B<>""),
"select Col1,Col2,sum(Col3),Col4
where Col1 is not null
group by Col1,Col2,Col4
order by Col4", 1), 999^99, 3)
ARRAY_CONSTRAIN to hide sort column
FILTER + VLOOKUP to select first entries of column 3 (L) in order to skip duplicates.

Resources