Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to filter a list and convert into a 2d array while keeping the rich text values with formula in Google Spreadsheet?

I have a list of data where the cells are rich text values with a link.

Data:

Fruits Rank
Apple 2
Orange 1
Pear 1
Cherry 2
Banana 2
Grape 1

My criteria:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

  1. extract Fruits with specified Rank (e.g. 1 or 2)
  2. transpose the output and split it into rows with a specified number of elements (max) in each row
  3. keep the rich text value (link URL) in the output

Example output:

Rank: 1, max in each row: 2


What I have tried:

A link to the sample spreadsheet

1. FILTER

The result keeps the link URL in the rich text value

=FILTER(A:A,B:B=1) in D2

FILTER Output
Orange
Pear
Grape

But I do not know how to convert it into a 2d array nor LIMIT or OFFSET the result.

2. QUERY

I can create the desired 2d array with multiple QUERY

=IFNA(TRANSPOSE(QUERY(A:B,CONCATENATE("SELECT A WHERE B=",1," LIMIT ",2," OFFSET ",(ROWS(A$1:A1)-1)*2),0)),) in F2 and applied to F2:F3

Orange Pear
Grape

But the result does not keep the link URL in the rich text value

3. With helper columns

First, FILTER:

=FILTER(A:A,B:B=1) in I2

FILTER Output
Orange
Pear
Grape

Second, convert into 2d array with OFFSET:

=OFFSET($I$2,COLUMNS($I2:I2)-1+(ROWS($2:2)-1)*2,0) in K2 and applied to K2:L3

This outputs what I want but I do not want to use a helper column.


My question:

Is it able and how to achieve with formula and without helper columns?

>Solution :

Here’s how you can do that:

=ArrayFormula(LAMBDA(rank,max,IFNA(VLOOKUP(SEQUENCE(ROUNDUP(
COUNTIF(B2:B,rank)/max),max),{SEQUENCE(COUNTIF(B2:B,rank)),
FILTER(A2:A,B2:B=rank)},2,0)))(1,2))

But I suggest creating a Named Function _WRAPROWS from Data > Named functions > Create function and using it like this:

=_WRAPROWS(FILTER(A2:A,B2:B=1),2,)

Where _WRAPROWS is defined as:

=ARRAYFORMULA(IFNA(VLOOKUP(SEQUENCE(CEILING(
ROWS(FLATTEN(vector))/wrap_count),wrap_count),
{SEQUENCE(ROWS(FLATTEN(vector))),FLATTEN(vector)},2,0),pad_with))

Arguments:

  • vector – The array to wrap.
  • wrap_count – The maximum number of values for each row.
  • pad_with – The value with which to pad.

See WRAPROWS in Google Sheets for more information.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading