Forum Discussion

Mark_Desmarais's avatar
Mark_Desmarais
Copper Contributor
Mar 17, 2023
Solved

New to Power Query - Duplicate All Columns

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.

  • 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

3 Replies

  • SergeiBaklan's avatar
    SergeiBaklan
    Diamond Contributor

    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
    • Mark_Desmarais's avatar
      Mark_Desmarais
      Copper Contributor

      SergeiBaklan 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.

      • SergeiBaklan's avatar
        SergeiBaklan
        Diamond Contributor

        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