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:

Facebook
Twitter
LinkedIn

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.

64 Responses

  1. 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. 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.

  3. well my last comment got trimmed somehow (actually got =LEFT()’ed)

    here it goes again

    in cell D2 and with no helper column(s)

    =IF(AND(ABS(A4-B4)

  4. 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)

  5. 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

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

  6. Hi,

    Is this formula correct?

    IF(ABS(A2-B2)=MIN(ABS(A2-B2),ABS(B2-C2),ABS(C2-A2)),AVERAGE(A2,B2),IF(ABS(B2-C2)=MIN(ABS(A2-B2),ABS(B2-C2),ABS(C2-A2)),AVERAGE(B2,C2),AVERAGE(C2,A2)))

  7. 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.

    =IF(ABS(A2-B2)<MIN(ABS(A2-C2),ABS(B2-C2)),AVERAGE(A2,B2),IF(ABS(A2-C2)<MIN(ABS(A2-B2),ABS(B2-C2)),AVERAGE(A2,C2),AVERAGE(B2,C2)))

    Rob

  8. =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!

  9. =IF(MAX(A1:C1)-MEDIAN(A1:C1)>MEDIAN(A1:C1)-MIN(A1:C1),(MIN(A1:C1)*2+(MEDIAN(A1:C1)-MIN(A1:C1)))/2,(MEDIAN(A1:C1)*2+(MAX(A1:C1)-MEDIAN(A1:C1)))/2)

  10. =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)

  11. 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?

  12. =IF((MAX(A2:C2)-MEDIAN(A2:C2))>(MEDIAN(A2:C2)-MIN(A2:C2)),AVERAGE(MIN(A2:C2),MEDIAN(A2:C2)),AVERAGE(MAX(A2:C2),MEDIAN(A2:C2)))

  13. 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)
    =INDEX(((SMALL(A2:C2,{1,1,2})/2+SMALL(A2:C2,{2,3,3})/2)),
    MATCH(MIN(ABS(SMALL(A2:C2,{1,1,2})-SMALL(A2:C2,{2,3,3}))),
    ABS(SMALL(A2:C2,{1,1,2})-SMALL(A2:C2,{2,3,3})),0))

  14. 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}

  15. 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.

    AVERAGE(MEDIAN(A2:C2),ABS(MIN(ABS(MEDIAN(A2:C2)-MAX(A2:C2)), ABS(MEDIAN(A2:C2)-MIN(A2:C2)))-MEDIAN(A2:C2)* IF(ABS(MEDIAN(A2:C2)-MAX(A2:C2))> ABS(MEDIAN(A2:C2)-MIN(A2:C2)),1,-1)))

  16. 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)

  17. 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:

    =IF(AND(ABS(B3-C3)

  18. =MAX(ABS(A1-B1),ABS(A1-C1),ABS(B1-C1))/2
    Notes:
    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.

  19. 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.

  20. 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

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

    =IF(MAX(A2:C2)-LARGE(A2:C2,2)<=SMALL(A2:C2,2)-MIN(A2:C2),(MAX(A2:C2)+LARGE(A2:C2,2))/((COUNT(A2:C2)-1)),(SMALL(A2:C2,2)+MIN(A2:C2))/((COUNT(A2:C2)-1)))

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

    SI(ABS(A3-B3)=MIN(ABS(A3-B3);ABS(A3-C3);ABS(B3-C3));MOYENNE(A3;B3);SI(ABS(A3-C3)=MIN(ABS(A3-B3);ABS(A3-C3);ABS(B3-C3));MOYENNE(A3;C3);MOYENNE(B3;C3)))

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

  23. not the most “elegant” but it works.

    =IF(ABS(B2-A2)<ABS(B2-C2),AVERAGE(A2:B2),IF(A2=C2,AVERAGE(A2,C2),AVERAGE(B2:C2)))

  24. A different approach
    “=IF(MAX(A2:C2)-ABS(G2)>ABS(G2)-MIN(A2:C2),AVERAGEIF(A2:C2,””&MAX(A2:C2)),AVERAGEIF(A2:C2,””&MIN(A2:C2)))”

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

      “=IF(MAX(A2:C2)-ABS(AVERAGE(A2:C2))>ABS(AVERAGE(A2:C2))-MIN(A2:C2),AVERAGEIF(A2:C2,””&MAX(A2:C2)),AVERAGEIF(A2:C2,””&MIN(A2:C2)))”

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

    =0.5*(SUM(A2:C2)-IF(AVERAGE(A2:C2)>AVERAGE(MAX(A2:C2),MIN(A2:C2)),MIN(A2:C2),MAX(A2:C2)))

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.