Fix Interchanged First and Last Names in Running Count in Google Sheets

Published on

This post explains how to fix interchanged first and last names in a running count array formula in Google Sheets.

A regular running count array formula will treat interchanged names as distinct entries, resulting in an incorrect count.

For example, if the name “Denise Young” appears twice and “Young Denise” appears thrice, the correct occurrence numbers should be {1, 2, 3, 4, 5}. However, the incorrect result would be {1, 2, 1, 2, 3}. Please see Figure 1 below for reference.

Fixing interchanged first and last names in a running count in Google Sheets
Figure 1

In this example, I have used an array formula in cell D2 to fix interchanged first and last names, ensuring the correct running count is returned.

Important Note:

One limitation of this formula is that it treats swapped names as identical. For instance, “Essie Phelps” and “Phelps Essie” will be counted as the same name, even if they are distinct entries. Keep this in mind when applying the solution.

How to Fix Interchanged First and Last Names in Running Count in Google Sheets

We will use a LAMBDA formula to return the correct running count while accounting for interchanged first and last names.

Here is the formula:

=ARRAYFORMULA(
   MAP(
        B2:B, 
        C2:C,
        LAMBDA(first, last, 
           IF(first & last = "", , SUM(COUNTIFS(B2:first & C2:last, {first & last, last & first})))
        )
   )
)

Formula Breakdown:

  • B2:B: Range containing first names.
  • C2:C: Range containing last names.
  • B2 and C2: The starting cells of the respective ranges.

This is an array formula, so you only need to enter it in cell D2, and it will expand automatically. If there are values in the column that block the formula’s expansion, you may encounter a #REF! error.

How the Formula Works:

Let’s break down the formula step by step.

Step 1: Non-Array Formula to Fix Interchanged Names

To begin, we can use a non-array formula to handle interchanged names in the running count. The key is using the COUNTIFS function with an OR-like condition to check for both name orders.

Here’s the formula:

=ArrayFormula(
   SUM(
      COUNTIFS($B$2:B2 & $C$2:C2, {B2 & C2, C2 & B2})
   )
)
  • Place this formula in cell D2 and drag it down to calculate the running count.
Non-array formula to fix swapped first and last names in a running count in Google Sheets
Figure 2

How It Works:

The COUNTIFS function counts occurrences of names in both orders:

=ArrayFormula(
   COUNTIFS($B$2:B2 & $C$2:C2, {B2 & C2, C2 & B2})
)
  • Criteria Range: $B$2:B2 & $C$2:C2 combines first and last names.
  • Criteria: {B2 & C2, C2 & B2} checks for both possible name orders.

The ARRAYFORMULA is required because, when dragging the formula down, the criteria range becomes an array. Combining two arrays using the ampersand (&) requires the use of the ARRAYFORMULA to handle the operation correctly.

The SUM function totals the results from the COUNTIFS function, ensuring a running count.

Step 2: Converting to an Array Formula

To avoid dragging the formula manually, we convert it into an array formula using the MAP function.

The syntax of the MAP function is:

MAP(array1, [array2, ...], LAMBDA)
  • array1: B2:B (first names).
  • array2: C2:C (last names).

We define placeholders (first and last) for the first and last names within the LAMBDA function:

=ARRAYFORMULA(
   MAP(
        B2:B, 
        C2:C,
        LAMBDA(first, last, 
           SUM(COUNTIFS(B2:first & C2:last, {first & last, last & first}))
        )
   )
)

Step 3: Handling Blank Cells

The above formula may inadvertently count blank cells. To prevent this, we wrap the formula with an IF condition to skip blank rows:

=ARRAYFORMULA(
   MAP(
        B2:B, 
        C2:C,
        LAMBDA(first, last, 
           IF(first & last = "", , SUM(COUNTIFS(B2:first & C2:last, {first & last, last & first})))
        )
   )
)

This ensures that the formula only processes rows with valid data.

Conclusion

This formula fixes interchanged first and last names in a running count. By using the MAP function and LAMBDA, we eliminate the need for manual adjustments, providing a dynamic and efficient solution.

Feel free to explore and adapt the formula to your specific needs!

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.

Lookup Values Under Categories in Excel and Google Sheets

We can use a combination of XLOOKUP and VLOOKUP in both Excel and Google...

Extract Items Under a Category from the Same Column in Excel

In Excel, you can use a combination of the XLOOKUP and DROP functions to...

How to Create a Searchable Table in Excel Using the FILTER Function

Finding specific records, or rows containing the required information, is straightforward in Excel using...

Time Sequences in Excel by Minute, Hour, or Second Increments

Creating time sequences, whether by hour, minute, or second increments in Excel, can be...

More like this

XMATCH Row by Row: Finding Values Across a Range in Google Sheets

Using the BYROW function with XMATCH in Google Sheets allows us to match values...

Limit Formula Expansion to a Specific Row in Google Sheets

In this tutorial, I’ll explain how to limit the expansion of an array formula...

3-D Referencing Structured Data Tables in Google Sheets

When you have several tables within a single sheet—not across multiple sheets in a...

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.