fbpx

How to lookup in any column – Excel Formula Trick

Share

Facebook
Twitter
LinkedIn

Do you want to lookup in any column and return the result? Something like this:

Lookup in any column - Excel trick

In this article, learn how to write necessary Excel formulas to get the result.

Data Setup for looking up in any column

You need to set up your data in below structure. One column with the data you want to get and multiple columns with potential lookup value. For example, team name in column C, member names in columns D:J, as depicted below.

data setup for looking in any column

Lookup formula:

Let’s say we want to lookup “Leonard”, who is in the tea “Geeky Group”. Here is the formula.

=XLOOKUP(1,BYROW(D5:J13,LAMBDA(row,COUNTIFS(row,"Leonard"))),C5:C13)

'notes:
'D5:J13 contains team member names
'C5:C13 contains team names
'this will return #N/A if none of the teams contain lookup value - ie Leonard

How does this formula work?

To understand how this lookup in any column formula is working, we need to first understand a few Excel concepts.

  • BYROW: This function let’s you define logic or operations that apply consistently for each row of the data. As we want to look in each row of team members and see if any of them is “Leonard”, BYROW is perfect for this operation.
  • LAMBDA: We can use Excel’s LAMBDA functions to create custom logic. As we need to check each row of the data to see if any of the members are “Leonard”, I created a LAMBDA to do that operation. LAMBDA(row, COUNTIFS(row, “Leonard”)) will return the count of “Leonard” in the input variable row

So now that you have the basic concepts ready, let’s understand the lookup in any column function. Here is the formula again.

=XLOOKUP(1,BYROW(D5:J13,LAMBDA(row,COUNTIFS(row,"Leonard"))),C5:C13)

'notes:
'D5:J13 contains team member names
'C5:C13 contains team names
'this will return #N/A if none of the teams contain lookup value - ie Leonard
  • BYROW(D5:J13,LAMBDA(row,COUNTIFS(row,”Leonard”))): This formula portion tells us how many times “Leonard” appeared in each row of the data. It will be 0 if the team doesn’t contain lookup value and 1(or more) if the team contains the lookup value. For our sample data, this would be {0;0;0;0;0;1;0;0;0}
  • XLOOKUP(1, BYROW(…), C5:C13): Now that we know which team has the lookup value (Leonard), we just lookup for 1 (count) in the BYROW output and return the corresponding team name from the column C.

What if there are multiple matching values?

By default XLOOKUP returns the first matching value whenever we have multiple matches. If you want to see all team names for a given person name (for example Amy is in two teams – “Geeky Group” and “99 Not Out”.

Using FILTER() function to see all matching values with lookup in multiple columns

In this case, we can use FILTER() function instead of XLOOKUP.

Using FILTER function to return all matching values

Here is the formula to see all team names for a given person.

=FILTER(C5:C13,BYROW(D5:J13="Amy",OR))

'notes:
'D5:J13 contains team member names
'C5:C13 contains team names
'Here we are using an advanced variation of BYROW that applies OR operation on every row of comparison directly. The end result would be a bunch of TRUE or FALSE values, TRUE for teams that contain "Amy" and FALSE for rest of the rows.

This formula uses an advanced variation of the BYROW by doing a comparison directly and applying OR operation on each row of comparison. The result of BYROW would be an array of TRUE or FALSE values. TRUE for rows which contain Amy and FALSE for the rest.

When FILTER(C5:C13 …) sees this array of TRUE/FALSE values, it would return the matching items from C5:C13 for all TRUE values.

In this case, the output is shown below.

FILTER formula output

Further reading:

If you want to understand how the inner parts of this formula are working, refer to below articles / videos.

Limitations of both formulas

Both of the above approaches (XLOOKUP and FILTER) only work with Excel 365 as BYROW is only available in that version of Excel. If you are using an older version of Excel (such as 2024, 2019 or 2016) you can’t use these approaches.

Alternative formula for older version of Excel

There is no alternative for the FILTER() approach as older versions of Excel are not capable of spilling values. But there is an alternative to XLOOKUP() approach of returning the first matching value by looking up any column.

Here is the formula:

=INDEX($C$5:$C$13,MATCH(1, MMULT((D5:J13="Leonard")*1,TRANSPOSE(COLUMN(D5:J13)^0)),0))

'notes:
'D5:J13 contains team member names
'C5:C13 contains team names

Using MMULT to mimic BYROW operation

Most of the above formula is easy to understand, but the bit with MMULT is the confusing part. So let me explain. Here is the MMULT portion: MMULT((D5:J13=”Leonard”)*1,TRANSPOSE(COLUMN(D5:J13)^0))

  • (D5:J13=”Leonard”) checks every cell of the team member data and returns a bunch of TRUE or FALSE values. TRUE when the cell value is “Leonard” and FALSE otherwise. This is how that output looks like:
boolean results of matrix comparison
  • (D5:J13=”Leonard”)*1 turns this boolean array into a bunch of 0s & 1s (0 = false and 1 = true). So the output at this point would be:
boolean values turned to 0s and 1s
  • COLUMN(D5:J13) would return the the column numbers for columns D to J. This would be an array of {4,5,6,7,8,9,10}
  • TRANSPOSE(COLUMN(D5:J13) turns these numbers into a row-wise array. So the net result at this point is {4;5;6;7;8;9;10}
  • TRANSPOSE(COLUMN(D5:J13)^0 takes all these numbers and makes them 1s as any number raised to the power of 0 would be. We just need a row-wise array of 1s same size as the number of columns in the team member array. So this is a long-winded way of getting there as older versions of Excel don’t have SEQUENCE formula. At this stage, our second part of MMULT operation has this array: {1;1;1;1;1;1;1}
  • Now, MMULT just multiplies these two matrices. Here they are again:
mmult illustration
  • When the matrix multiplication is done, we end up with a vertical (row-wise) array of 0s and 1s. 0 when the row doesn’t contain the lookup value (Leonard) and 1 otherwise. The result of this multiplication is:
mmult output

Let’s put everything together

So now that we know how the MMULT magic is working, let’s put all the pieces together.

=INDEX($C$5:$C$13,MATCH(1, MMULT((D5:J13="Leonard")*1,TRANSPOSE(COLUMN(D5:J13)^0)),0))

'notes:
'D5:J13 contains team member names
'C5:C13 contains team names

INDEX($C$5:$C$13,MATCH(1, MMULT(…), 0)) simply looks for a 1 in the MMULT result and returns the corresponding value from range C5:C13.

Further reading on older Excel formulas:

Please refer to below pages for further learning on these formula techniques.

Example Workbook: Lookup in any column

If you need a practice file to understand these formulas better, download it here.

In conclusion

While Excel’s lookup functions (XLOOKUP, VLOOKUP, FILTER, INDEX+MATCH) are great, they all suffer from one nagging limitation. They can only lookup in one place at a time (ie one column or row). But most of the time, our business data is not so tidy. We get data that can span multiple columns. In such cases, using the BYROW() to process one row at a time and then applying lookup or filter logic is a great alternative.

Moreover, if your data is structured vertically (ie team members are listed in rows instead of columns), we can use the same approach with BYCOL function. It applies the logic by column.

The BY functions (BYROW and BYCOL) are great addition to Excel and should be part of every analyst’s toolkit. Using them solves many tricky data problems easily.

Facebook
Twitter
LinkedIn

Share this tip with your colleagues

Excel and Power BI tips - Chandoo.org Newsletter

Get FREE Excel + Power BI Tips

Simple, fun and useful emails, once per week.

Learn & be awesome.

Welcome to Chandoo.org

Thank you so much for visiting. My aim is to make you awesome in Excel & Power BI. I do this by sharing videos, tips, examples and downloads on this website. There are more than 1,000 pages with all things Excel, Power BI, Dashboards & VBA here. Go ahead and spend few minutes to be AWESOME.

Read my storyFREE Excel tips book

Overall I learned a lot and I thought you did a great job of explaining how to do things. This will definitely elevate my reporting in the future.
Rebekah S
Reporting Analyst
Excel formula list - 100+ examples and howto guide for you

From simple to complex, there is a formula for every occasion. Check out the list now.

Calendars, invoices, trackers and much more. All free, fun and fantastic.

Advanced Pivot Table tricks

Power Query, Data model, DAX, Filters, Slicers, Conditional formats and beautiful charts. It's all here.

Still on fence about Power BI? In this getting started guide, learn what is Power BI, how to get it and how to create your first report from scratch.

Leave a Reply