Find the Column Header of the Max Value in Google Sheets

When you have multiple columns of data with numbers below, you might want to find the column header of the max value in each row in Google Sheets.

For example, suppose you have items listed in column A and their stock quantities across three warehouses in the next three columns. In this case, you may want to retrieve the warehouse name for each item with the highest quantity.

Example of finding the column header for the maximum values in each row in Google Sheets

This involves finding the maximum value and then locating the corresponding header. This process should repeat for each row to cover all items. Sometimes, there may be a tie, in which case multiple headers should be returned.

We can use either array formulas or non-array formulas to find the column header of the max value in Google Sheets. Let’s explore the examples.

Example: Finding the Column Header of the Max Value in Google Sheets

In the following dataset, column A contains item names, while columns B, C, and D hold the quantities for Warehouse 1, Warehouse 2, and Warehouse 3.

To find the column header of the max value for the first item, use the following formula in cell F2:

=TEXTJOIN(", ", TRUE, IFNA(FILTER($B$1:$D$1, XMATCH(IF(B2:D2="", NA(), B2:D2), MAX(B2:D2)))))

Drag this formula down to apply it to all rows.

  • If there are multiple matching values, the corresponding headers will be returned as a comma-separated list.

Formula Explanation

The formula filters the range $B$1:$D$1 based on the condition below:

XMATCH(IF(B2:D2="", NA(), B2:D2), MAX(B2:D2))

Where:

  • XMATCH(IF(B2:D2="", NA(), B2:D2), MAX(B2:D2)) – Matches each value in the row against the max value and returns its position or #N/A if not found. For example, if the maximum value is in the first column, it returns {1, #N/A, #N/A}.
    • IF(B2:D2="", NA(), B2:D2) – Converts empty cells into #N/A errors and keeps values in other cells. This ensures that blank cells are not considered when finding the max value.
    • MAX(B2:D2) – Returns the highest value in the row.
  • FILTER($B$1:$D$1, condition) – Filters the header row (B1:D1) based on the condition (XMATCH result). If the condition is a number (not an error), the corresponding header is returned.
  • TEXTJOIN(", ", TRUE, IFNA(...))Joins multiple headers (if any) into a single cell.

Since the header row reference ($B$1:$D$1) is absolute, it remains unchanged when dragging the formula down, while the row conditions update dynamically.

This is how the formula identifies the column header of the maximum value in each row.

Array Formula to Find the Column Header of the Max Value in Each Row

Drag-down formulas have limitations. For instance, you’ll need to copy the formula to new or inserted rows unless you’re using structured tables. To avoid this, you can convert the formula into an array formula using the BYROW and LAMBDA functions:

=BYROW(B2:D9, LAMBDA(r_, TEXTJOIN(", ", TRUE, IFNA(FILTER(B1:D1, XMATCH(IF(r_="", NA(), r_), MAX(r_)))))))

This formula converts the previous logic into a custom unnamed LAMBDA function, where:

  • r_ represents the current row.
  • BYROW applies the formula to each row in the array, returning the column header of the max value for each row.

Additional Tip: Creating a Table with Items, Max Values, and Headers

If you want to see the max value and corresponding headers for all items in one table, use this approach:

=LET(
   data, ARRAYFORMULA(SPLIT(FLATTEN(A2:A&"|"&B1:D1&"|"&B2:D), "|")), 
   ftr, FILTER(data, CHOOSECOLS(data, 3)>0), 
   SORTN(SORT(ftr, 1, 1, 3, 0), 9^9, 2, 1, 1)
)
Returning the item, maximum value, and corresponding headers in a table format

Where:

  • A2:A contains the items.
  • B1:D1 holds the headers.
  • B2:D holds the values.

Pros and Cons:

Pros:

  • Doesn’t rely on LAMBDA, making it resource-friendly.
  • Returns headers of max values for all items at once.

Cons:

  • If there are multiple max values for an item, it only returns one header, not all.

Explanation of the Formula

  • ARRAYFORMULA(SPLIT(FLATTEN(A2:A&"|"&B1:D1&"|"&B2:D), "|")) – Converts the data into a flattened, unpivoted format (named data).
  • FILTER(data, CHOOSECOLS(data, 3)>0) – Filters out blank rows from the data to improve performance (named ftr).
  • SORTN(SORT(ftr, 1, 1, 3, 0), 9^9, 2, 1, 1) – Sorts the filtered (ftr) items alphabetically, then by max quantity in descending order, ensuring the max value is at the top. SORTN removes duplicates, leaving unique items with their max value and header.

Sample Sheet

Sample Sheet

Resources

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.

Cycle Highlights in Google Sheets – Rotate Highlights Daily

Want to cycle highlights in Google Sheets every day? Whether you're rotating a meal...

Filter Rows Containing Multiple Selected Values in Google Sheets

This tutorial explains how to filter rows in a column containing multiple selected drop-down...

Two-Way Lookup with XLOOKUP in Google Sheets

When you need to look up one search key vertically and another horizontally, you...

How to Filter by Total in Google Sheets Pivot Tables

Google Sheets offers many tools to summarize and analyze data, but Pivot Tables are...

More like this

Cycle Highlights in Google Sheets – Rotate Highlights Daily

Want to cycle highlights in Google Sheets every day? Whether you're rotating a meal...

Filter Rows Containing Multiple Selected Values in Google Sheets

This tutorial explains how to filter rows in a column containing multiple selected drop-down...

Two-Way Lookup with XLOOKUP in Google Sheets

When you need to look up one search key vertically and another horizontally, you...

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.