Table.SelectColumns Function in Power Query M

Sumit Bansal
Written by

Sumit Bansal is the founder of TrumpExcel.com and a 13-time Microsoft Excel MVP. He started this site in 2013 to share his passion for Excel through easy tutorials, tips, and training videos, helping you master Excel, boost productivity, and maybe even enjoy spreadsheets!

The Table.SelectColumns function in Power Query keeps only the columns you name and removes everything else from a table. It also puts the columns in the order you list them, so you can pick and rearrange columns in one step.

Every time you right-click a column and choose Remove Other Columns, this is the function Power Query writes for you.

In this article, I’ll show you how to use it with practical examples, from keeping one column to picking columns based on their names or contents.

Follow along with the example file

Table.SelectColumns Function Examples.xlsx

Download

Table.SelectColumns Function Syntax

= Table.SelectColumns(table as table, columns as any, optional missingField as nullable number) as table
  • table – the table you want to keep columns from. This is usually the previous step, such as Source.
  • columns – the column names you want to keep, as a list in curly brackets, such as {"Name","Status"}. For a single column, you can also give just the name as text. Column names are case-sensitive.
  • missingField (optional) – what to do when a column you listed doesn’t exist in the table. Use MissingField.Error (the default, which gives an error), MissingField.UseNull (adds the column filled with null), or MissingField.Ignore (skips it).

What it returns – a table with only the listed columns, in the order you listed them. All the rows stay as they are.

The Table.SelectColumns function works in Power Query in Excel for Microsoft 365, 2024, 2021, 2019, 2016, and Excel for Mac (Microsoft 365), as well as in Power BI.

When to Use the Table.SelectColumns Function

Use this function when you need to:

  • Keep just the few columns you need from a wide table or export
  • Keep some columns and change their order at the same time
  • Stop a query from breaking when a column is missing from the source file
  • Let a list in your workbook decide which columns a query keeps
  • Keep all the columns whose names follow a pattern, like every column ending in “Sales”
  • Remove columns that are completely empty

Example 1: Keep a Single Column

Let’s start with the simplest use, keeping just one column from a table.

Below is a list of event registrations with the attendee’s name, company, city, and ticket type, and I want to keep only the Attendee column.

Event registrations table with Attendee, Company, City and Ticket Type columns

To follow along, select any cell in the table and go to Data > From Table/Range to open it in the Power Query Editor. If Power Query adds a Changed Type step, use #"Changed Type" in place of Source in the formulas below.

Add a new step (by clicking on the fx icon next to the formula bar) and use this formula:

= Table.SelectColumns(Source,"Attendee")

Result: a table with only the Attendee column

Only the Attendee column is left, with all six names

Here, the formula takes the Source table and keeps only the column named “Attendee”. The Company, City, and Ticket Type columns are removed, while all six rows stay.

Since there’s only one column, I gave the name as plain text. Writing it as a list, {"Attendee"}, gives the same result.

Table.SelectColumns only works on columns. To keep some rows and remove others, use the Table.SelectRows function instead.

Example 2: Keep Multiple Columns

Most of the time, you’ll want to keep more than one column.

Below is a support ticket log with six columns, and I want to keep only the Ticket ID, Opened By, and Status columns.

Support ticket log with six columns: Ticket ID, Opened By, Category, Priority, Channel and Status

Add a new step (by clicking on the fx icon next to the formula bar) and use this formula:

= Table.SelectColumns(Source,{"Ticket ID","Opened By","Status"})

Result: a table with the Ticket ID, Opened By, and Status columns

Only the Ticket ID, Opened By and Status columns are left

Here, the column names go inside curly brackets, separated by commas. That’s a list in Power Query, and Table.SelectColumns keeps every column in it.

You can get the same result without typing.

Hold the Control key (Command on a Mac), click the Ticket ID, Opened By, and Status headers, then right-click one of them and choose Remove Other Columns. Power Query adds a step called Removed Other Columns, and its formula uses Table.SelectColumns.

Pro Tip: If your source might gain new columns later, keeping the columns you need is safer than removing the ones you don’t. New columns stay out of your query instead of showing up in the result.

Example 3: Keep Columns and Change Their Order

Here’s something Table.SelectColumns does that’s easy to miss. It returns the columns in the order you list them, not in the order they sit in the table.

Below is an inventory list where Product Name is the fourth column, and I want a table with Product Name first, followed by SKU and Qty on Hand.

Inventory table with SKU, Warehouse, Qty on Hand, Product Name and Unit Cost columns

Add a new step (by clicking on the fx icon next to the formula bar) and use this formula:

= Table.SelectColumns(Source,{"Product Name","SKU","Qty on Hand"})

Result: a table with Product Name, SKU, and Qty on Hand, in that order

Product Name, SKU and Qty on Hand columns, in that order

Here, Product Name comes first in the list, so it becomes the first column, even though SKU came before it in the original table. The Warehouse and Unit Cost columns aren’t in the list, so they’re removed.

So the same step picks the columns and puts them in order, and you don’t need a separate step with the Table.ReorderColumns function afterward.

Each column name can appear in the list only once. If you list the same name twice, like {"SKU","SKU"}, you get an error that says “The field ‘SKU’ already exists in the record.”

Example 4: Add a Missing Column as Null (MissingField.UseNull)

By default, Table.SelectColumns gives an error when a column you listed isn’t in the table. The third argument lets you change that.

Below is a monthly orders file with Order ID, Region, and Amount. Some months also have a Discount column, but this month doesn’t, and I want the result to always have the same four columns.

November orders table with Order ID, Region and Amount, and no Discount column

If you use Table.SelectColumns(Source,{"Order ID","Region","Amount","Discount"}) here, the step fails with this message: “Expression.Error: The column ‘Discount’ of the table wasn’t found.”

You get the same error when the capitalization doesn’t match, because column names are case-sensitive. Asking for “region” when the column is “Region” fails too.

Add a new step (by clicking on the fx icon next to the formula bar) and use this formula:

= Table.SelectColumns(Source,{"Order ID","Region","Amount","Discount"},MissingField.UseNull)

Result: a table with Order ID, Region, Amount, and a Discount column where every value is null

Order ID, Region, Amount and a new Discount column filled with null

Here, MissingField.UseNull tells the function to add any missing column and fill it with null instead of giving an error. Order ID, Region, and Amount exist, so they come through with their values.

This is really useful when the same query runs on a new file every month. Every month comes out with the same four columns, so a later step that uses the Discount column (like changing its type) doesn’t break when a file is missing it.

Follow along with the example file

Table.SelectColumns Function Examples.xlsx

Download

Example 5: Skip Columns That Don’t Exist (MissingField.Ignore)

Sometimes you don’t want a missing column added at all. You just want the query to keep whichever of your columns are there.

Below is a course feedback export with the student, course, rating, comments, and how the feedback was submitted. Some exports also have an Instructor column, and I want to keep Student, Course, Instructor, and Rating whenever they exist.

Course feedback table with Student, Course, Rating, Comments and Submitted Via, and no Instructor column

Add a new step (by clicking on the fx icon next to the formula bar) and use this formula:

= Table.SelectColumns(Source,{"Student","Course","Instructor","Rating"},MissingField.Ignore)

Result: a table with the Student, Course, and Rating columns

Student, Course and Rating columns; the missing Instructor column is skipped

Here, this export has no Instructor column, so MissingField.Ignore simply skips it. The other three columns come through as usual, and no error shows up.

Use MissingField.UseNull when the later steps need every column to be there, like the monthly files in Example 4. Use MissingField.Ignore when you only want what’s actually in the file.

Just remember that both options also hide typos. If you type “Ratng” instead of “Rating”, UseNull gives you an empty Ratng column and Ignore quietly drops the column, so check that the result has the columns you listed.

Example 6: Use a List of Column Names from Your Worksheet

This next one is quite practical when you share a report. Below is an employee list that includes salary and bank account details, and I want a version with only the columns listed in a separate table.

Employees table with Emp ID, Name, Department, Salary, Bank Account and Phone Ext columns

The columns to keep are listed in a one-column table called KeepColumns, on its own sheet (Example 6 Columns). Right now, it lists Name, Department, and Phone Ext.

Go to Data > Get Data > From Other Sources > Blank Query. In the Power Query Editor, open the Advanced Editor (Home > Advanced Editor) and use this code.

It reads both tables by name, so you don’t have to load them into Power Query first.

let
Source = Excel.CurrentWorkbook(){[Name="Employees"]}[Content],
KeepList = Excel.CurrentWorkbook(){[Name="KeepColumns"]}[Content][Columns to Keep],
#"Removed Other Columns" = Table.SelectColumns(Source,KeepList)
in
#"Removed Other Columns"

Result: a table with the Name, Department, and Phone Ext columns

Only Name, Department and Phone Ext, the columns listed in the KeepColumns table

Here, the KeepList step reads the Columns to Keep column of the KeepColumns table. Taking one column from a table this way gives you a list, {"Name","Department","Phone Ext"}, which is exactly what Table.SelectColumns needs.

Add a column name to the KeepColumns table (or remove one) and refresh the query, and the result changes with it. You never have to open the M code.

Important: Each name in the KeepColumns table has to match a column name exactly, including capital letters. A typo or an extra space gives the “wasn’t found” error, so fix the name in the table and refresh.

Example 7: Keep Columns Whose Names Match a Pattern

When a table has many similar columns, typing each name gets tedious. You can build the list of names with a formula instead.

Below is a store sales report with a Target and a Sales column for each quarter, and I want to keep the Store column and all the Sales columns.

Store sales table with a Target and a Sales column for Q1 and Q2

Create a new blank query, open the Advanced Editor, and use this code:

let
Source = Excel.CurrentWorkbook(){[Name="StoreSales"]}[Content],
SalesColumns = List.Select(Table.ColumnNames(Source),each Text.EndsWith(_,"Sales")),
#"Removed Other Columns" = Table.SelectColumns(Source,{"Store"}&SalesColumns)
in
#"Removed Other Columns"

Result: a table with the Store, Q1 Sales, and Q2 Sales columns

Store plus the Q1 Sales and Q2 Sales columns

Here, the Table.ColumnNames function gives a list of all six column names. The List.Select function then keeps only the names that end with “Sales” (the underscore _ stands for each name as it’s checked).

Then {"Store"}&SalesColumns joins the two lists, so Store comes first, followed by the two Sales columns. When a Q3 Sales column shows up in the report, the query picks it up on the next refresh.

The Text.EndsWith function is case-sensitive, though. A column named “Q3 sales” (lowercase s) won’t match “Sales”, so keep the column names consistent.

Example 8: Remove Columns That Are Completely Empty

Exports from other systems often come with columns that have a header but no data. Below is a sales rep export where the Fax and Notes columns are empty, and I want to keep only the columns that have at least one value.

Sales rep export where the Fax and Notes columns are completely empty

Create a new blank query, open the Advanced Editor, and use this code:

let
Source = Excel.CurrentWorkbook(){[Name="RepExport"]}[Content],
NonEmptyColumns = List.Select(Table.ColumnNames(Source),each List.NonNullCount(Table.Column(Source,_))>0),
#"Removed Other Columns" = Table.SelectColumns(Source,NonEmptyColumns)
in
#"Removed Other Columns"

Result: a table with the Rep, Region, Deals Closed, and Revenue columns

Rep, Region, Deals Closed and Revenue; the empty Fax and Notes columns are gone

Here, the Table.Column function gets all the values in one column, and the List.NonNullCount function counts how many of them aren’t null. The List.Select function keeps the names of the columns where that count is more than 0.

Fax and Notes have no values at all, so their count is 0 and they’re left out. If someone fills in a note later, the Notes column comes back on the next refresh.

This checks for null, which is what blank cells from an Excel table come in as. A column filled with empty text (“”) or spaces still counts as having values, so it stays.

Other Excel Articles You May Also Like:

All Power Query Functions

Sumit Bansal

Sumit Bansal

13x Microsoft Excel MVP

Hey! I'm Sumit Bansal, founder of trumpexcel.com and a Microsoft Excel MVP. I started this site in 2013 because I genuinely love Microsoft Excel (yes, really!) and wanted to share that passion through easy Excel tutorials, tips, and Excel training videos. My goal is straightforward: help you master Excel skills so you can work smarter, boost productivity, and maybe even enjoy spreadsheets along the way!

Get the FREE 51 Excel Tips Ebook

Enter your details and the free PDF is on its way to your inbox.

Hmm, that didn't go through. Please check your email and try again.

No spam. You'll also get my weekly Excel newsletter. Unsubscribe anytime.

Check your inbox!

The ebook is on its way to your email. It usually lands within a couple of minutes.