HomeGoogle DocsSpreadsheetLookup and Retrieve the Column Header in Google Sheets

Lookup and Retrieve the Column Header in Google Sheets

Published on

What do you mean by lookup and retrieve the column header in Google Sheets? I must answer this first before the formula.

I have a list/table in Google Sheets with multiple columns, in which I want to search down the very first column for a key.

If found, I want the column headers based on one condition.

This screenshot better depicts what I meant by lookup and retrieving the column header in Google Sheets.

search first column and return header row

I want to search down the name “Julia” in column A and, if found, return the header row which contains the dates.

But it should be a conditional return. I want to get the date(s) if the person was absent (A).

Which is the better function in this case?

You may have a preconceived idea of using Vlookup, Hlookup, or Index-Match.

But for me, the Query seems easier.

Formula:

=query(transpose(query(A2:F6,"Select * where A='Julia'",1)),"Select Col1 where Col2='A'")

This formula would return the dates 3/1/18 and 5/1/18 because the person in question (Julia) was absent on those days.

How Does This Query Formula Work?

The inner Query returns the header row and the row that contains the name Julia in Col1.

So we have two rows – the first row with headers (dates) and the second row with the status “P” and “A” where “P” stands for Present and “A” stands for Absent.

The Transpose function that wraps the inner Query makes these two-row outputs into two columns.

The first column contains the headers, and the second has the values “P” and “A.”

The outer Query returns Col1 (column 1) if Col2 (column 2) matches “A.”

You can use this formula to look up and retrieve the column header in Google Sheets.

The plus point of this formula is that it conditionally returns the column header.

How Can I Adjust the Formula If My Data Range Is Different?

Once again, see the above formula.

This time in the below formula, I have made the range infinite.

=iferror(query(transpose(query(A2:F,"Select * where A='Julia'",1)),"Select Col1 where Col2='A'"))

Earlier, it was A2:F6. Additionally, I have enclosed the formula within the IFERROR function.

These modifications can help you in two ways.

  1. First, the formula can support an unlimited number of rows.
  2. Secondly, if the search key is not present in the first column, the result will be blank instead of an error.

If your number of columns is different, change the reference A2:F to A2:Z or something like that.

There are no other changes required in the formula.

Can You Provide Us With One More Example?

Why not? Here you go!

Example 2: Lookup and Conditionally Retrieve the Column Header

The below sample data shows the class-wise dress color codes on weekdays in a school.

Let’s code a formula to find the classes with the dress color code Blue on Wednesday.

lookup and extract column headers conditionally - example 2

This time the search key is “Wednesday” in column A.

Once found, search that row for matching conditions, i.e., uniform color Blue and return the corresponding column header, i.e., the class name.

Formula:

=iferror(query(transpose(query(A1:K,"Select * where A='Wednesday'",1)),"Select Col1 where Col2='Blue'"))

The formula is similar to our first example formula. The data range, condition, and search key are different.

I hope you find the above example handy in your real life.

Thanks for the stay!

Prashanth KV
Prashanth KV
Your Trusted Google Sheets and Excel Guide Prashanth KV brings a wealth of experience in Google Sheets and Excel, cultivated through years of work with multinational corporations in Mumbai and Dubai. As a recognized Google Product Expert in Docs Editors, Prashanth shares his expertise through insightful blogging since 2012. Explore his blog for practical tips and guidance on maximizing your spreadsheet skills.

SUMIFS with XLOOKUP in Excel and Google Sheets

SUMIFS and SUMIF are infamous for producing an "argument must be a range" error...

Counting XLOOKUP Results with COUNTIFS in Excel and Google Sheets

We can use COUNTIF or COUNTIFS alongside an XLOOKUP formula to conditionally count the...

Appointment Schedule Template in Google Sheets

An appointment schedule template in Google Sheets can assist you in efficiently managing your...

Creating Sequential Dates in Equally Merged Cells in Google Sheets

Do you know how to create sequential dates in equally merged cells across a...

More like this

Appointment Schedule Template in Google Sheets

An appointment schedule template in Google Sheets can assist you in efficiently managing your...

Creating Sequential Dates in Equally Merged Cells in Google Sheets

Do you know how to create sequential dates in equally merged cells across a...

Interactive Random Task Assigner in Google Sheets

You have multiple tasks and multiple people. Here's a fun way to randomly assign...

13 COMMENTS

  1. Hi,

    Thank you for the great tutorial. I am trying to return not only the header of each column, but an additional value from another column. I have an example of what I need here.

    One thing that is tripping me up is that I have the search key appearing multiple times. I have a sample of the data with a better description than what I have been able to describe here: [URL removed by Admin]

    I would greatly appreciate it if you would take a look at this.

    Thank you so much,
    D.E.

    • Hi Darrell Ellis,

      The problem you mentioned is not inline with the topic of this tutorial. However, I have coded two formulas that you can try.

      Since you have not given me edit access to your sheet, you will need to add the formulas yourself. The formulas are as follows:

      J2:

      =LET(month,A2:A,bus,B2:B,test_range,C2:G,drop_down,I1,
      FILTER(FILTER(bus,month=drop_down),
      BYROW(FILTER(test_range,month=drop_down),LAMBDA(r,COUNTIF(r,"X")))))

      K2:

      =MAP(TOCOL(J2:J,1),
      LAMBDA(r,LET(ftr,FILTER(C2:G,A2:A=I1,B2:B=r),
      FILTER(C1:G1,ftr="X"))))

  2. Hi,

    Is there any way to combine this with a Dropdown list so that in line with your first example, a cell contains a dropdown list containing the names in Column A, and depending on the name selected from the list, the Query formula would output the Column Headers (Attendance Dates) with “A”

    This is basically to replace having to edit the formula every time to change the column A reference (name) as in my purpose, column A is frequently updated and changed.

  3. Hi Prashanth, is there a way to limit the query without a search key, and lock it to the current row? So if there is a cell(s) with TRUE in the row the formula is in, the query returns the header of the TRUE column?

    =transpose(query(transpose(query($B$1:$E$5,"Select * where A="""&$B2&"""",1)),"Select Col1 where Col2=TRUE"))

    but instead of B2 as a search query, it would look at B2:E2 for TRUEs, and return the header from B1:E1? Then on the next row down, it would look at B3:E3 for TRUEs and return the header from row B1:E1.

    Thanks

    • Hi, James,

      To filter header row, based on TRUEs in current row use FILTER instead of QUERY.

      Eg.

      =filter($B$1:$E$1,B2:E2=TRUE)

      This will return values from B1:E1 if B2:E2 contains TRUE. It will expand the result horizontally based on number of TRUEs. So make sure that there are sufficient blank cells for this formula to expand.

      You can also consider combining the output. In that case, you can try this one.

      =textjoin(", ",true,filter($B$1:$E$1,B2:E2=TRUE))

      The above (either of the one) formula is for row # 2 (B2:E2). You can drag-down this formula for other rows.

LEAVE A REPLY

Please enter your comment!
Please enter your name here

This site uses Akismet to reduce spam. Learn how your comment data is processed.