Merge Multiple Columns in One code

Shreesurya

Board Regular
Joined
Jul 7, 2014
Messages
50
Hi,

I am Merging 2 columns in Power query and its being repeated for rest of the table.

Col A & B are merged and then C&D and then E &F, finally i will have 3 Columns from 6 Columns.

Problem here is when I merge every time new line of code being created. is there a possibility in Power query to use Nested Merge and have just one line of code instead of having each line for every merge I do.


Power Query:
let
    Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],
    #"Merged Columns" = Table.CombineColumns(Table.TransformColumnTypes(Source, {{"Column1", type text}, {"Column2", type text}}, "en-US"),{"Column1", "Column2"},Combiner.CombineTextByDelimiter("", QuoteStyle.None),"A"),
    #"Merged Columns1" = Table.CombineColumns(Table.TransformColumnTypes(#"Merged Columns", {{"Column3", type text}, {"Column4", type text}}, "en-US"),{"Column3", "Column4"},Combiner.CombineTextByDelimiter("", QuoteStyle.None),"B"),
    #"Merged Columns2" = Table.CombineColumns(Table.TransformColumnTypes(#"Merged Columns1", {{"Column5", type text}, {"Column6", type text}}, "en-US"),{"Column5", "Column6"},Combiner.CombineTextByDelimiter("", QuoteStyle.None),"C"),
    #"Merged Columns3" = Table.CombineColumns(Table.TransformColumnTypes(#"Merged Columns2", {{"Column7", type text}, {"Column8", type text}}, "en-US"),{"Column7", "Column8"},Combiner.CombineTextByDelimiter("", QuoteStyle.None),"D"),
    #"Merged Columns4" = Table.CombineColumns(Table.TransformColumnTypes(#"Merged Columns3", {{"Column9", type text}, {"Column10", type text}}, "en-US"),{"Column9", "Column10"},Combiner.CombineTextByDelimiter("", QuoteStyle.None),"E"),
    #"Merged Columns5" = Table.CombineColumns(Table.TransformColumnTypes(#"Merged Columns4", {{"Column11", type text}, {"Column12", type text}}, "en-US"),{"Column11", "Column12"},Combiner.CombineTextByDelimiter("", QuoteStyle.None),"F")
in
    #"Merged Columns5"
 

Excel Facts

What did Pito Salas invent?
Pito Salas, working for Lotus, popularized what would become to be pivot tables. It was released as Lotus Improv in 1989.
Here is a query that takes a table with an assumed even number of columns of text and joins with a comma cols 0 & 1 , 2 & 3, ... n-2 & n -1 where n is the total number of columns. The resulting table column names are A,B,C... and the resulting table has n/2 columns. If the column data is not text it won't work and would need to be modified.

Power Query:
let
    Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],
    lst = Table.ToColumns(Source),
    eolist = Table.ColumnCount(Source)/2-1,
    lst1 = List.Accumulate({0..eolist}, {}, (s,c)=> s & {List.Zip({lst{c*2}, lst{c*2+1}})}),
    lst2 = List.Transform(lst1, each List.Transform(_, (x)=> Text.Combine(x,","))),
    Headers = List.Transform({0..eolist}, each Character.FromNumber(65+_)),
    Result = Table.FromColumns(lst2, Headers)
in
    Result

Book1
ABCDEFGHIJKLMNOPQ
1Col1Col2Col3Col4Col5Col6Col7Col8Col9Col10ABCDE
2AEIMQUAAEEIIMMA,EI,MQ,UAA,EEII,MM
3BFJNRVBBFFJJNNB,FJ,NR,VBB,FFJJ,NN
4CGKOSWCCGGKKOOC,GK,OS,WCC,GGKK,OO
5DHLPTXDDHHLLPPD,HL,PT,XDD,HHLL,PP
6
Sheet1
 
Upvote 0
Thank you so much for the code Gordon, Is it possible to enhance this code where I can specify which columns to merge (By Providing column Headers), Because my data doesn't come with the same column numbers, however, the Header name would be the same. For Example, data in Column C might be there in Column D.
 
Upvote 0
Maybe like this?

Power Query:
let
    Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],
    ColsToMerge = {{"Col5","Col6","Merge5&6"},{"Col7","Col8","Merge7&8"}},
    ColsZip = List.Transform(ColsToMerge, each List.Zip({Table.Column(Source, _{0}),Table.Column(Source, _{1})})),
    ColsMergedNames = List.Transform(ColsToMerge, each _{2}),
    ColsCombine =List.Transform(ColsZip, each List.Transform(_, (x)=> Text.Combine(x,", "))),
    xCols = List.Accumulate(ColsToMerge, {}, (s,c)=> s & List.FirstN(c,2)),
    xTable = Table.SelectColumns(Source, List.RemoveItems(Table.ColumnNames(Source),xCols)),
    Result = Table.FromColumns(Table.ToColumns(xTable) & ColsCombine, Table.ColumnNames(xTable) & ColsMergedNames)
in
    Result

Book4
ABCDEFGHIJKLMNOPQRST
1Col1Col2Col3Col4Col5Col6Col7Col8Col9Col10Col1Col2Col3Col4Col9Col10Merge5&6Merge7&8
2AEIMQUAAEEIIMMAEIMIIMMQ, UAA, EE
3BFJNRVBBFFJJNNBFJNJJNNR, VBB, FF
4CGKOSWCCGGKKOOCGKOKKOOS, WCC, GG
5DHLPTXDDHHLLPPDHLPLLPPT, XDD, HH
6
Sheet1
 
Upvote 0
Solution
let Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content], ColsToMerge = {{"Col5","Col6","Merge5&6"},{"Col7","Col8","Merge7&8"}}, ColsZip = List.Transform(ColsToMerge, each List.Zip({Table.Column(Source, _{0}),Table.Column(Source, _{1})})), ColsMergedNames = List.Transform(ColsToMerge, each _{2}), ColsCombine =List.Transform(ColsZip, each List.Transform(_, (x)=> Text.Combine(x,", "))), xCols = List.Accumulate(ColsToMerge, {}, (s,c)=> s & List.FirstN(c,2)), xTable = Table.SelectColumns(Source, List.RemoveItems(Table.ColumnNames(Source),xCols)), Result = Table.FromColumns(Table.ToColumns(xTable) & ColsCombine, Table.ColumnNames(xTable) & ColsMergedNames) in Result
Hi Gordon,

This is awesome. this coding worked the way I wanted, Thank you so much for helping...
 
Upvote 0

Forum statistics

Threads
1,215,899
Messages
6,127,637
Members
449,393
Latest member
Messi1408

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