How to conditionally use function TOROW

viva4567

New Member
Joined
Sep 17, 2024
Messages
1
Office Version
  1. 365
Platform
  1. MacOS
I've been given a csv file with multiple rows of data for each account. Most have two rows of data and I've been able to learn how to use TOROW to move all that into one row so I can further sort and analyze the data. Some rows have an additional row of that needs to handled in the same way, that is, appending the third row to the single new row created by TOROW. I don't know how to do this. A sample of the data format is below.
My Excel skills are pretty basic so if anyone can offer help, please keep that in mind.

Many thanks!
 

Attachments

  • Screen Shot 2024-09-17 at 12.35.09 PM.png
    Screen Shot 2024-09-17 at 12.35.09 PM.png
    50.2 KB · Views: 13

Excel Facts

How can you automate Excel?
Press Alt+F11 from Windows Excel to open the Visual Basic for Applications (VBA) editor.
Hello, test the following to see if it is of any use:

Excel Formula:
=LET(
array,SUBSTITUTE(A26:E34,"",""),
a,A26:A34,
b,SCAN(0,--(a<>""),LAMBDA(a,b,a+b)),
c,IFNA(DROP(REDUCE("",UNIQUE(b),LAMBDA(x,y,VSTACK(x,TOROW(FILTER(array,b=y))))),1),""),
d,ISNUMBER(--(c)),
IF(d=TRUE,c*1,c))
 
Upvote 0
An alternative approach:

If the number of rows per record was consistent throughout the entire dataset, you could simply use WRAPROWS-TOCOL. For example, if the number of rows was always 2, the generic syntax would be:

Excel Formula:
=WRAPROWS(TOCOL(range), COLUMNS(range)*2)

The same approach can also be used when the number of rows per record varies by "padding" the dataset with blank rows, so each record contains the same number of rows. With your sample data, the following formula could be used:

Excel Formula:
=LET(
    data, A26:E34,
    names, TAKE(data,, 1),
    id, SCAN(1, names, LAMBDA(a,v, a+(v<>""))),
    keyIds, UNIQUE(id),
    rowCount, MMULT(N(keyIds=TOROW(id)), id^0),
    maxCount, MAX(rowCount),
    newIds, TOCOL(IFS(maxCount-rowCount>=SEQUENCE(, maxCount), keyIds), 2),
    resize, SORTBY(EXPAND(IF(ISBLANK(data), "", data), ROWS(id)+ROWS(newIds),, ""), VSTACK(id, newIds)),
    WRAPROWS(TOCOL(resize), COLUMNS(data)*maxCount)
)

There are many variations this approach can take depending on the situation and the desired output. Cheers!
 
Upvote 0

Forum statistics

Threads
1,221,544
Messages
6,160,428
Members
451,645
Latest member
androidmj

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top