Close this search box.

Find the Average of Closest 2 Numbers out of 3 [formula challenge]



Today I am asking you a tricky formula question. This is asked by Ionel on the Introduction to VLOOKUP, OFFSET & MATCH Formulas post.

The question is,

I have data in three columns: A,B,C and I want to get the average of the closest two values out of three in each row. Could you help me with a formula for this?

Now, how would you go about it?

Calculating Average of closest 2 numbers using ExcelWhat does closest of two mean?

We can assume that close-ness is nothing but distance between 2 numbers on numeric scale. So 3 is closer to 2 and 4 compared to 1 or 5.

Your challenge:

Assuming your data is in A2:C10, what formula will you write in D2:D10 to solve this?

Go ahead and get some coffee and get thinking.

Want to cop-out?

I have posted one solution in the next comment. You can see how I went about solving it.

More Formula Challenges:


Share this tip with your colleagues

Excel and Power BI tips - Chandoo.org Newsletter

Get FREE Excel + Power BI Tips

Simple, fun and useful emails, once per week.

Learn & be awesome.

Welcome to Chandoo.org

Thank you so much for visiting. My aim is to make you awesome in Excel & Power BI. I do this by sharing videos, tips, examples and downloads on this website. There are more than 1,000 pages with all things Excel, Power BI, Dashboards & VBA here. Go ahead and spend few minutes to be AWESOME.

Read my storyFREE Excel tips book

Overall I learned a lot and I thought you did a great job of explaining how to do things. This will definitely elevate my reporting in the future.
Rebekah S
Reporting Analyst
Excel formula list - 100+ examples and howto guide for you

From simple to complex, there is a formula for every occasion. Check out the list now.

Calendars, invoices, trackers and much more. All free, fun and fantastic.

Advanced Pivot Table tricks

Power Query, Data model, DAX, Filters, Slicers, Conditional formats and beautiful charts. It's all here.

Still on fence about Power BI? In this getting started guide, learn what is Power BI, how to get it and how to create your first report from scratch.

Compare two Excel sheets

How to compare two Excel sheets using VLOOKUP? [FREE Template]

You are the boss of ACME Inc. And one day, both of your accounts receivables team members Sara and James come to you with two versions of the customer payment data. How do you compare these two Excel sheets and reconcile the data? In this article, let me explain the step by step process.

64 Responses to “Find the Average of Closest 2 Numbers out of 3 [formula challenge]”

  1. Vipul says:

    Chandoo - liked your helper column approach. But I think this problem can have multiple answers in certain cases. E.g. 21,22,23 (from your file) the closest two can be 21,22 or 22,23 and hence the average can be 21.5 or 22.5!

  2. Ramesh says:

    Hi Chandoo, I quickly tried something for this. But I have used a few helper columns.


  3. Daan says:

    Acknowledging the problem mentioned by Vipul, a simple formula suffices:

    =if(max(a1:c1)-median(a1:c1)> median(a1:c1)-min(a1:c1), mean(median(a1:c1), min(a1:c1)), mean(median(a1:c1), max(a1:c1)))

    Vipul's problem could be caught with an additional if statement, or you can play with > or >= to sort out these cases within the formula above.

  4. In D2

    =IF(COUNTIF(A2:C2,AVERAGE(A2:C2))=1,"-", IF(COUNTIF(A2:C2,">"&AVERAGE(A2:C2))=1, (SUM(A2:C2)-MAX(A2:C2))/2,(SUM(A2:C2)-MIN(A2:C2))/2))

  5. Daan says:

    Whoops, guess that should be average() instead of mean().
    Too much Python and too little Excel of late...

  6. Luke M says:


  7. Nunes says:

    In cell D2:


  8. Luke M says:

    =(SUM(A2:C2)- CHOOSE(COUNTIF(A2:C2,">"& AVERAGE(A2:C2)), MAX(A2:C2), MIN(A2:C2)))/2

  9. Nunes says:

    well my last comment got trimmed somehow (actually got =LEFT()'ed)

    here it goes again

    in cell D2 and with no helper column(s)


  10. Brendan says:

    =MIN(ABS(A3:B3-B3:C3), ABS(A3-C3)) (as an array formula, Ctrl-Shift Enter)

  11. =IF(ABS(LARGE(A1:C1,2)-MAX(A1:C1))

  12. [...] don’t know how to comment on Chandoo’s site, so I’ll post it here. I have data in three columns: A,B,C and I want to get the average of [...]

  13. king wiemann says:


  14. Pawel says:


  15. Pawel says:

    the text box cuts the formula


  16. Nunes says:


  17. Nunes says:

    how can i post a comment without having it trimmed?!?!?

  18. Oli says:

    Hi Chandoo - =SUM(MAX(((SMALL(ABS(MEDIAN($A2:$C2)-$A2:$C2),2)=(ABS(MEDIAN($A2:$C2)-$A2:$C2)))*$A2:$C2)),MAX(((MIN(ABS(MEDIAN($A2:$C2)-$A2:$C2))=(ABS(MEDIAN($A2:$C2)-$A2:$C2)))*$A2:$C2)))/2 (CSE formula)

  19. This works for me!

    =(IF(IF(MIN(ABS(LARGE(A2:C2,2) - LARGE(A2:C2,1)), ABS(LARGE(A2:C2,2) - LARGE(A2:C2,3))) = ABS(LARGE(A2:C2,2) - LARGE(A2:C2,1)),1,2) = 1, LARGE(A2:C2,1), LARGE(A2:C2,3)) + LARGE(A2:C2,2)) / 2

    • Rachel says:

      I tried to edit this formula for a data set of 4 values (average the 3 closest values) but was unsuccessful. Any suggestions?

  20. diva says:

    how about this one ?


  21. arun says:


    Is this formula correct?


  22. Rob says:

    Hopefully this comes through okay... there's a couple of less-than signs in it.

    Old-school...no array formulas. Could quickly get ugly if you need to compare more than 3 columns.



  23. Justin says:

    =IF(ABS(MAX(A2:C2)-AVERAGE(A2:C2))<= ABS(MIN(A2:C2)-AVERAGE(A2:C2)), (SUM(A2:C2)-MIN(A2:C2))/2, (SUM(A2:C2)-MAX(A2:C2))/2)
    I finally figured it out - escape the less-than character!

  24. Gregory says:

    You're giving me a headache Chandoo. Here's my formula:


  25. Gregory says:

    Well, the formula didn't post even though I had everything in the comment box. Not worry, @Rob had the same formula, he beat me to the punch.

  26. Fred says:

    I came up with this:


  27. Doug Jenkins says:

    Nice puzzle. Here's my best attempt:


  28. Asheesh Shrivastav says:

    Hi Chandoo,
    I have attempted using IF statement

  29. Geoff says:


  30. Yair says:


  31. Larry says:

    =IF(LARGE(A2:C2,1)-LARGE(A2:C2,2)>LARGE(A2:C2,2)- LARGE(A2:C2,3), (LARGE(A2:C2,2)+LARGE(A2:C2,3))/2, (LARGE(A2:C2,1)+LARGE(A2:C2,2))/2)

  32. Dave says:

    Some very nice and impressive formulae guys, but where in the "real world" would you want to calculate the average of the two closest items to the exclusion of the remaining items?

  33. Drazen says:


  34. Ayush Jain says:

    Hi Chandoo,

    How about this ?


  35. Ayush Jain says:

    Please ignore my solution.... It was about the average of two smallest numbers 🙂

  36. ikkeman says:

    first, the middle number is always part of the average
    second, the numbers are not ordered, so make sure no negative numbers pop up
    finally, I prefer small(1,2,3) to min,median,max because it allows for more flexibility (m,m,m works only for three numbers, small can be extended to any number of columns)

    So while the below is more complex than some - I think it has the benefit of more universal application: (array enter)

  37. Eric says:


  38. Eric says:

    OK, trying again:

    {=SUM((A1:C1 "less than or equal" (AVERAGE(A1:C1)+STDEV(A1:C1)))*(A1:C1 "greater than or equal" (AVERAGE(A1:C1)-STDEV(A1:C1)))*A1:C1)/2}

  39. lhm says:


    i believe this might be the shortest possible.

  40. Ulrik says:

    Fortunately, MEDIAN immediately gives you one of the values when you have an odd number of values. Finding the other value is a bit cumbersome.


  41. Fowmy says:

    I think, the solution posted by Larry is very straightforward and easy for anyone to understand:

    =IF(LARGE(A2:C2,1)-LARGE(A2:C2,2)>LARGE(A2:C2,2)- LARGE(A2:C2,3), (LARGE(A2:C2,2)+LARGE(A2:C2,3))/2, (LARGE(A2:C2,1)+LARGE(A2:C2,2))/2)

  42. Fowmy says:

    This array formula works for me:


  43. Fowmy says:

    My previous post did not display the full formula, how about this one ?

  44. Fowmy says:

    I am struggling to post my full formula.


  45. Abhishek says:

    Hi guys,

    This is my first post in Chandoo though i have been a constant visitor since past 2 months. Must say this is ons of the best excel sites and i like the business writing skills of chandoo apart from the excel skills of course.

    Ok, so here goes my formula is:


  46. KIM WENNERBERG says:

    Isn't it this simple??? All those IFs and
    What are above comments about middle value always being used? I don't think that is the requirement.

  47. KIM WENNERBERG says:

    In above post we assume source data is in col A:C.
    THere are three potential pairs of numbers to compare, thus 3 arguments for MAX.
    Whereas the final number is to be average of two numbers, I just divided MAX difference value by 2 instead of using AVERAGE.

  48. KIM WENNERBERG says:

    Now I re-read the requirement-- my formula should be MIN, not MAX

  49. ????? says:


  50. baran says:


  51. Yogesh P says:

    I wrote this one and worked fine....


  52. Yogesh P says:

    why my formula is not displayed correctly in my last post?

  53. yacine says:

    j'ai lu les reponse mais pa deveritable reponse pour moi j'ai porcede comme suit: j'ai fai la soustraction entre tous les trois chiffres en suite un comparaison et je calacul la moyenne sauf la formule elle tres grande mais elle marche impecable si vous voulez je ous l'envoi par email nemyac100@yahoo.fr

    Google translate:
    I read the response but pa deveritable answer for me I porcede as follows: I fai subtraction between every three digits in a row and I compared the average calacul formula except it very much but it works if impecable I want you to send e mail nemyac100@yahoo.fr

  54. Lane says:

    Dead thread, but I still felt like sharing. Does not address the possibility of there being multiple couples with same range.


  55. Ulrik says:


  56. Ivan says:

    Hey! I guess the challenge is over now... sorry I just discover it. Most of your proposal doesn't work. here mine:)


    Note: I use Excel in french so just replace "Si" by "if" and "MOYENNE" by "AVERAGE".

  57. PINOY-exceler says:

    not the most "elegant" but it works.


  58. […] don’t know how to comment on Chandoo’s site, so I’ll post it […]

  59. Ramchandra says:

    A different approach

    • Ramchandra says:

      Regret posting without G2
      G2 = Average(A2:C2)


  60. Siva says:

    In the cell D2, (assuming the data is in A2:C2), the formula can be as follows:


Leave a Reply