SOLVED

New to Power Query - Duplicate All Columns

Copper Contributor

Hello, I have a fairly simple problem I'm not sure how to approach.

 

I have a table in Power Query and I want to duplicate all the columns (and modify their names) without having to do it manually as new columns are added.

 

For example, I have:

Column1Column2Column3
   
   

 

And I want it to become

 

Column1AColumn1BColumn2AColumn2BColumn3AColumn3B
      
      

 

I assume the answer here is something like, "get a list of all the column names, run duplicate column on all of them, then rename them" but I'm not sure how to approach that.

3 Replies
best response confirmed by Mark_Desmarais (Copper Contributor)
Solution

@Mark_Desmarais 

You may generate script like this

let
    Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],

    names = Table.ColumnNames (Source),
    namesA = List.Transform( names, (q) => q & "A"),

    ourColumns = Table.ToColumns(Source),
    tableWithDuplicatedColumns = Table.FromColumns(
        ourColumns & ourColumns,
        names & namesA)
in
    tableWithDuplicatedColumns

@Sergei Baklan Thank you very much, this is what I was looking for. I appreciate your understanding of the problem.

The only issue I have at the moment is that the columns in this solution are actually organized like this:

 

Column1AColumn2AColumn3AColumn1BColumn2BColumn3B
      

 

Where I'd prefer them like this:

 

Column1AColumn1BColumn2AColumn2BColumn3AColumn3B
      

 

Is there an elegant way to write the step with Table.FromColumns where the columns where the two sets of columns are merged in alternating columns?

 

Thank you.

@Mark_Desmarais 

That could be like

let
    Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],

    names = Table.ColumnNames (Source),
    namesA = List.Transform( names, (q) => q & "A"),

    ourColumns = Table.ToColumns(Source),
    tableWithDuplicatedColumns = Table.FromColumns(
        List.Union( List.Zip( {ourColumns, ourColumns} ) ),
        List.Union( List.Zip( {names, namesA} ) )
    )
in
    tableWithDuplicatedColumns

based on previous sample

1 best response

Accepted Solutions
best response confirmed by Mark_Desmarais (Copper Contributor)
Solution

@Mark_Desmarais 

You may generate script like this

let
    Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],

    names = Table.ColumnNames (Source),
    namesA = List.Transform( names, (q) => q & "A"),

    ourColumns = Table.ToColumns(Source),
    tableWithDuplicatedColumns = Table.FromColumns(
        ourColumns & ourColumns,
        names & namesA)
in
    tableWithDuplicatedColumns

View solution in original post