Power Query--Split Multiple Columns at one time

alansidman

Well-known Member
Joined
Feb 26, 2007
Messages
8,342
Office Version
  1. 365
Platform
  1. Windows
In attached is a sample. I wish to split the columns that have been merged with the pipe delimiter. I now wish to split each of these columns. I can do this one column at a time, but I wish to do this with one line of Mcode.

Book1
ABCDEF
1Column101234
2xyz@abc.comCom1|view onlycom2|view only
3yog@abc.comCom1|view onlycom2|view onlycom3|view only
Sheet2
 

Excel Facts

Can a formula spear through sheets?
Use =SUM(January:December!E7) to sum E7 on all of the sheets from January through December
Rich (BB code):
let
    Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],
    Merge = Table.CombineColumns(Source,{"Column1", "0", "1", "2", "3", "4"},Combiner.CombineTextByDelimiter("|", QuoteStyle.None),"Merged"),
    Split = Table.SplitColumn(Merge, "Merged", Splitter.SplitTextByDelimiter("|", QuoteStyle.Csv), {"Merged.1", "Merged.2", "Merged.3", "Merged.4", "Merged.5", "Merged.6", "Merged.7", "Merged.8", "Merged.9"})
in
    Split
ms.png
 
Upvote 0
or even like this
ms.png

Rich (BB code):
let
    Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],
    TCC = Table.CombineColumns(Source,{"0", "1", "2", "3", "4"},Combiner.CombineTextByDelimiter("|", QuoteStyle.None),"Merged"),
    Split = Table.SplitColumn(TCC, "Merged", Splitter.SplitTextByDelimiter("|", QuoteStyle.Csv), {"Merged.1", "Merged.2", "Merged.3", "Merged.4", "Merged.5", "Merged.6", "Merged.7", "Merged.8"}),
    Demote = Table.DemoteHeaders(Split),
    TransposeA = Table.Transpose(Demote),
    Replace = Table.ReplaceValue(TransposeA,"Merged.","",Replacer.ReplaceText,{"Column1"}),
    TransposeB = Table.Transpose(Replace),
    Promote = Table.PromoteHeaders(TransposeB, [PromoteAllScalars=true])
in
    Promote
but M is much longer
 
Upvote 0
or like this
ms.png

Rich (BB code):
let
    Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],
    TCC = Table.CombineColumns(Source,{"0", "1", "2", "3", "4"},Combiner.CombineTextByDelimiter("|", QuoteStyle.None),"Merged"),
    Split = Table.SplitColumn(TCC, "Merged", Splitter.SplitTextByDelimiter("|", QuoteStyle.Csv), {"Merged.1", "Merged.2", "Merged.3", "Merged.4", "Merged.5", "Merged.6", "Merged.7", "Merged.8"}),
    Demote = Table.DemoteHeaders(Split),
    TransposeA = Table.Transpose(Demote),
    Index = Table.AddIndexColumn(TransposeA, "Index", -1, 1),
    TypeText = Table.TransformColumnTypes(Index,{{"Index", type text}}),
    Replace = Table.ReplaceValue(TypeText,"-1","Column1",Replacer.ReplaceText,{"Index"}),
    TRC = Table.RemoveColumns(Replace,{"Column1"}),
    TSC = Table.SelectColumns(TRC,{"Index", "Column2", "Column3"}),
    TransposeB = Table.Transpose(TSC),
    Promote = Table.PromoteHeaders(TransposeB, [PromoteAllScalars=true])
in
    Promote
as you know there are many ways to do that
 
Upvote 0
one more solution
Code:
let
  Source = Excel.CurrentWorkbook(){[Name = "Table1"]}[Content],
  Result = List.Accumulate(
    Table.ColumnNames(Source), 
    Source, 
    (st, cur) => 
      if List.AnyTrue(List.Transform(Table.Column(st, cur), each Text.Contains(_, "|")))
      then Table.SplitColumn(st, cur, Splitter.SplitTextByDelimiter("|", QuoteStyle.Csv), 2)
      else st
  )
in
  Result
 
Upvote 0
thanks Zbyszek ;)
you show me that: Nothing should be done in haste but gripping a flea
Rich (BB code):
let
    Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],
    ReplacedNull = Table.ReplaceValue(Source,null,"|",Replacer.ReplaceValue,{"Column1", "0", "1", "2", "3", "4"}),
    TCC = Table.CombineColumns(ReplacedNull,{"0", "1", "2", "3", "4"},Combiner.CombineTextByDelimiter("|", QuoteStyle.None),"Merged"),
    SplitCount = Table.AddColumn(TCC, "SCount", each List.Count(Text.Split([Merged],"|"))),
    MaxCount = List.Max(SplitCount[SCount]),
    Split = Table.SplitColumn(TCC, "Merged", Splitter.SplitTextByDelimiter("|", QuoteStyle.Csv), MaxCount),
    Demote = Table.DemoteHeaders(Split),
    TransposeA = Table.Transpose(Demote),
    Index = Table.AddIndexColumn(TransposeA, "Index", -1, 1),
    TypeText = Table.TransformColumnTypes(Index,{{"Index", type text}}),
    Replace = Table.ReplaceValue(TypeText,"-1","Column1",Replacer.ReplaceText,{"Index"}),
    TRC = Table.RemoveColumns(Replace,{"Column1"}),
    TSC = Table.SelectColumns(TRC,{"Index", "Column2", "Column3"}),
    TransposeB = Table.Transpose(TSC),
    Promote = Table.PromoteHeaders(TransposeB, [PromoteAllScalars=true])
in
    Promote
ms.png

 
Upvote 0
@alansidman: I am probably wrong but for some reason (and I am recently very interested in Power Query), I feel like that you don't need blank cells in columns in between.
I mean; result like this:

Column1Merged.1Merged.2Merged.3Merged.4Merged.5Merged.6
xyz@abc.comCom1view onlycom2view only
yog@abc.comCom1view onlycom2view onlycom3view only

So here is my M-Code attempt with this assumption:

VBA Code:
let
    Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],
    Merge = Table.AddColumn(Source, "Merged", each Text.Combine(List.Transform(List.Skip(Record.FieldValues(_), 1),Text.From),"|")),
    CleanUp = Table.SelectColumns(Merge,{Table.ColumnNames(Merge){0}, "Merged"}),
    Destination = Table.SplitColumn(CleanUp, "Merged", 
        Splitter.SplitTextByDelimiter("|", QuoteStyle.Csv), 
        List.Max(List.Transform(CleanUp[Merged], each List.Count(Text.PositionOf(_,"|",Occurrence.All )))) + 1)
in
    Destination

Note: Alan - I see that you are using the first release of XL2BB. Just FYI - there is a newer version in case you'd like to update.
 
Upvote 0
Big Thanks Sandy. I was pulling my hair trying to figure this one out.
 
Upvote 0
@smozgur

Code is very clean. Like the fact that no nulls in between. Thanks for the heads up on XL2BB

Alan
 
Upvote 0

Forum statistics

Threads
1,223,792
Messages
6,174,612
Members
452,574
Latest member
hang_and_bang

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