Forum Discussion

Anonymous's avatar
Anonymous
Not applicable
9 years ago
Solved

Power Query keep first and last columns only

Hi,

 

I need to keep the first and last columns of multiple sheets I'm pulling in from Excel.

 

They vary in number of columns and I'm utilising the same Power Query to transform them.

 

If I use the "Remove Other Columns" wizard it hardcodes the column numbers which means when I use it for the next sheet it is trying to keep a non-existant column.

 

Is there anyway of saying in PQ to keep the first an alst column without referencing the column numbers?

 

Thanks.

  • Sure:

     

        KeepColumns = {List.First(Table.ColumnNames(Name_of_your_previous_step)),List.Last(Table.ColumnNames(Name_of_your_previous_step))},
        #"Removed Other Columns" = Table.SelectColumns(Name_of_your_previous_step,KeepColumns)

     

2 Replies

  • MarcelBeug's avatar
    MarcelBeug
    Community Champion

    Sure:

     

        KeepColumns = {List.First(Table.ColumnNames(Name_of_your_previous_step)),List.Last(Table.ColumnNames(Name_of_your_previous_step))},
        #"Removed Other Columns" = Table.SelectColumns(Name_of_your_previous_step,KeepColumns)

     

    • Anonymous's avatar
      Anonymous
      Not applicable

      Thanks for that Marcel