Longest Chain

yrpsoa

New Member
Joined
Jan 18, 2018
Messages
26
Office Version
  1. 365
Platform
  1. Windows
  2. MacOS
I've been using excel for many years for lots of different applications. However, a very simple 'problem' is stumping me. A colleague came to me asking how to find the longest 'name chain' in a dataset. Eg/ Daniel Craig -> Craig David -> David Bradley -> Bradley Cooper. It's more of a brain exercise than some mission-critical problem, but I jumped in, as I like a good puzzle. I started with first names in col A, last names in col B, and in col C something like '=xlookup(b2,A:A,C:C,-1)+1'. This worked, except it only found the first instance of the last name from col A, rather than the longest chain. I then moved to '=MAX(CHOOSECOLS(FILTER($A$2:$C$20,$A$2:$A$20=B2,{0,0,0}),3))+1' in col C, which is 90% there, as it looks for the largest chain, but runs into problems with circular references if you have Daniel Craig -> Craig David -> David Daniel -> Craig David -> ....

To breakdown the above formula, FILTER returns all the rows with a first name which matches the last name in question, CHOOSECOLS returns only the counts associated with these rows (which are the results of itself from other rows), MAX finds the largest, then +1 as it's the next step in the chain.

There either needs to be a way to adjust the above to return zero if it picks up the same name twice, or my next thought is recursive lambdas where you substitute the name you're referencing for "" and remove it from the lookup column for future use, but it fries my brain thinking how to structure that.

Can anyone think of a solution? I don't want to use macros.

Thanks in advance!
 

Attachments

  • Screenshot 2023-11-09 at 21.00.47.png
    Screenshot 2023-11-09 at 21.00.47.png
    162.5 KB · Views: 37

Excel Facts

Formula for Yesterday
Name Manager, New Name. Yesterday =TODAY()-1. OK. Then, use =YESTERDAY in any cell. Tomorrow could be =TODAY()+1.

Forum statistics

Threads
1,223,908
Messages
6,175,305
Members
452,633
Latest member
DougMo

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top