cancel
Showing results for 
Search instead for 
Did you mean: 
Reply
Highlighted
Kudo Commander
Kudo Commander

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.

1 ACCEPTED SOLUTION

Accepted Solutions
Highlighted
Community Champion
Community Champion

Re: Power Query keep first and last columns only

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)

 

Specializing in Power Query Formula Language (M)

View solution in original post

2 REPLIES 2
Highlighted
Community Champion
Community Champion

Re: Power Query keep first and last columns only

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)

 

Specializing in Power Query Formula Language (M)

View solution in original post

Highlighted
Kudo Commander
Kudo Commander

Re: Power Query keep first and last columns only

Thanks for that Marcel

Helpful resources

Announcements
Ignite

Microsoft Ignite

This will be a conference that you do not want to miss!

Get Ready for Power BI Dev Camp

Get Ready for Power BI Dev Camp

Don't miss the Power BI Dev Camp this week!

August Community Highlights

Check out a full recap of the month!

August 2020 CYST Challenge

Check out the winners of the recent 'Can You Solve These?' community challenge!

Experience what’s next for Power BI

Join us for an in-depth look at the new Power BI features and capabilities at the free Microsoft Business Applications Launch Event.

Top Solution Authors