Splitting a column into multiple columns with Query

jonybandana

New Member
Joined
Dec 16, 2022
Messages
33
Office Version
  1. 365
Platform
  1. Windows
Hi everyone. I have something like the following table (imported from SAP):

TITLE
0- Part 123
D142
D542
S653
0- Part 453
T311
Y221
K411
M091
J818

I would like to divide this data into columns in the following way:

0- Part 1230- Part 453
S653T311
D142Y221
D542K411
M091
J818

This would have to be done dynamically, because I have a lot of rows that begin with "0 - " and I would like each one to get its own column.

Is this possible to do?
 

Excel Facts

Can you sort left to right?
To sort left-to-right, use the Sort dialog box. Click Options. Choose "Sort left to right"
I can't help thinking I'm missing something obvious, but something like this should work:

Power Query:
let
    Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"TITLE", type text}}),
    #"Added Custom" = Table.AddColumn(#"Changed Type", "GroupName", each if Text.Start([TITLE],2) = "0-" then [TITLE] else null),
    #"Filled Down" = Table.FillDown(#"Added Custom",{"GroupName"}),
    #"Grouped Rows" = Table.Group(#"Filled Down", {"GroupName"}, {{"Data", each _[TITLE]}}),
    #"Removed Other Columns1" = Table.SelectColumns(#"Grouped Rows",{"Data"}),
    #"Transposed Table" = Table.Transpose(#"Removed Other Columns1"),
    #"Added Custom1" = Table.AddColumn(#"Transposed Table", "Custom", each Table.FromColumns(Record.ToList(_), Table.ColumnNames(#"Transposed Table"))),
    #"Removed Other Columns" = Table.SelectColumns(#"Added Custom1",{"Custom"}),
    #"Expanded Custom" = Table.ExpandTableColumn(#"Removed Other Columns", "Custom", Table.ColumnNames(#"Removed Other Columns"{0}[Custom])),
    #"Promoted Headers" = Table.PromoteHeaders(#"Expanded Custom", [PromoteAllScalars=true])
in
    #"Promoted Headers"
 
Upvote 1
If your interested in a formula.
Fluff.xlsm
ABCDE
1TITLE
20- Part 1230- Part 1230- Part 4530- part 567
3D142D142T311a123
4D542D542Y221b567
5S653S653K411c999
60- Part 453M091d785
7T311e354
8Y221
9K411
10M091
110- part 567
12a123
13b567
14c999
15d785
16e354
17
Sheet7
Cell Formulas
RangeFormula
C2:E7C2=LET(a,VSTACK(FILTER(ROW(A1:A200),LEFT(A1:A200,2)="0-"),COUNTA(A1:A200)+1),m,MAX(DROP(a,1)-DROP(a,-1)),DROP(REDUCE("",SEQUENCE(ROWS(a)-1),LAMBDA(x,y,HSTACK(x,EXPAND(CHOOSEROWS(A1:A200,SEQUENCE(INDEX(a,y+1,1)-INDEX(a,y,1),,INDEX(a,y,1))),m,,"")))),,1))
Dynamic array formulas.
 
Upvote 1
I can't help thinking I'm missing something obvious, but something like this should work:

Power Query:
let
    Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"TITLE", type text}}),
    #"Added Custom" = Table.AddColumn(#"Changed Type", "GroupName", each if Text.Start([TITLE],2) = "0-" then [TITLE] else null),
    #"Filled Down" = Table.FillDown(#"Added Custom",{"GroupName"}),
    #"Grouped Rows" = Table.Group(#"Filled Down", {"GroupName"}, {{"Data", each _[TITLE]}}),
    #"Removed Other Columns1" = Table.SelectColumns(#"Grouped Rows",{"Data"}),
    #"Transposed Table" = Table.Transpose(#"Removed Other Columns1"),
    #"Added Custom1" = Table.AddColumn(#"Transposed Table", "Custom", each Table.FromColumns(Record.ToList(_), Table.ColumnNames(#"Transposed Table"))),
    #"Removed Other Columns" = Table.SelectColumns(#"Added Custom1",{"Custom"}),
    #"Expanded Custom" = Table.ExpandTableColumn(#"Removed Other Columns", "Custom", Table.ColumnNames(#"Removed Other Columns"{0}[Custom])),
    #"Promoted Headers" = Table.PromoteHeaders(#"Expanded Custom", [PromoteAllScalars=true])
in
    #"Promoted Headers"
Beautiful!

If your interested in a formula.
Fluff.xlsm
ABCDE
1TITLE
20- Part 1230- Part 1230- Part 4530- part 567
3D142D142T311a123
4D542D542Y221b567
5S653S653K411c999
60- Part 453M091d785
7T311e354
8Y221
9K411
10M091
110- part 567
12a123
13b567
14c999
15d785
16e354
17
Sheet7
Cell Formulas
RangeFormula
C2:E7C2=LET(a,VSTACK(FILTER(ROW(A1:A200),LEFT(A1:A200,2)="0-"),COUNTA(A1:A200)+1),m,MAX(DROP(a,1)-DROP(a,-1)),DROP(REDUCE("",SEQUENCE(ROWS(a)-1),LAMBDA(x,y,HSTACK(x,EXPAND(CHOOSEROWS(A1:A200,SEQUENCE(INDEX(a,y+1,1)-INDEX(a,y,1),,INDEX(a,y,1))),m,,"")))),,1))
Dynamic array formulas.

Wow! This is also nice!

Sorry for hijacking the question, but I can't stop saying these two are beautiful!
 
Upvote 0
Here one more

Book1
ABCDEFG
1TITLE0- Part 1230- Part 4530- part 567
20- Part 123D142T311a123
3D142D542Y221b567
4D542S653K411c999
5S653M091d785
60- Part 453e354
7T311
8Y221
9K411
10M091
110- part 567
12a123
13b567
14c999
15d785
16e354
17
18
Sheet1
Cell Formulas
RangeFormula
D1:F6D1=LET(r,Table1[TITLE],z,HSTACK(r,SCAN(TAKE(r,1),r,LAMBDA(a,b,IF(LEFT(b,2)="0-",b,a)))),c,INDEX(z,,2),u,UNIQUE(c),IFERROR(DROP(REDUCE("",u,LAMBDA(x,y,HSTACK(x,FILTER(r,c=y)))),,1),""))
Dynamic array formulas.
 
Upvote 0
And one more Power Query solution

Power Query:
let
    Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],
    cTypes = Table.TransformColumnTypes(Source,{{"TITLE", type text}}),
    nCol = Table.AddColumn(cTypes, "Group", each if Text.Start([TITLE],2) = "0-" then [TITLE] else null),
    fill = Table.FillDown(nCol,{"Group"}),
    grp = Table.Group(fill, {"Group"}, {{"Count", each _[TITLE]}}),
    result = Table.PromoteHeaders(Table.FromColumns(grp[Count]))
in
    result
 
Upvote 1
VBA as well :)

VBA Code:
Sub jec()
 Dim ar, sq, j As Long
 ar = Sheets(1).ListObjects(1).DataBodyRange
 ReDim a(UBound(ar))
 
 With CreateObject("scripting.dictionary")
   For j = 1 To UBound(ar)
     If Left(ar(j, 1), 2) = "0-" Then
       sq = .Item(ar(j, 1))
       If IsEmpty(sq) Then sq = a
       sq(0) = ar(j, 1)
       sq(UBound(sq)) = sq(UBound(sq)) + 1
       .Item(ar(j, 1)) = sq
     Else
       sq(sq(UBound(sq))) = ar(j, 1)
       sq(UBound(sq)) = sq(UBound(sq)) + 1
      .Item(sq(0)) = sq
     End If
   Next
  Sheets(1).Range("D1").Resize(UBound(sq), .Count) = Application.Transpose(.items)
 End With
End Sub
 
Upvote 0

Forum statistics

Threads
1,223,939
Messages
6,175,533
Members
452,652
Latest member
eduedu

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