Excel bug/feature: recursive VLOOKUP for hierarchy calculations

There's lots of Q&A websites out there talking about Excel bugs that are actually formula errors, and others that talk about recursive VLOOKUPs when they really mean iterative. To be clear on what we are talking about:
Excel (2007, 2010, 2011)  has an undocumented feature where you can use circular reference in the third argument of VLOOKUP without iterative calculation.

Why should I care? 

Here's one type of problem you can solve with this "feature". Suppose we have an organizational chart and want to find how many levels deep each person is. Similar to this problem posted on MrExcel.


IDBOSSNAME
1John Smith
21Susan Jones
32Alice Johnson
42Tim Moore
51Rachel Parker
61Tom Allen
75Michael Gross
85Kim Dole
96Kathy Roberts
106Betsy Foster
1110Mini Foster

You can do it with this in cell D2:  
=IF(ISBLANK(B2),1,VLOOKUP(B2,A:D,4,FALSE)+1)

So what's going on here? For the first row, we have a cop-out, this rule makes LEVEL = 1 for John Smith who has no boss. For the second row VLOOKUP finds Susan's boss and then looks up John's LEVEL and adds one.

So that's cute, since the order of operations by default goes from top to bottom, Excel will evaluate the rows above it and find the needed value, right? Wrong. Excel will set its execution order correctly to account for the problem.

IDBOSSNAMELEVEL
32Alice Johnson3
106Betsy Foster3
1John Smith1
96Kathy Roberts3
85Kim Dole3
75Michael Gross3
1110Mini Foster4
51Rachel Parker2
21Susan Jones2
42Tim Moore3
61Tom Allen2


Now... what if you enter a value that is a legitimate circular reference?

11Bob=VLOOKUP(1,A:D,4,false)

In 2007 and 2010 calculation is halted without an error. In Excel 2011, this throws an error correctly.
So in conclusion, Excel will allow you to evaluate true recursive problems with VLOOKUP without resorting to iterative calculation, which nobody wants to use. The engine just works and produces the correct answer if it is mathematically possible.

Comments

Popular posts from this blog

Nontechnical: What is ERC-721?

I Was Kidnapped in Manila and Lived to Tell About it

Grandmom bakes virus bread