Split 1 column into multiple columns with up to 500 cells each - google-sheets

In Google Sheets, I have a column that is arbitrarily long.
I want to split this column into separate columns of length 500.
How can I do this?
Some things I've been thinking may be involved in a solution:
TRANSPOSE
ARRAY_CONSTRAIN

Arrayformula, an example for number 5, change to 500.
=ArrayFormula(IFERROR(
vlookup(
(TRANSPOSE(ROW(INDIRECT("a1:a"&ROUNDUP(COUNTA(A:A)/5))))-1)*5 + ROW(INDIRECT("a1:a"&5)),
{ROW(A:A),A:A},2,)
))
ROUNDUP(COUNTA(A:A)/5 the number of columns. Up because the last column may contain less than N rows.
TRANSPOSE(...)*5 + ROW(INDIRECT("a1:a"&5)) to get matrix of numbers.
Matrix:
1 6 11 16
2 7 12 17
3 8 13 18
4 9 14 19
5 10 15 20
{ROW(A:A),A:A} to get the number of a row and value to return
vlookup to return a value
IFERROR to show "" if error.

Having an arbitrarily long column implies you'll need arbitrarily many columns for the split, and spreadsheet formulas cannot create new columns. So the best we can is to use all columns available.
Assuming the data column begins with cell A1, and the upper left corner of the range in which it should be split is B1, the following formula will work, if you fill the first 500 rows of the sheet with it:
=offset($A$1, row()-row($B$1) + 500*(column()-column($B$1)), 0)
Otherwise, change A1 and B1 to the top source and upper-left corner of destination.
Explanation: the offset moves from A1 down by the specified amount, which increments by 1 with every row and by 500 with every column.

You could also use this formula:
=ARRAYFORMULA(
TRIM(
SPLIT(
TRANSPOSE(
SPLIT(
QUERY(
<long_column_range> & "," & IF(
MOD(1, <columns>) = 0, "|", ""
),, 9^9
), "|"
)
), ","
)
)
)
where:
<long_column_range> is the range of the long column that you want to split (e.g. A1:A) and
<columns> is the number of columns that you want for the long column to be split into.
Taken from this article.

Related

Count of items in column A not in column B (Google sheets)

I have two columns of numbers in Google sheets. I’m trying to find a formula to give me a count of items in column A that are not in column B. The numbers in the columns are descending and unique in each column but can be duplicated across columns. The columns can also have different amounts of items in them.
Column A has 5 4 3 1
Column B has 4 2 1
The answer is this case would be 2 as the numbers 5 and 3 in column A are not in column B.
I’ve tried using sum, if and countif but can’t come up with a solution. Also not sure if this would be an array formula or not.
Without a helper column you can use reduce and lambda functions.
=reduce(0, A:A, LAMBDA(prev, a_value, prev + if(not(a_value = ""), iferror(min(0, match(a_value, B:B, false)), 1), 0)))
Fill column c with this formula:
=iferror(min(0,match(A1,B:B,false)),1)
If A1 is in column b the match function will return an value, which is then reduced to 0 by the min function. Otherwise match will return an error, and the iferror will return 1.
Then you just need to sum column c.

Calculate the average of sets of cells satisfying a certain criterion (AVERAGEIF)

This is my Items sheet
And this is my Values sheet
As can be seen from the table, alfa is associated with 20 40 60 80, beta with 30 40 70 80 and gamma with 50 60 70 80.
In the Items sheet in cell B1 (next to the first item) I would like a formula (Arrayformula or alike) generating the average value for each item. In my example it should be:
alfa -> 50 (that is: (20+40+60+80)/4 = 200/4)
beta -> 55 (that is: (30+40+70+80)/4 = 220/4)
gamma-> 65 (that is: (50+60+70+80)/4 = 260/4)
So the final result should be:
This is my googlesheet: example
P.S. For simplicity's sake, I used just columns A:C for items in Values sheet. In real case I have 10 columns so I want to avoid to specify each one in the formula and instead use a range.
try:
=BYROW(A1:A3, LAMBDA(x, INDEX(QUERY(SPLIT(FLATTEN(Values!A1:C10&"​"&Values!D1:D10), "​"),
"select avg(Col2) where Col1 = '"&x&"'"), 2)))
update
=IFERROR(BYROW(A1:INDEX(A:A, MAX(ROW(A:A)*(A:A<>""))),
LAMBDA(x, INDEX(QUERY(SPLIT(FLATTEN(
FILTER(Values!A:C, Values!D:D<>"")&"​"&
FILTER(Values!D:D, Values!D:D<>"")), "​"),
"select avg(Col2) where Col1 = '"&x&"'"), 2))))

Sum first N values in a Comma Separated List (Google Sheets)

Given the following conditions:
A cell containing a Comma Separated List (Column B in table)
A cell containing how many values to sum from the Comma Separated List (Column A in table)
Data
MAX INDEX
COMMA SEPARATED LIST TO SUM
EXPECTED MATH
EXPECTED VALUE
5
1,2,3,4,5,6,7,8,9,10,11,12,13,14,15
=1+2+3+4+5
15
3
5,10,15,20,25,30,35,40,45,50
=5+10+15
30
image
I have seen formulas using OFFSET, but in application, OFFSET doesn't seem to work with a CS List (only an actual range).
Is there a formula that will sum up only the first N values in a Comma Separated List without using helper columns? (So the entirety of the operation will consist of only three cells... MAX Index value, the Comma Separated List, and the output cell containing the formula).
try:
=INDEX(IFERROR(1/(1/BYROW(
IF(SEQUENCE(1, COLUMNS(SPLIT(B2:B, ",")))<=A2:A, SPLIT(B2:B, ","), ),
LAMBDA(xx, SUM(xx))))))
or alternative:
=INDEX(IFERROR(1/(1/
BYROW(IFERROR(SPLIT(REGEXEXTRACT(B2:B5&",",
BYROW(A2:A5, LAMBDA(xx, JOIN(, REPT("\d+,", xx))))), ",")),
LAMBDA(yy, SUM(yy))))))
or non-lambda alternative:
=FLATTEN(INDEX(IFERROR(1/(1/QUERY(TRANSPOSE(
IF(SEQUENCE(1, COLUMNS(SPLIT(B2:B, ",")))<=A2:A, SPLIT(B2:B, ","), )*1),
"select "&TEXTJOIN(",", 1, "sum(Col"&SEQUENCE(ROWS(A2:A))&")")))),2))
to quickly confuse noobs:
=INDEX(BYROW(BYROW(
IF(SEQUENCE(1, COLUMNS(SPLIT(B2:B, ",")))<=
INDIRECT("A2:A"&MAX((A2:A<>"")*ROW(A2:A))), SPLIT(B2:B, ","), )*1,
LAMBDA(aa, JOIN("+", aa))),
LAMBDA(xx, INDEX(QUERY(, "select "&xx), 2))))
=sum(array_constrain(split(B2, ","),1, A2))
Explanation:
sum over
a constrained array (of A2 columns and 1 row)
formed by splitting the contents of B2 on ,
Use this
=MAP(B2:B,A2:A, LAMBDA(rg,idx, IF(rg="",, ARRAYFORMULA(SUM(ARRAY_CONSTRAIN(SPLIT(rg,", ",0),1,idx))))))
Demo
Used formulas help
MAP - LAMBDA - IF - ARRAYFORMULA - SUM - SPLIT
=MAP(
A2:INDEX(A2:A,COUNTA(A2:A)),
B2:INDEX(B2:B,COUNTA(A2:A)),
LAMBDA(a,b,
QUERY(
QUERY(SPLIT(b,","),
"Select "&ARRAYFORMULA(JOIN("+","Col"&SEQUENCE(a))),0
)
,"offset 1",0)
)
)
SPLIT by comma, then use QUERY to do the addition. The addition string Col1+Col2+.. is created dynamically using JOIN

Combine a SPLIT formula with a formula that chooses N unique words from the SPLIT outcome

I got a sentence which I SPLIT into words without the punctuation. Next I want to choose three random, but unique words from that split. I use the formula as seen in cell I2. Is it possible to combine both the SPLIT formula and the other formula into one (big) formula?
SPLIT formula:
=ARRAYFORMULA(REGEXREPLACE(SPLIT(A2," "),"[,.?!]",""))
Formula to choose three random unique words:
=ARRAYFORMULA(ARRAY_CONSTRAIN(SPLIT(FLATTEN(QUERY(QUERY(QUERY(SPLIT(FLATTEN(
ROW(B2:G2)&"×"&RANDARRAY(ROWS(B2:G2), COLUMNS(B2:G2))&"×"&B2:G2), "×"),
"select max(Col3) group by Col2 pivot Col1"),
"offset 1", 0),,9^9)), " "), 9^9, 3))
I understand that you want to get 3 random unique words from a string.
in what follows i am going to demonstrate how get truly random words when the sheet is modified plus handling exceptions, ponctuation and more, like this take a look at this sheet.
Solution:
Notes:
This solution handels punctuation notice the highlighted characters with yellow.
To get N unique random words just replace [n] of SORTN Function with a cell refrence.
Paste this formula in B2.
=ArrayFormula(IF(A2="",,JOIN(" ,",TRANSPOSE(QUERY(SORTN({RANDARRAY(COUNTA(UNIQUE(SPLIT(TRIM(REGEXREPLACE(A2,"[[:punct:]]",""))," ")))),TRANSPOSE(UNIQUE(SPLIT(TRIM(REGEXREPLACE(A2,"[[:punct:]]",""))," ")))},3,,1,RANDBETWEEN(0,1))," Select Col2 ")))))
Explanation: Pending...
1 - We need UNIQUE(SPLIT(TRIM(REGEXREPLACE(A2,"[[:punct:]]",""))," ")) to rplace punctuation with nothing "" and TRIM spaces in start, tailing and additional spaces, SPLIT the string with " " as a delimiter, and then get the UNIQUE columns resulted from SPLIT, which is He|is|cunning|as|a|fox and TRANSPOSE the output like this TRANSPOSE(UNIQUE([Output])
to join it with random numbers column later.
2 - we need an Array {} that contain He|is|cunning|as|a|fox and column with random numbers , like this { RANDARRAY , He|is|cunning|as|a|fox }.
To get the column with random numbers: RANDARRAY(COUNTA(UNIQUE(SPLIT(TRIM(REGEXREPLACE(A2,"[[:punct:]]",""))," "))))
RANDARRAY takes [columns] set to 1 and [rows] set to COUNTA(UNIQUE(SPLIT(TRIM(REGEXREPLACE(A2,"[[:punct:]]",""))," "))) which is the COUNTA( He|is|cunning|as|a|fox )
3 - Now we have to SORTN the output with [n] set to 3 meaning 3 words in this case
"to get N unique random words" just replace [n] with a cell refrence.
[sort_column] set to 1 the column of random number and [is_ascending] set to RANDBETWEEN(0,1) to get either 0 or 1, [is_ascending] 0 means Flase it sort's Descending , 1 means True sort ascending.
4 - QUERY " Select Col2 ", the randomized column of words.
5 - TRANSPOSE the column.
6 - JOIN with " ,"
After researching for a while I came across the use of array_constrain to pick a fixed number of results and sort with randarray to randomize the outcome.
=ARRAY_CONSTRAIN(
transpose(SORT(transpose(ARRAYFORMULA(REGEXREPLACE(SPLIT(A2," "),"[,.?!]",""))),
randarray(COUNTA(ARRAYFORMULA(REGEXREPLACE(SPLIT(A2," "),"[,.?!]","")))),true))
,1,3)
If anyone happens to have a better solution to this, I would gladly see a response.

Condtional formatting to highlight duplicates fetching across multiple columns

I have a sheet where I need to highlight the the second duplicates if they are same across multiple columns.
Sl. No. Name Age Target(Millions)
1 ABC 30 2.3
2 DEF 40 1.3
3 ABC 30 4.3
4 GHI 44 0.3
5 JKL 33 6.3
For example the serial number 3 column Name and Age is to be highlighted because its a duplicate of serial 1 Name and Age
Note that not both rows to be highlighted.
I tried with
=AND(countif($B$2:$C,B2)>1, countif($B$2:$C,C2)>1)
This should do it. I made it scalable for if you want to add more columns:
=ROWS(QUERY(ArrayFormula(TO_TEXT(ARRAY_CONSTRAIN($A:$D, ROW()-1, COUNTA(1:1)))), "select "&JOIN(",", ArrayFormula("Col"&({2,3})))&" where "&JOIN(" and ", ArrayFormula("Col"&({2,3})&"='"&ArrayFormula(HLOOKUP(ArrayFormula(VLOOKUP($A$1,$1:$1,({2,3}),0)),$A:$D,ROW(),0))&"'"))))
Readable:
=ROWS(
QUERY(
ArrayFormula(
TO_TEXT(ARRAY_CONSTRAIN(
$A:$D,
ROW()-1,
COUNTA(1:1)
))
),
"select "&
JOIN(
",",
ArrayFormula("Col"&({2,3}))
)&
" where "&
JOIN(
" and ",
ArrayFormula(
"Col"&
({2,3})&"='"&
ArrayFormula(
HLOOKUP(
ArrayFormula(
VLOOKUP(
$A$1,
$1:$1,
({2,3}),
0
)
),
$A:$D,
ROW(),
0
)
)&
"'"
)
)
)
)
Highlight cell if the number of rows with matching cells in previous rows is >0. To add more columns, you'll have to add to every instance of the {2,3}'s. For example, if you want to include D, add a 4 to the arrays.
If you expect to change which columns you want a lot, you can create a separate column elsewhere with the column indexes that you want, then use FILTER in place of the array to save typing.
This highlights any duplicate cell after the original as well.

Resources