First a quick personal update: There has been a magnitude 7.8 earth quake in NZ on 14th November 2016 early morning. It is centered in Kaikoura, which is about 250 km away from Wellington. We did feel several shakes and after shocks. It has been an interesting and often scary experience. But my family is safe. I feel very sad for the all the damage and the loss for families in NZ. If you suffered from this quake, My prayers and thoughts are with you.
Yesterday, a friend asked me an interesting question. He has school distance data, like below. He wants to know which is the closest school for each school.
There are a few ways to answer this question. Let’s examine two approaches – formulas & pivot tables and see the merits of both.
Formulas to find closest school
All the distance data is in a table named dist.
Assuming you have school names & types in cells H5, I5, we want to find out the closest school of any type and same type in adjacent columns, as shown below.
Let’s take a look at the formulas first. All of these are array formulas. So press CTRL+Shift+Enter after typing.
- J5: Closest School Distance (Any type): =MIN(IF(dist[From]=H5,dist[Distance]))
- K5: Closest School Name (Any type): =INDEX(dist[To],MATCH(H5&J5,dist[From]&dist[Distance],0))
- L5: Closest School Distance (Same type): =MIN(IF(dist[From]=H5,IF(dist[To Type]=I5,dist[Distance])))
- M5: Closest School Name (Same type): =INDEX(dist[To],MATCH(H5&L5,dist[From]&dist[Distance],0))
How do these formulas work?
Let’s examine them one at a time.
Closest School Distance (Any type)
Formula: =MIN(IF(dist[From]=H5,dist[Distance]))
How it works:
- We check if From school is same as the one in H5 and get the corresponding distances only.
- This will return a bunch of distances and FALSE values. Distances will be listed only for the schools that match H5, for all others, the IF() gives FALSE.
- We then pass this list to MIN formula to find the minimum distance.
As we are using arrays inside IF formula, we must press Ctrl+Shift+Enter to get correct results.
Related: Learn more about MAXIF & MINIF formulas.
Closest School Distance (Same type)
Formula: =MIN(IF(dist[From]=H5,IF(dist[To Type]=I5,dist[Distance])))
How it works:
- We check if From school is same as the one in H5 and if the [To Type] is same as I5 and get the corresponding distances only.
- This will return a bunch of distances and FALSE values. Distances will be listed only for the schools that match H5 and of type I5, for all others, the IF() gives FALSE.
- We then pass this list to MIN formula to find the minimum distance.
Finding the corresponding school name:
Once we know the minimum school distance, we just use array MATCH to find corresponding school number and get the name of the school with an INDEX().
=INDEX(dist[To],MATCH(H5&J5,dist[From]&dist[Distance],0))
As we are concatenating two lists in the MATCH formula, we need to press Ctrl+Shift+Enter to get correct results.
We use same logic to fetch school name for the distance in column L too.
Related: Learn about multi-condition lookups
Formula approach – comments
While the formula approach gives answers we want, it is very tricky to write these formulas. The MIN(IF(…)) structure is not easy to master.
As the formulas check entire data, they can be very slow on large sets.
Pivot table to find closest school
First create a pivot table from the dist table with below settings:
- Add From and From type to row labels area
- Add To and To type to column labels area
- Add distance to values area, summarize it by SUM
- Remove sub totals & grand totals
- Set up pivot in tabular layout
We get this.
At this stage, finding closest school gets easy. We simply use SMALL formula on each pivot table row to find 2nd smallest value (because smallest value is 0 and we should ignore it.) to get the distance. Finding school name is a simple matter of using INDEX + MATCH.
Of course, finding the distance for closest school of same type still requires using array version of SMALL with SMALL(IF(…)) structure. But this formula would be significantly faster as we don’t process all the 10000 rows of data.
Comments on Pivot Table approach
Pivot table approach simplifies the problem and helps us answer the questions faster. You can also apply conditional formatting on top of Pivot Table to instantly highlight closest school(s).
Download example workbook
Click here to download the closest school example workbook. Play with the formulas & pivot table to learn more. Examine the conditional formatting rules for some cool techniques.
How would you find the closest school?
By asking your neighbors, of course. Jokes aside, how would you find the closest school for a given school? Would you use formulas or pivot tables or some other approach? Please share your thoughts in the comments.
Need to learn, here is your closest school
If you need to master Excel, look no farther. Excel School, your closest and most awesome online class makes you, well, awesome in Excel. Learn from basics to advanced concepts, all from the comfort of your office or home. There are over 50 lessons and dozens of sample workbooks to make you an Excel pro.
14 Responses to “Finding the closest school [formula vs. pivot table approach]”
Another advantage of the Pivot Table approach is that it deals with ties.
To get the closest directly using the pivot table couldn´t be used a new calculated field with the formula:
= If(Distance =0;NOD();Distance )
Then use a table with columns as:
From|From type|To|To Type
And the new value crossed.
Then apply a filter in the To column so it shows only the least value ( 10 best filter ) ??
With that u get a table with the answer to the closest school.
By the way... there are some schools with more than 1 option as closest school. 😉
As for the answer for same school... just filtering the To Type would force the pivot table to give away the correct answer.
May be using 3xpivot tables with From/To types fixed could give away the answer...
Thanks for sharing. Interesting.
Well written. Thanks.
Nice tip. Thanks a lot
Your formula to find the name of the closest school of the same type is incorrect. For example, SCH-0046 (Primary) is shown as the closest of same type to SCH-0028 (High).
Yes, Gerard, you are absolutely right.
I landed on this page of Chandoo's blog after I clicked the link in his email titled "The secret to quickly analyze & make sense of any data ... (Chandoo.org)". The link took me to this Case Study about Finding the Closest School.
The correct formula for the distance to the closest school of the same type is:
=MIN(IF((dist[To]$H6)*(dist[From]=$H6)*(dist[To Type]=$I6),dist[Distance],""))
This formula would also work if the distance from a school to itself was entered as 0 instead of being left blank.
And the ID of the closest school of the same type is then given by the following formula:
=INDEX(dist[To],MATCH(1,(dist[From]=$H6)*(dist[Distance]=Q6)*(dist[To Type]=$I6),0))
Needless to say, both formulas are CSE (array) formulas and got to entered with CTRL+SHIFT+ENTER.
Great idea .. Excellent tips.
Really information article…
Thanks for sharing..
I Liked your Blog Post.
Great post. Thank you for sharing.
Thanks for sharing with us this important article.
that's a brilliant one. Really helpful.