+ Reply to Thread
Results 1 to 16 of 16

Can SUMPRODUCT do OR statements?

  1. #1
    Forum Contributor tangcla's Avatar
    Join Date
    06-04-2008
    Location
    Melbourne, Australia
    MS-Off Ver
    2010
    Posts
    136

    Can SUMPRODUCT do OR statements?

    So, let's say my basic SUMPRODUCT formula starts off like this:

    SUMPRODUCT($G$35:$G$3082*($D$35:$D$3082="OP")

    Can I get it so that the SUMPRODUCT gives me results for criteria in which D35-D3082 is, say, OP and OT and OZ?

  2. #2
    Forum Expert shg's Avatar
    Join Date
    06-20-2007
    Location
    The Great State of Texas
    MS-Off Ver
    2003, 2010
    Posts
    40,678
    Let's say you try SUMPRODUCT($G$35:$G$3082 * ($D$35:$D$3082 = {"OP","OT","OZ"})

  3. #3
    Forum Contributor tangcla's Avatar
    Join Date
    06-04-2008
    Location
    Melbourne, Australia
    MS-Off Ver
    2010
    Posts
    136
    Let's say you're awesome.

  4. #4
    Forum Expert shg's Avatar
    Join Date
    06-20-2007
    Location
    The Great State of Texas
    MS-Off Ver
    2003, 2010
    Posts
    40,678
    Well, let's say thank you

  5. #5
    Valued Forum Contributor Richard Schollar's Avatar
    Join Date
    05-23-2006
    Location
    Hampshire UK
    MS-Off Ver
    Excel 2002
    Posts
    1,264
    Hi

    An alternative approach which is more efficient is:

    SUMPRODUCT($G$35:$G$3082*(ISNUMBER(MATCH($D$35:$D$3082,{"OP","OT","OZ"},0)))

    The efficiency gain comes from retaining a 1 dimensional array in the final output passed to Sumproduct. I suspect that any real world benefit is questionable unless you are using really big ranges though, but it's always good to know of a few ways to do things.

    Richard

  6. #6
    Forum Contributor tangcla's Avatar
    Join Date
    06-04-2008
    Location
    Melbourne, Australia
    MS-Off Ver
    2010
    Posts
    136
    Quote Originally Posted by shg
    Well, let's say thank you
    Let's say I do

    thank you I forgot to say it before

  7. #7
    Forum Contributor tangcla's Avatar
    Join Date
    06-04-2008
    Location
    Melbourne, Australia
    MS-Off Ver
    2010
    Posts
    136
    Quote Originally Posted by RichardSchollar
    Hi

    An alternative approach which is more efficient is:

    SUMPRODUCT($G$35:$G$3082*(ISNUMBER(MATCH($D$35:$D$3082,{"OP","OT","OZ"},0)))

    The efficiency gain comes from retaining a 1 dimensional array in the final output passed to Sumproduct. I suspect that any real world benefit is questionable unless you are using really big ranges though, but it's always good to know of a few ways to do things.

    Richard
    There's so many formulas in Excel, so much unharnessed power... but let me try and master sumproduct first hehe

  8. #8
    Forum Expert
    Join Date
    01-15-2007
    Location
    Brisbane, Australia
    MS-Off Ver
    2007
    Posts
    6,591
    Hi

    Merely to add to the confusion with another syntax approach.....

    SUMPRODUCT((G35:G3082),(D35:D3082="OP")+(D35:D3082="OT")+(D35:D3082="OZ"))


    rylo

  9. #9
    Forum Expert Colin Legg's Avatar
    Join Date
    03-30-2008
    Location
    UK
    MS-Off Ver
    365
    Posts
    1,256
    In case anyone is interested..... I took a little time to test the calculation speeds of these formulas by measuring the calculation time of a worksheet containing 800 of each formula.

    The average results are below. If anyone else tests them, then the ratios should be similar. I'd have to test more iterations to get the results absolutely reliable, but I think they provide a good indicator as they stand.

    Formula 1 ---- Average Speed 2.3375 secs
    =SUMPRODUCT($G$33:$G$3080*(ISNUMBER(MATCH($D$33:$D$3080,{"OP","OT","OZ"},0))))

    Formula 2 ---- Average Speed 2.835938 secs
    =SUMPRODUCT($G$33:$G$3080*($D$33:$D$3080={"OP","OT","OZ"}))

    Formula 3 ---- Average Speed 2.8625 secs
    =SUMPRODUCT(($G$33:$G$3080),($D$33:$D$3080="OP")+($D$33:$D$3080="OT")+($D$33:$D$3080="OZ"))

    Formula 4 ---- Average Speed 2.875 secs *This formula is CTRL+SHIFT+ENTER entered....
    =SUM($G$33:$G$3080*($D$33:$D$3080={"OP","OT","OZ"}))


    So Richard is correct: his version (formula 1) is more efficient, perhaps at a cost of being slightly more complex(?), whilst the other solutions are quite similar.

    That said, I was able to improve its efficiency further by coercing the boolean result array into a numerical result array using negation, and then passing two numerical arrays into Sumproduct and letting it multiply/sum them together, and then converting the numerical result returned from the function into a postive number using negation again. So my proposed solution is this variation:

    Formula 5 ---- Average Speed 1.84375 secs
    =-SUMPRODUCT($G$33:$G$3080,-(ISNUMBER(MATCH($D$33:$D$3080,{"OP","OT","OZ"},0))))

    It's a pretty big time save over formula 1 but, again, the compromise is that it is more complicated...


    Hope that's of interest...
    Colin
    Last edited by Colin Legg; 08-14-2008 at 01:42 PM.

  10. #10
    Forum Expert
    Join Date
    01-15-2007
    Location
    Brisbane, Australia
    MS-Off Ver
    2007
    Posts
    6,591
    Colin

    Found that intesting.

    On formula 5, can you test with the forced coercion in this construct

    =SUMPRODUCT($G$33:$G$3080,--(ISNUMBER(MATCH($D$33:$D$3080,{"OP","OT","OZ"},0))))
    I doubt that it would be any better, but to me it makes the formula more consistent.

    From Colin_I via PM.....
    Basically, in terms of efficiency:
    -SUMPRODUCT({Numbers},-{Booleans}) is faster than
    SUMPRODUCT({Numbers},--{Booleans}) is faster than
    SUMPRODUCT({Numbers}*{Booleans})

    rylo
    Last edited by rylo; 08-17-2008 at 06:19 PM. Reason: Added timing update from Colin

  11. #11
    Forum Expert shg's Avatar
    Join Date
    06-20-2007
    Location
    The Great State of Texas
    MS-Off Ver
    2003, 2010
    Posts
    40,678
    Quote Originally Posted by tangcla
    Let's say you're awesome.
    Quote Originally Posted by shg
    Well, let's say thank you
    Quote Originally Posted by tangcla
    Let's say I do. Thank you, I forgot to say it before
    What I meant was thank you for the compliment -- it wasn't a nudge for more appreciation.

    Thanks for posting the follow-up.

  12. #12
    Forum Contributor tangcla's Avatar
    Join Date
    06-04-2008
    Location
    Melbourne, Australia
    MS-Off Ver
    2010
    Posts
    136
    haha, I'm confused now I think I'll stick with what I had.

  13. #13
    Forum Expert daddylonglegs's Avatar
    Join Date
    01-14-2006
    Location
    England
    MS-Off Ver
    2016
    Posts
    14,675
    There are some other potential differences. Any text in a single cell in the sum range G33:G3080 [even a "formula blank"] will cause formulas 1, 2 and 4 to fail, 3 and 5 won't.

    I presume this is part of a larger SUMPRODUCT formula but, if not, this might be as quick

    =SUM(SUMIF($D$35:$D$3082,{"OP","OT","OZ"},$G$35:$G$3082))

  14. #14
    Forum Contributor tangcla's Avatar
    Join Date
    06-04-2008
    Location
    Melbourne, Australia
    MS-Off Ver
    2010
    Posts
    136
    Quote Originally Posted by daddylonglegs
    There are some other potential differences. Any text in a single cell in the sum range G33:G3080 [even a "formula blank"] will cause formulas 1, 2 and 4 to fail, 3 and 5 won't.

    I presume this is part of a larger SUMPRODUCT formula but, if not, this might be as quick

    =SUM(SUMIF($D$35:$D$3082,{"OP","OT","OZ"},$G$35:$G$3082))
    yes it's part of a bigger sumproduct (there's three nested conditions).

    I noticed that an error like #N/A will cause the formula to fail, however I did an IF(ISERROR(vlookup on it so that it would return a blank value, and then it works.

  15. #15
    Forum Expert daddylonglegs's Avatar
    Join Date
    01-14-2006
    Location
    England
    MS-Off Ver
    2016
    Posts
    14,675
    You might be able to make the formula work with #N/As ignored.

    For instance, with a version of Richard's suggestion,

    =SUMPRODUCT($G$33:$G$3082,--ISNUMBER(MATCH($D$33:$D$3082,{"OP","OT","OZ"},0)))

    #N/As in D33:D3082 won't stop you getting a result

    This formula will allow #N/As in either range

    =SUM(IF(ISNUMBER(MATCH($D$33:$D$3082,{"OP","OT","OZ"},0)),IF(ISNUMBER($G$33:$G3082),$G$33:$G3082)))

    confirmed with CTRL+SHIFT+ENTER

    Although, of course, you might prefer an error if you want to be alerted to #N/As when you didn't know they were there

  16. #16
    Forum Contributor tangcla's Avatar
    Join Date
    06-04-2008
    Location
    Melbourne, Australia
    MS-Off Ver
    2010
    Posts
    136
    Hello, time to dig up an old thread - daddylonglegs, in your last post you mentioned about a SUMPRODUCT formula which will allow errors and blanks.

    Can you please elaborate on how one constructs a formula with this? I now need a formula for this!

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

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