Dynamically removing columns
This page shows how to remove unwanted columns without naming the columns to keep. The query works out which columns have headers each time it runs, which helps when columns move or when new ones are added to the source over time.
The example
Section titled “The example”The data sits in a named range called Data, which covers more columns and rows than the data fills:

The named range
Loaded into Power Query with Excel.CurrentWorkbook(), the range arrives as a table inside the Content column, with nine columns named Column1 to Column9:

Only the first three columns are wanted. Table.SelectColumns could pick them, but it would need their names written into the query, so if the columns moved, or if a new one were added, the query would not follow.
Building the list of columns
Section titled “Building the list of columns”The alternative is to build the list of columns from the data itself:
- Use
Table.Combineon the Content column, which joins all the tables it holds into one. Here there is only one table, so this simply expands it:
![The formula Table.Combine(Source[Content]) and the resulting table, with Name, Country and Age in the first row of Column1 to Column3](/_astro/dynamically-removing-columns-3.mlhTNQ6Z_1tMBRM.webp)
- Wrap the formula in
Table.PromoteHeaders, so the first row becomes the headers. The empty columns keep their default names, such as Column4 and Column5:
![The formula Table.PromoteHeaders(Table.Combine(Source[Content])), with the columns now headed Name, Country, Age, Column4 and Column5](/_astro/dynamically-removing-columns-4.BYfRVsU0_1QDdu5.webp)
- Wrap that in
Table.ColumnNames, which returns the headers as a list. Name this step ColNames:

- Filter the list with
List.Selectto drop any name that contains Column, any name that is a space or empty, and any null. Name this step FilteredCols. The test can be anything that suits the data:

- Only now, once the list has been filtered, use
Table.SelectColumnsto keep the columns named in FilteredCols. Name this step FinalTable:
![The formula Table.SelectColumns(Table.PromoteHeaders(Table.Combine(Source[Content])), FilteredCols), returning just the Name, Country and Age columns](/_astro/dynamically-removing-columns-7.CEU6DPqq_Z14sgzd.webp)
Testing with a new column
Section titled “Testing with a new column”To test the query, add a new field to the range in column E, leaving column D empty:

The named range with a new field
After a refresh, and with no change to the query, New Field appears as a fourth column, while the empty columns D and F to I stay out:
