Skip to main content
cancel
Showing results for 
Search instead for 
Did you mean: 

Register now to learn Fabric in free live sessions led by the best Microsoft experts. From Apr 16 to May 9, in English and Spanish.

Reply
Anonymous
Not applicable

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
MarcelBeug
Community Champion
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)

 

Specializing in Power Query Formula Language (M)

View solution in original post

2 REPLIES 2
MarcelBeug
Community Champion
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)

 

Specializing in Power Query Formula Language (M)
Anonymous
Not applicable

Thanks for that Marcel

Helpful resources

Announcements
Microsoft Fabric Learn Together

Microsoft Fabric Learn Together

Covering the world! 9:00-10:30 AM Sydney, 4:00-5:30 PM CET (Paris/Berlin), 7:00-8:30 PM Mexico City

PBI_APRIL_CAROUSEL1

Power BI Monthly Update - April 2024

Check out the April 2024 Power BI update to learn about new features.

April Fabric Community Update

Fabric Community Update - April 2024

Find out what's new and trending in the Fabric Community.