Split cell by delimiter into specific columns

Danny54

Active Member
Joined
Jul 3, 2019
Messages
295
Office Version
  1. 365
Platform
  1. Windows
Is it possible to use Power Query to split comma delimited data in a excel worksheet into specific new columns?

the actual input sheet has 120,000 rows
the actual input sheet has 2 cols
the comma delimited data in column b can be split into 160 new cols

Input sheet
1630514280158.png


output sheet (notice all the a's are lined up in one column, all the d's in one column ect..
1630514343913.png


Thanks
 

Excel Facts

How to change case of text in Excel?
Use =UPPER() for upper case, =LOWER() for lower case, and =PROPER() for proper case. PROPER won't capitalize second c in Mccartney
Yes, split into rows. Compare the split col to a list of possible values. Get the position of the value in the list. Pivot on this position, use no aggregation.
 
Upvote 0
Thanks for the reply.
I have successfully loaded and split the values by the comma delimiter, however I'm not certain how to align each column by its value (see last insert)

1630526942632.png



let
Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],
#"Changed Type" = Table.TransformColumnTypes(Source,{{"Column1", type text}, {"Column2", type text}}),
#"Promoted Headers" = Table.PromoteHeaders(#"Changed Type", [PromoteAllScalars=true]),
#"Changed Type1" = Table.TransformColumnTypes(#"Promoted Headers",{{"Color", type text}, {"Values", type text}}),
#"Split Column by Delimiter" = Table.SplitColumn(#"Changed Type1", "Values", Splitter.SplitTextByDelimiter(",", QuoteStyle.Csv), {"Values.1", "Values.2", "Values.3", "Values.4"}),
#"Changed Type2" = Table.TransformColumnTypes(#"Split Column by Delimiter",{{"Values.1", type text}, {"Values.2", type text}, {"Values.3", type text}, {"Values.4", type text}})
in
#"Changed Type2"


1630527070051.png




so, how would i go about shifting and aligning cell data to create the following using mcode (if its possible)

1630527898577.png


Thanks
 
Upvote 0
try

Power Query:
let
    Source = Table.Buffer(Excel.CurrentWorkbook(){[Name="Table1"]}[Content]),
    tbl1 = Table.AddColumn(Source, "Custom", each Text.Split([Values],",")),
    tblRemoveColumns = Table.RemoveColumns(tbl1,{"Values"}),
    tblExpandCustom = Table.ExpandListColumn(tblRemoveColumns, "Custom"),
    tblDuplicateColumn = Table.DuplicateColumn(tblExpandCustom, "Custom", "Custom - Copy"),
    tblPivotColumn = Table.Buffer(Table.Pivot(tblDuplicateColumn, List.Distinct(tblDuplicateColumn[Custom]), "Custom", "Custom - Copy")),
    lstHeaders = List.Buffer(List.LastN(Table.ColumnNames(tblPivotColumn),Table.ColumnCount(tblPivotColumn)-1)),
    tbl2 = Table.AddIndexColumn(Source,"Index",0,1,Int64.Type),
    tblMergeQueries = Table.NestedJoin(tbl2, {"Color"}, tblPivotColumn, {"Color"}, "tbl2", JoinKind.LeftOuter),
    tblExpandedtbl2 = Table.ExpandTableColumn(tblMergeQueries, "tbl2", lstHeaders, lstHeaders),
    tblSortRows = Table.Sort(tblExpandedtbl2,{{"Index", Order.Ascending}}),
    tblUnsorted = Table.RemoveColumns(tblSortRows,{"Index", "Values"}),
    lstSortOrder = {"Color"} & List.Sort(List.LastN(Table.ColumnNames(tblUnsorted),Table.ColumnCount(tblUnsorted)-1)),
    tblSorted = Table.ReorderColumns(tblUnsorted,lstSortOrder)
in
    tblSorted
 
Upvote 0
You could also split into rows, duplicate the column, then pivot:

Power Query:
let
    Source = Excel.CurrentWorkbook(){[Name="Table2"]}[Content],
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"Color", type text}, {"Values", type text}}),
    #"Split Column by Delimiter" = Table.ExpandListColumn(Table.TransformColumns(#"Changed Type", {{"Values", Splitter.SplitTextByDelimiter(",", QuoteStyle.Csv), let itemType = (type nullable text) meta [Serialized.Text = true] in type {itemType}}}), "Values"),
    #"Changed Type1" = Table.TransformColumnTypes(#"Split Column by Delimiter",{{"Values", type text}}),
    #"Duplicated Column" = Table.DuplicateColumn(#"Changed Type1", "Values", "Values - Copy"),
    #"Pivoted Column" = Table.Pivot(#"Duplicated Column", List.Distinct(#"Duplicated Column"[Values]), "Values", "Values - Copy")
in
    #"Pivoted Column"
 
Upvote 0
My original idea was as such
Power Query:
let
    Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],
    ValuesToList = Table.AddColumn(Source, "List", each List.Select(Text.Split([Values],","),each _<>"")),
    SortedHeaders = List.Buffer(List.Sort(List.Distinct(List.Combine(ValuesToList[List])))),
    ExpandList = Table.ExpandListColumn(ValuesToList, "List"),
    Positions = Table.AddColumn(ExpandList, "Positions", each List.PositionOf(SortedHeaders,[List])),
    PivotByPosition = Table.Pivot(Table.TransformColumnTypes(Positions, {{"Positions", type text}}, "en-BE"), List.Sort(List.Distinct(Table.TransformColumnTypes(Positions, {{"Positions", type text}}, "en-BE")[Positions])), "Positions", "List")
in
    PivotByPosition

Then like @RoryA, I thought about duplicating the column. A cup of coffee in the morning can do wonders sometimes.
Just a small tweak, I would also sort the headers.

Power Query:
let
    Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],
    ValuesToList = Table.AddColumn(Source, "List", each List.Select(Text.Split([Values],","),each _<>"")),
    ExpandList = Table.ExpandListColumn(ValuesToList, "List"),
    DuplicateValue = Table.DuplicateColumn(ExpandList, "List", "Value"),
    SortedPivotHeaders = Table.Pivot(DuplicateValue, List.Sort(List.Distinct(DuplicateValue[List])), "List", "Value")
in
    SortedPivotHeaders
 
Upvote 0
for your reference:
1630595331633.png

Power Query:
let
    Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],
    res = let 
              a=List.Distinct(Text.Split(Text.Combine(Source[Values],","),","))
          in
              Table.SplitColumn(
                  Table.TransformColumns(
                                          Source,
                                          {"Values", each List.Transform(a,(x)=>if Text.Contains(_,x) then x else null)}
                                        ),
                                "Values",
                                each _
                               )
in
    res
拆分列.xlsx
ABCDEFGHIJK
1ColorValuesColorValues.1Values.2Values.3Values.4Values.5Values.6Values.7
2Reda,d,j,zRedadjz
3Orangea,bb,c,dOrangeadbbc
4GreenbbGreenbb
5Brownd,fBrowndf
6
7
8
Sheet1
 
Upvote 0
Power Query:
let
    Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],
    a = Table.AddColumn(
            Table.ExpandListColumn(
                Table.TransformColumns(
                                        Source,
                                        {"Values",each Text.Split(_,",")}
                                      ),
                                  "Values"
                                  ),
                       "n",
                       each [Values]
                      ),
    b = Table.Sort(Table.Pivot(a, List.Distinct(a[n]), "n", "Values"),{"Color",1})
in
    b
拆分列.xlsx
ABCDEFGHIJK
1ColorValuesColorValues.1Values.2Values.3Values.4Values.5Values.6Values.7
2Reda,d,j,zRedadjz
3Orangea,bb,c,dOrangeadbbc
4GreenbbGreenbb
5Brownd,fBrowndf
6
7Coloradjzbbcf
8Redadjz
9Orangeadbbc
10Greenbb
11Browndf
Sheet1
 
Upvote 0
How would you change these (posts 5, 6, 8) to ensure the output color column remains in the same order as the Source?
 
Upvote 0

Forum statistics

Threads
1,223,701
Messages
6,173,915
Members
452,537
Latest member
the little giant

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