Power Query to Dynamically Select First and Last Column

legalhustler

Well-known Member
Joined
Jun 5, 2014
Messages
1,195
Office Version
  1. 365
Platform
  1. Windows
In Power Query, I have a step where the column "Custom" stores "table" values and if I click the expand button it allows me to manually select the columns I want to show, but how can I dynamically select just the first and last columns only of the "table" values?
 

Excel Facts

Did you know Excel offers Filter by Selection?
Add the AutoFilter icon to the Quick Access Toolbar. Select a cell containing Apple, click AutoFilter, and you will get all rows with Apple
I used the following (it accounts if there is a single column within the table values and return just that otherwise it returns the first and last columns)

Power Query:
= Table.AddColumn(PreviousStep, "NewColumnName", each Table.SelectColumns([Custom],if Table.ColumnCount([Custom])=1 then {Table.ColumnNames([Custom]){0}} else {Table.ColumnNames([Custom]){0},Table.ColumnNames([Custom]){Table.ColumnCount([Custom])-1}}))
 
Upvote 0
I used the following (it accounts if there is a single column within the table values and return just that otherwise it returns the first and last columns)

Power Query:
= Table.AddColumn(PreviousStep, "NewColumnName", each Table.SelectColumns([Custom],if Table.ColumnCount([Custom])=1 then {Table.ColumnNames([Custom]){0}} else {Table.ColumnNames([Custom]){0},Table.ColumnNames([Custom]){Table.ColumnCount([Custom])-1}}))

Ok - I need help making a small adjustment. How do I modify this formula so that when the last column is returned it's renamed to Column2? Obviously the first column will always be Column1 so that doesn't need to change, only the last column if it's not already named Column2

Power Query:
Table.SelectColumns([Custom],if Table.ColumnCount([Custom])=1 then {Table.ColumnNames([Custom]){0}} else {Table.ColumnNames([Custom]){0},Table.ColumnNames([Custom]){Table.ColumnCount([Custom])-1}})
 
Upvote 0
Once again:

Cross-posting (posting the same question in more than one forum) is not against our rules, but the method of doing so is covered by #13 of the Forum Rules.

Be sure to follow & read the link at the end of the rule too!

Cross posted at:

There is no need to repeat the link(s) provided above but if you have posted the question at other places, please provide links to those as well.

If you do cross-post in the future and also provide links, then there shouldn’t be a problem. However, this is the second time in quick succession you have failed to do so. Further breaches will probably result in removal of your posting ability.
 
Upvote 0

Forum statistics

Threads
1,216,499
Messages
6,131,012
Members
449,613
Latest member
MedDash99

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top