+ Reply to Thread
Results 1 to 22 of 22

SUMIF to return only first two matches

  1. #1
    Registered User
    Join Date
    09-30-2019
    Location
    Aberdeen, Scotland
    MS-Off Ver
    Office 365
    Posts
    9

    SUMIF to return only first two matches

    I am looking for a SUMIF that returns only the first two matches, not the results from the full table. In the example below, I would like to return a SUM of 5 (not 6), please.

    Bacon.PNG

  2. #2
    Forum Moderator Glenn Kennedy's Avatar
    Join Date
    07-08-2012
    Location
    Digital Nomad... occasionally based in Ireland.
    MS-Off Ver
    O365 (PC) V 2403
    Posts
    44,064

    Re: SUMIF to return only first two matches

    How is this:
    =SUMIFS($C$4:$C$100,$B$4:$B$100,F3,$C$4:$C$100,"<="&AGGREGATE(15,6,$C$4:$C$100/($B$4:$B$100=F3),2))
    Attached Files Attached Files
    Glenn




    None of us get paid for helping you... we do this for fun. So DON'T FORGET to say "Thank You" to all who have freely given some of their time to help YOU.

    Temporary addition of accented to illustrate ongoing problem to the TT: Lá fhéile Pádraig sona dhaoibh

  3. #3
    Registered User
    Join Date
    09-30-2019
    Location
    Aberdeen, Scotland
    MS-Off Ver
    Office 365
    Posts
    9

    Re: SUMIF to return only first two matches

    It's close - collecting the right data, but what it has done has picked up the last match, then the first match (instead of the first two matches)

    If I change the last criteria in the formula to 3 instead of 2, it then picks up the last, first & second matches.

  4. #4
    Registered User
    Join Date
    09-30-2019
    Location
    Aberdeen, Scotland
    MS-Off Ver
    Office 365
    Posts
    9

    Re: SUMIF to return only first two matches

    Actually, I can see that it is finding the smallest numerical match + the next smallest when the formula restricts it to 2.

    Rather than the smallest 2 numerical matches, I am simply looking to find the first 2 matches as they appear in the list (regardless of value).

  5. #5
    Forum Expert
    Join Date
    09-30-2019
    Location
    Chiangmai, Thailand
    MS-Off Ver
    Office 2016, Excel 2019
    Posts
    1,234

    Re: SUMIF to return only first two matches

    (Sorry about my English)

    I just can do this.

    Put a value you want to sum as "Bacon" in E3.
    And then use this formula

    =IFERROR(INDIRECT("C"&AGGREGATE(15,6,ROW($B$4:$B$14)/(1/($B$4:$B$14=$E$3)),1)),0)+
    IFERROR(INDIRECT("C"&AGGREGATE(15,6,ROW($B$4:$B$14)/(1/($B$4:$B$14=$E$3)),2)),0)


    Sorry , this may not help much.

    Regards.

  6. #6
    Forum Moderator Glenn Kennedy's Avatar
    Join Date
    07-08-2012
    Location
    Digital Nomad... occasionally based in Ireland.
    MS-Off Ver
    O365 (PC) V 2403
    Posts
    44,064

    Re: SUMIF to return only first two matches

    Inzaghi, you have me baffled. As afar as I can see it's fine. Unfortunately, you didn't post a file showing it calculating the wrong result, to I can only guess that you have made an error.

    See file, with a modified choice of values, and an unchanged formula. Show me EXACTLY what is wrong.
    Attached Files Attached Files

  7. #7
    Forum Moderator Glenn Kennedy's Avatar
    Join Date
    07-08-2012
    Location
    Digital Nomad... occasionally based in Ireland.
    MS-Off Ver
    O365 (PC) V 2403
    Posts
    44,064

    Re: SUMIF to return only first two matches

    Unfortunately the attachment icon doesn't work at the moment. So, to attach an Excel file you have to do the following: Just before posting, scroll down to Go Advanced and then scroll down to Manage Attachments. Now follow the instructions at the top of that screen.

  8. #8
    Registered User
    Join Date
    09-30-2019
    Location
    Aberdeen, Scotland
    MS-Off Ver
    Office 365
    Posts
    9

    Re: SUMIF to return only first two matches

    Glenn,

    If you change the "6" in C8 to "150" (above the value of the next biggest entry for "W"), then the new value returned becomes 109 (i.e. the SUM of the two smallest matches). I'd want the value returned to be 159 (i.e. the SUM of the first two matches for W). See attached.
    Attached Files Attached Files

  9. #9
    Forum Expert
    Join Date
    07-20-2011
    Location
    Mysore, India.
    MS-Off Ver
    Excel 2019
    Posts
    8,587

    Re: SUMIF to return only first two matches

    This works
    ARRAY formula

    =IFERROR(SUM((IFERROR(C4:C16*(IF(B4:B16=F3,ROW(B4:B16),"")<=SMALL(IF(B4:B16=F3,ROW(B4:B16),""),2)),0))),"")

    How ARRAY formula is entered

    Paste Formula in the cell.
    Press F2
    Hold Shift+ Ctrl Keys and hit Enter key.
    Now the formula is surrounded by {} by excel.
    Attached Files Attached Files
    Pl note
    Array formula should be confirmed with Ctrl+Shift+Enter keys together.
    If answere is satisfactory press * to add reputation.

  10. #10
    Forum Moderator Glenn Kennedy's Avatar
    Join Date
    07-08-2012
    Location
    Digital Nomad... occasionally based in Ireland.
    MS-Off Ver
    O365 (PC) V 2403
    Posts
    44,064

    Re: SUMIF to return only first two matches

    Well, ain't that a pain in the butt!! I was certain it was OK.

    Use this ordinary formula:

    =SUM(INDEX(($C$4:$C$16)*($B$4:$B$16=F3)*(ROW($4:$16)<=SMALL(INDEX(ROW($4:$16)+($B$4:$B$16<>F3)*1E+99,,), 2)),,))

    Do your best to break it!!
    Attached Files Attached Files

  11. #11
    Registered User
    Join Date
    09-30-2019
    Location
    Aberdeen, Scotland
    MS-Off Ver
    Office 365
    Posts
    9

    Re: SUMIF to return only first two matches

    Glenn, you've cracked it! Had to tweak/simplify my data table to make it work, but its singing beautifully now. Spent hours trying to figure it out myself, until eventually, at the age of 39, admitted defeat and asked for help! Very happy you've fixed this for me, many thanks!

  12. #12
    Forum Moderator Glenn Kennedy's Avatar
    Join Date
    07-08-2012
    Location
    Digital Nomad... occasionally based in Ireland.
    MS-Off Ver
    O365 (PC) V 2403
    Posts
    44,064

    Re: SUMIF to return only first two matches

    Great. Glad to have sorted you out. Sadly, I'll not see 39 again, but never afraid to admit defeat!!

  13. #13
    Forum Expert XLent's Avatar
    Join Date
    10-13-2010
    Location
    Northumberland, UK
    MS-Off Ver
    various
    Posts
    2,704

    Re: SUMIF to return only first two matches

    FWIW, re: AGGREGATE method:

    =SUM(MOD(AGGREGATE(15,6,(ROW($C$4:$C$16)*10^7+$C$4:$C$16)/($B$4:$B$16=$F$3),{1,2}),10^7))

  14. #14
    Registered User
    Join Date
    09-30-2019
    Location
    Aberdeen, Scotland
    MS-Off Ver
    Office 365
    Posts
    9

    Re: SUMIF to return only first two matches

    Quote Originally Posted by Glenn Kennedy View Post
    Well, ain't that a pain in the butt!! I was certain it was OK.

    Use this ordinary formula:

    =SUM(INDEX(($C$4:$C$16)*($B$4:$B$16=F3)*(ROW($4:$16)<=SMALL(INDEX(ROW($4:$16)+($B$4:$B$16<>F3)*1E+99,,), 2)),,))

    Do your best to break it!!
    Glenn, building on the quoted fix, if I wanted to apply the same formula to 2 potential columns (SUMIFS or SUMIF(OR() I presume?), how would the formula change? In the attached example for the first 3 instances of 'E' I'd want to return an sum of 612.
    Attached Files Attached Files

  15. #15
    Registered User
    Join Date
    12-03-2012
    Location
    CHENNAI , INDIA
    MS-Off Ver
    Excel 2007
    Posts
    93

    Re: SUMIF to return only first two matches

    Hi ,

    Using helper columns makes it simple.

    Use one helper column with the formula :
    Formula: copy to clipboard
    Please Login or Register  to view this content.
    . Enter this in L4 and copy down.

    Use a second helper column with the formula :
    Formula: copy to clipboard
    Please Login or Register  to view this content.
    . Enter this in M4 and copy it down.

    The formula in cell I4 is :
    Formula: copy to clipboard
    Please Login or Register  to view this content.
    . This is an array formula , to be entered using CTRL SHIFT ENTER.

    Narayan
    Attached Files Attached Files

  16. #16
    Forum Expert XLent's Avatar
    Join Date
    10-13-2010
    Location
    Northumberland, UK
    MS-Off Ver
    various
    Posts
    2,704

    Re: SUMIF to return only first two matches

    Quote Originally Posted by inzaghi View Post
    ...if I wanted to apply the same formula to 2 potential columns (SUMIFS or SUMIF(OR() I presume?), how would the formula change? In the attached example for the first 3 instances of 'E' I'd want to return an sum of 612.
    with the AGGREGATE approach:

    Formula: copy to clipboard
    Please Login or Register  to view this content.

  17. #17
    Registered User
    Join Date
    09-30-2019
    Location
    Aberdeen, Scotland
    MS-Off Ver
    Office 365
    Posts
    9

    Re: SUMIF to return only first two matches

    Formula works perfectly, thank-you very much! In addition, would there be a way to make it work if the data wasn't in the subsequent column to the reference (E)? Example attached, where again I'd be looking to return a sum of 612.
    Attached Files Attached Files

  18. #18
    Forum Expert XLent's Avatar
    Join Date
    10-13-2010
    Location
    Northumberland, UK
    MS-Off Ver
    various
    Posts
    2,704

    Re: SUMIF to return only first two matches

    using that specific sample file layout:

    Formula: copy to clipboard
    Please Login or Register  to view this content.

  19. #19
    Registered User
    Join Date
    09-30-2019
    Location
    Aberdeen, Scotland
    MS-Off Ver
    Office 365
    Posts
    9

    Re: SUMIF to return only first two matches

    Excellent, again that works perfectly, thank-you.

    Just one final thing, if I then wanted to sum all instances, rather than just the first 3, what would I take out of the formula?

  20. #20
    Forum Guru
    Join Date
    08-28-2014
    Location
    USA
    MS-Off Ver
    Excel 2019
    Posts
    17,572

    Re: SUMIF to return only first two matches

    Try pasting the following into cell N3: =SUM(SUMIFS(J$4:J$16,B$4:B$16,M3),SUMIFS(K$4:K$16,E$4:E$16,M3))
    Let us know if you have any questions.
    Consider taking the time to add to the reputation of everybody that has taken the time to respond to your query.

  21. #21
    Registered User
    Join Date
    09-30-2019
    Location
    Aberdeen, Scotland
    MS-Off Ver
    Office 365
    Posts
    9

    Re: SUMIF to return only first two matches

    Awesome, thank you JeteMc, that works perfectly!

  22. #22
    Forum Guru
    Join Date
    08-28-2014
    Location
    USA
    MS-Off Ver
    Excel 2019
    Posts
    17,572

    Re: SUMIF to return only first two matches

    You're Welcome and thank you for the feedback. Please take a moment to mark the thread as 'Solved' using the thread tools menu above your first post. I hope that you have a blessed day.

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. SUMIF (maybe?) help needed - Copy value if text cell matches
    By geamer in forum Excel Formulas & Functions
    Replies: 4
    Last Post: 04-10-2018, 08:59 AM
  2. [SOLVED] If date matches and room matches, return room info in to cell
    By HonorBray in forum Excel Formulas & Functions
    Replies: 6
    Last Post: 03-12-2018, 12:09 PM
  3. [SOLVED] SUMIF value matches one in range
    By shoschett in forum Excel Formulas & Functions
    Replies: 2
    Last Post: 04-29-2014, 09:17 AM
  4. =SUMIF but contains instead of matches
    By tyleromaha in forum Excel Formulas & Functions
    Replies: 2
    Last Post: 08-09-2012, 12:53 PM
  5. Replies: 6
    Last Post: 06-22-2012, 02:25 PM
  6. Sumif and matches
    By maymano in forum Excel General
    Replies: 4
    Last Post: 07-04-2011, 07:53 AM
  7. SumIf Criteria Matches
    By Debbie Dies in forum Excel Formulas & Functions
    Replies: 68
    Last Post: 09-06-2005, 07:05 PM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1