How to create multiple sequences of varying lengths with arrayformula - google-sheets

Let's say I have a list of 3 values in a column A. The amount of values is arbitrary, so there can be more. So for example:
|A|B
-+-+-
1|4|
2|2|
3|3|
In the column B, I would like to get a sequence, which would include increasing sequences with the lengths of the values given in column A. So the above example would be:
The expected sequences:
4 = 1,2,3,4
2 = 1,2
3 = 1,2,3
The expected table:
A|B
---+-
1|4|1
2|2|2
3|3|3
4| |4
5| |1
6| |2
7| |1
8| |2
9| |3
I am currently achieving this by having the following function in B1: ={SEQUENCE(A1);SEQUENCE(A2);SEQUENCE(A3)} but as you can see, if I were to add another value to the column A, I would need to modify the said function. How can I achieve this dynamically?

Another approach (for google sheets). In B1
=Arrayformula(transpose(split(textjoin("~", 1, if(column(1:1)<=A1:A, column(1:1),)), "~")))

say your data starts at A2, then use the following formula:
=ARRAYFORMULA( transpose( split( join("\", LEFT(join( "\", COLUMN(offset($A$1,,,,max($A2:$A))) ), offset($A$2,,,COUNT($A2:$A))*2-1) ) ,"\") ) )
in cell B2. Mind that if you have numbers >= 10 then special care must be taken with text( ... ,"00") and *2 --> *3 and so on, accordingly if you expect for >=100...

Related

How to transpose & split multiple columns and repeat specific cells in a column

I am looking to transpose, split, and keep the correct corresponding Category/Reference Number.
Column A: Category / Reference Number.
Column B: Email (CSV)
| A | B | | A | B |
|001|Email1,Email2,Email3| |001|Email1|
|002|Email4,Email5,Email6| |001|Email2|
| | | |001|Email3|
| | | |002|Email4|
| | | |002|Email5|
| | | |002|Email6|
Here is another post which is similar to what I am looking to accomplish. The only difference is in this post, the OP requested that the formula duplicates data X times. Here is the formula that is used:
=ARRAYFORMULA({TRANSPOSE(SPLIT(CONCATENATE(REPT(B2:B&",", A2:A^2)), ",")),
TRANSPOSE(SPLIT(CONCATENATE(REPT(C2:C&",", A2:A)), ","))})
I have tried modifying this formula by removing the "^2", "A2:A" replacing with a COUNTIF (to determine the number of emails in each row), and keep breaking the formula.
What am I doing wrong?
Here is my sheet.
try:
=ARRAYFORMULA(TRIM(QUERY(SPLIT(FLATTEN(IF(IFERROR(SPLIT('Form Responses'!C2:C, ","))="",,
'Form Responses'!B2:B&"×"&SPLIT('Form Responses'!C2:C, ","))), "×"),
"where Col2 is not null")))

count empty cells until the next filled cell

I have a table similar to the one below and like to automate the calculation of the sum column. The number of rows per day varies. I'm looking for a way to find the number of empty cells in the date column after the current row. This number can then be used to fill the sum column.
Is there regardless of the solution below a way to count the number of empty cells between the dates?
Date |Value|Sum
----------+-----+---
16/07/2020| 2| 5
| 3|
17/07/2020| 2| 10
| 3|
| 5|
18/07/2020| 2| 11
| 3|
| 5|
| 1|
if you start from row 1 use:
=ARRAYFORMULA(IF(A:A="",,VLOOKUP(A:A, QUERY({VLOOKUP(ROW(A:A),
FILTER({ROW(A:A), A:A}, A:A<>""), 2), B:B},
"select Col1,sum(Col2) group by Col1"), 2, 0)))
=ARRAYFORMULA(IFNA(VLOOKUP(A:A, QUERY(IF(B:B="",,VLOOKUP(ROW(A:A),
IF(A:A<>"", {ROW(A:A), A:A}), 2, 1)),
"select Col1,count(Col1)
where Col1 is not null
group by Col1
label count(Col1)''"), 2, 0)))
I don't think you need the answer to your first question to figure out the answer to the Sum.
With the entirety of column C blank, try this in C1:
=ARRAYFORMULA({"Sum";IF(A2:A="",,VLOOKUP(A2:A,QUERY({VLOOKUP(ROW(A2:A),FILTER({ROW(A2:A),A2:A},A2:A<>""),2),B2:B},"Select Col1,SUM(Col2) group by Col1"),2,0))})
If that doesn't work, it might be easier to demonstrate the idea on a sample sheet.
André, try this:
1.) Delete C:C entirely (including the header).
2.) Place the following formula into cell C1:
=ArrayFormula({"Sum";IF(A2:A="","",VLOOKUP(A2:A,QUERY({VLOOKUP(ROW(A2:A),FILTER({ROW(A2:A),A2:A},A2:A<>"",B2:B<>""),2,TRUE),B2:B},"Select Col1, SUM(Col2) Group By Col1"),2,FALSE))})
UPDATE:
Your post example shows headers. The formula I suggested, then, accounted for those headers. Since your actual sample sheet is different and does not use headers like the original post, you'd use this version:
=ArrayFormula(IF(A:A="","",VLOOKUP(A:A,QUERY({VLOOKUP(ROW(A:A),FILTER({ROW(A:A),A:A},A:A<>"",B:B<>""),2,TRUE),B:B},"Select Col1, SUM(Col2) Group By Col1"),2,FALSE)))

Is there an arrayformula to fill all blank cells in a column with the content from the cell above?

In the following Google sheet:
A B
------------------------
1| Tom | something |
2| | something |
3| John | something |
4| Lana | something |
5| | something |
6| | something |
7| Jason | something |
------------------------
I want an array formula applied to column A which will automatically fill any blanks with the last data in the cell above (or above that, if there are multiple blanks).
The result should look like this:
A B
------------------------
1| Tom | something |
2| Tom | something |
3| John | something |
4| Lana | something |
5| Lana | something |
6| Lana | something |
7| Jason | something |
------------------------
As the sheet can be massive, applying the formula to each cell is not practical.
If I paste the formula =if(ISBLANK(A2), A1) into the blanks manually it will fill them, but dragging said formula over a whole column will not work.
Yes you can do it with vlookup, by creating an array that includes a row number and using the inexact lookup form of vlookup so that it finds the previous row that contains text:
=ArrayFormula(query({vlookup(row(A:A),{if(A:A<>"",row(A:A)),A:A},2),B:B},"select Col1,Col2 where Col2 is not null"))
That solution might help from this source:
https://infoinspired.com/google-docs/spreadsheet/fill-blank-cells-with-the-values-above/
The advantage it doesn't add extra rows at the tail end of the column.
= scan(0, indirect("B2:B" & reduce(0, B: B, lambda(a, v, (
if (v = "", v & a, row(v)))))), lambda(a, v,
if (v = "", a, v)))
The simplest way to achieve this goal would be to create a new column, and use the formula =IF(ISBLANK($A2), $B1, $A2) from B2 down. I think that what you meant was that you can't simply replace the column in place, correct? This would solve it in that case, but it is not a single arrayformula. If you are worried about inserting the formula into all rows, try selecting the first cell, CTRL+SHIFT+Down arrow key to select the full range, and CTRL+ENTER to paste the formula.
If you truly need an arrayformula, one would hope that =ArrayFormula(IF(ISBLANK(A2:A), B1:B, A2:A)) or the like would work, but unfortunately Sheets does not iterate over the results of this formula so that it does. Other functions (such as vlookup) do work properly, but I'm not sure how to compose a formula to achieve what you're looking to do here using those.
To answer your Q in one word "NO" (though slightly simplistic).
An approach that might suit is to filter ColumnA to select (Blanks) only and in the first blank cell (in your case A2) enter:
=A1
where the 1 is the number of the row immediately above the formula cell.
Copy down to suit and Edit > Copy > Edit > Paste special > Paste values only, then take off the filter.

Google Sheets formula with variable number of disjoint columns

In the table I have multiple disjoint columns with similar meaning: “is this row interesting?”
I want to create an array formula to get all interesting rows. How can I approach this?
Example table:
Obj id | Case 1 data | Case 1 interesting? | Case 2 data | Case 2 interesting?
1 | … | YES | … | NO
2 | … | NO | … | NO
3 | … | NO | … | YES
4 | … | NO | … | NO
5 | … | YES | … | YES
6 | … | NO | … | NO
The actual table is split into several sheets with different subsets of ids on each sheet.
My current approach is stuck with INDIRECT function not accepting array or ranges. I first search for my columns: FILTER(COLUMN(A1:1), REGEXMATCH(A1:1, "interesting")), then I convert column addresses to ranges, but when I feed the result to INDIRECT, it only returns the first column.
The desired formula would output an array of unique object ids where each row is interesting for at least one case.
UPDATE: here is a test table for this problem. There are 3 sheets: student's data with ids and 2 programs. Each program has several exams (not known beforehand. The desired formula would output an array of unique student ids with at least 1 Passed exam (in the test sheet: 1, 3, 4, 6)
={"Passing"; ARRAYFORMULA(UNIQUE(QUERY({
IF(IFERROR(REGEXEXTRACT(REGEXREPLACE(TRIM(TRANSPOSE(QUERY(
TRANSPOSE('Program 1'!A1:Z), , 999^99))), "Pass", "♠"), "♠"))="♠", 'Program 1'!A1:A, );
IF(IFERROR(REGEXEXTRACT(REGEXREPLACE(TRIM(TRANSPOSE(QUERY(
TRANSPOSE('Program 2'!A1:Z), , 999^99))), "Pass", "♠"), "♠"))="♠", 'Program 2'!A1:A, )},
"where Col1 is not null order by Col1", 0)))}
if you want to VLOOKUP it:
=ARRAYFORMULA(IF(LEN(A2:A), IF(IFERROR(VLOOKUP(A2:A, UNIQUE(QUERY({
IF(IFERROR(REGEXEXTRACT(REGEXREPLACE(TRIM(TRANSPOSE(QUERY(
TRANSPOSE('Program 1'!A1:Z), , 999^99))), "Pass", "♠"), "♠"))="♠", 'Program 1'!A1:A, );
IF(IFERROR(REGEXEXTRACT(REGEXREPLACE(TRIM(TRANSPOSE(QUERY(
TRANSPOSE('Program 2'!A1:Z), , 999^99))), "Pass", "♠"), "♠"))="♠", 'Program 2'!A1:A, )},
"where Col1 is not null", 0)), 1, 0))<>"", "PASS", "FAIL"), ))

google spreadsheets query sql in operator

First I will explain what I am trying to achieve
I have a list with several columns and rows, like this
| a | b | c
-----------------
1 | f1 | 4 | f
2 | f2 | 9 | k
3 | f3 | 1 | x
In another column outside the list I want to write a list with variable size, that contains values of column 'a'.
This list would be for a query like this (suppose that I write the variable list in the column 'd')
=QUERY(A2:D3,"SELECT a, b, c WHERE A IN D")
I think the problem is that I can not use the IN operator from sql
Is there a way to achieve this? The most important part is that I want to write manually a list with variable size for the query?
Thy this formula, in cell E1:
=FILTER(A:C,REGEXMATCH(A:A,JOIN("|",FILTER(D:D,D:D<>""))))
FILTER formula can take any array as condition parameter. REGEXMATCH + JOIN with pipe | will match true when any text from D matches text from A.
This formula is good for one more reason, you may use it as source for further query, like this: =query(my_formula, "select Col1, Col3 where Col2 > 0")

Resources