How to get non-adjacent columns with FILTER function in Excel

Excel 365’s FILTER() function is great for getting a cut of data that meets your criteria. But what if you need to filter and then show non-adjacent columns? Something like below. In this article, let me show you a few options to get discrete columns after filtering with the FILTER function.

Get Non-adjacent columns with Excel FILTER function

Using CHOOSECOLS with FILTER()

Imagine you have a table data named “staff” and you want to see all the staff who joined in year 2021. We can use below FILTER function for that.

This will provide a list of all staff who joined in year 2021, as depicted below.

filter result, but we don't want all the columns - just 1,2,7 & 8

But we don’t want all columns, just ID, Gender, Salary and Leave Balance.

To see just columns 1,2,7 & 8 of this filtered data, we can use below formula.

This will give you exactly what you need without anything else.

Filter with choosecols - sample result

What if I need to get data, but the column order is different from original data…

Showing columns out of order in Excel

Say, you do want the columns 2,6,8&9 but you want them to show up in the order 6,8,2&9 in the final output.

You can still use the CHOOSECOLS function like below.

=CHOOSECOLS(
FILTER(staff, YEAR(staff[date of join])=2021),
6,8,2,9)

How to get columns from a list of header names

If you want to use a range of column names and show filtered data for only those columns, we can use XMATCH along with CHOOSECOLS and FILTER, as demoed below.

using column headers to figure out which columns to show
  1. Set up your column headers in a range like Z5:AC5
  2. Now, we can use XMATCH to find the positions of these headers. =XMATCH(Z5:AC5, staff[#headers])
  3. When you pass the result of XMATCH to CHOOSECOLS, you can pick these columns.
=CHOOSECOLS(
     FILTER(staff,YEAR(staff[Date of Join])=2021),
     XMATCH(Z5:AC5,staff[#Headers]))

How does this work…

  • Let’s go inside out.
  • The FILTER() function gets all the staff data for people whose joining date is in 2021.
  • Range Z5:AC5 holds the names of the columns we want to see.
  • XMATCH(Z5:AC5, staff[#Headers]) will tell you the column numbers for the columns you want by looking them up in the table header row.
  • CHOOSECOLS() will then return those exact columns

Learn more about these functions:

  • How to use FILTER function in Excel, FILTER function video tutorial
  • How to use XLOOKUP function in Excel
  • How to use tables in Excel

The post How to get non-adjacent columns with FILTER function in Excel appeared first on Chandoo.org – Learn Excel, Power BI & Charting Online.

Original source: https://chandoo.org/wp/how-to-get-non-adjacent-columns-with-filter-function-in-excel/

Close Menu