+ Reply to Thread
Results 1 to 21 of 21

Conditional sum

  1. #1
    Registered User
    Join Date
    01-23-2010
    Location
    Switzerland
    MS-Off Ver
    Excel 2004
    Posts
    10

    Conditional sum

    Hi,
    I need help on how to write a conditional sum for a specific problem. I tried with SUMIF and SUM(IF()), but I can't seem to find the right thing.

    Basically, I want to sum cells in a column (A) for which cells in another column (B) are lower than a certain value specified in a given cell.

    Exemple: if my columns A and B are:

    A B
    1) 100 3
    2) 210 5
    3) 350 1
    4) 150 2
    5) 300 4

    if I want to add all values in A for which cells in B are lower than or equal to 3, it's easy:
    sumif(B1:B5, "<=3", A1:A5) will give the correct result 600 (that is, 100+350+150)

    However I will need have a non constant criteria: instead of requiring the values in B to be lower than or equal to the constant "3", I would to specify that I want the values lower than or equal to THE VALUE IN CELL B1.
    In fact, I would like something like this:
    sumif(B1:B5, "<=B1", A1:A5) but of course if does not work because the criteria can only involve constant values and not other cells.

    The reason is that I want to then calculate this for each line:
    for line 1, find the sum of all values in A for which cells in B are lower than B1
    for line 2, find the sum of all values in A for which cells in B are lower than B2
    for line 3, find the sum of all values in A for which cells in B are lower than B3, etc.


    The expected results, could then we displayed in a column C, which would yield:
    A B C
    1) 100 3 600
    2) 210 5 1110
    3) 350 1 350
    4) 150 2 500
    5) 300 4 900

    Do you have an idea on what would be the simplest method?
    Of course, I could in principle sort the data in ascending order of the column B and calculate the partial sums in column C: however, the values in column B (and thus, the order) is dependent on other variable parameters so I cannot resort everything everytime the values in B change.

    Thanks in advance!

  2. #2
    Valued Forum Contributor
    Join Date
    07-29-2009
    Location
    Belgium
    MS-Off Ver
    Excel 2003/Excel 2010
    Posts
    534

    Re: Help with conditional sums

    You were pretty close with: sumif(B1:B5, "<=B1", A1:A5)
    Attached Files Attached Files

  3. #3
    Registered User
    Join Date
    01-23-2010
    Location
    Switzerland
    MS-Off Ver
    Excel 2004
    Posts
    10

    Re: Help with conditional sums

    Thanks!
    However, a little problem arises: if we change the values in column A (say, if you change A2 from 210 to 310, your suggestion does not automatically take into account the modification. We then need to re-drag the formula over column C to make it refresh the new data. Any idea on how to avoid this?

    Apart from this detail, I am simply curious to understand the logic of your formula:
    - I have never used a symbol "&" in a function. Wondering what it does in general and in our case.
    - I am surprised that you only reference the cell $A$1 in the last argument of the function. Looks as if the other cells of column A are never involved. However, they obviously are, since the whole formula works fine! I wonder how come...

  4. #4
    Forum Expert martindwilson's Avatar
    Join Date
    06-23-2007
    Location
    London,England
    MS-Off Ver
    office 97 ,2007
    Posts
    19,320

    Re: Help with conditional sums

    shouldn't it be
    =SUMIF($B$1:$B$15,"<="&B1,$A$1:$A$15)
    "Unless otherwise stated all my comments are directed at OP"

    Mojito connoisseur and now happily retired
    where does code go ?
    look here
    how to insert code

    how to enter array formula

    why use -- in sumproduct
    recommended reading
    wiki Mojito

    how to say no convincingly

    most important thing you need
    Martin Wilson: SPV
    and RSMBC

  5. #5
    Registered User
    Join Date
    01-23-2010
    Location
    Switzerland
    MS-Off Ver
    Excel 2004
    Posts
    10

    Re: Help with conditional sums

    That's what I thought too, but WHER's formula clearly provides results that do take into account all cells in column A, even though only $A$1 is specified...

  6. #6
    Valued Forum Contributor
    Join Date
    07-29-2009
    Location
    Belgium
    MS-Off Ver
    Excel 2003/Excel 2010
    Posts
    534

    Re: Help with conditional sums

    Hi Martin, patphilly
    This is something i recently came across (in another forum)
    the "range" dimensions ($B$1:$B$5 in the example) actually determine the "sum_range "dimensions while the first cell reference of the "sum_range" ($A$1 in the example) determines the upperleft startingpoint of the "sum_range".
    e.g. if we write the formula as : =sumif($B$1:$B$5,"<="&B1,$A$1) or
    =sumif($B$1:$B$5,"<="&B1,$A$1:$A$5) or
    =sumif($B$1:$B$5,"<="&B1,$A$1:$A$2) the result should be the same.
    If i understood correctly.... big if!
    As for the results not updating if you change A2 to 310: this does work for me.
    Last edited by WHER; 01-23-2010 at 09:25 PM.

  7. #7
    Forum Expert martindwilson's Avatar
    Join Date
    06-23-2007
    Location
    London,England
    MS-Off Ver
    office 97 ,2007
    Posts
    19,320

    Re: Conditional sum

    but it doesnt recalculate???? unless you specify the whole range

  8. #8
    Valued Forum Contributor
    Join Date
    07-29-2009
    Location
    Belgium
    MS-Off Ver
    Excel 2003/Excel 2010
    Posts
    534

    Re: Conditional sum

    It does seem to recalculate correctly in my workbook. However to check, i think it's better to change the value in A3 instead of A2. Because the corresponding value in B2 (5) is the biggest in range B1:B5, the conditions implied in the formula leave the results in C1, C3, C4, C5 unchanged, as they should i think.

  9. #9
    Forum Expert martindwilson's Avatar
    Join Date
    06-23-2007
    Location
    London,England
    MS-Off Ver
    office 97 ,2007
    Posts
    19,320

    Re: Conditional sum

    hmm not in my workbook!
    Attached Files Attached Files

  10. #10
    Valued Forum Contributor
    Join Date
    07-29-2009
    Location
    Belgium
    MS-Off Ver
    Excel 2003/Excel 2010
    Posts
    534

    Re: Conditional sum

    When i change A2 (of Sheet1, Strange.xls) from 210 to 200, both C2 and D2 change from 1110 to 1100. The other cells/results in columns C and D remain the same, as they should.

  11. #11
    Registered User
    Join Date
    01-23-2010
    Location
    Switzerland
    MS-Off Ver
    Excel 2004
    Posts
    10

    Re: Conditional sum

    Well, that's interesting indeed. I have the same problem as Martin. When I change A2 from 210 to 200 in his file Strange.xls, ONLY D2 changes to 1100, while C2 stays at 1110.

    Also I am wondering if someone can briefly explain the importance of the & symbol in that case.

  12. #12
    Forum Expert martindwilson's Avatar
    Join Date
    06-23-2007
    Location
    London,England
    MS-Off Ver
    office 97 ,2007
    Posts
    19,320

    Re: Conditional sum

    <=B1 or =b1 or any variant thereof cant be used in countif it requires either cell ref OR a text string
    but if you use & to combine the text ref "<=" with the cell ref B1 then it will accept it

  13. #13
    Valued Forum Contributor
    Join Date
    07-29-2009
    Location
    Belgium
    MS-Off Ver
    Excel 2003/Excel 2010
    Posts
    534

    Re: Conditional sum

    Just to illustrate the weirdness of what's happening here: some snapshots of Martin's strange.xls workbook on my computer.
    Attached Images Attached Images

  14. #14
    Forum Guru DonkeyOte's Avatar
    Join Date
    10-22-2008
    Location
    Northumberland, UK
    MS-Off Ver
    O365
    Posts
    21,531

    Re: Conditional sum

    I don't have an answer to this but thought I would simply state that like WHER I can not replicate this issue...

    I've tested on both 2007 (Vista SP2) & 2002 (XP SP3) and in both cases calculation updates with change in A2 ... so even though the precedents are not directly referenced the formula still recalculates as expected.

    I note OP has Mac and thus I can't vouch for that version - I know note from a quick google that there have been issues with SUMIF sum_range & Mac on earlier versions (98) - but this would not account for the issues experienced by Martin ...

    Just to clarify regards environments, on which of your version(s) did you test Martin ?

    Bugs on specific versions are not unheard of... we came across one recently regards use of double unary coercion which when used in conjunction with Arrays/Sumproduct with Volatile precedents causes the auto calculation of said functions to fail on Volatile action... this bug is specific to XL2007 (resolved in 2010 it seems)

    Quote Originally Posted by WHER
    This is something i recently came across (in another forum)
    the "range" dimensions ($B$1:$B$5 in the example) actually determine the "sum_range "dimensions while the first cell reference of the "sum_range" ($A$1 in the example) determines the upperleft startingpoint of the "sum_range".
    e.g. if we write the formula as : =sumif($B$1:$B$5,"<="&B1,$A$1) or
    =sumif($B$1:$B$5,"<="&B1,$A$1:$A$5) or
    =sumif($B$1:$B$5,"<="&B1,$A$1:$A$2) the result should be the same.
    If i understood correctly.... big if!
    the above is (as you would expect) perfectly correct, however, irrespective of the above issue I'd not use the top left only approach simply for the sake of transparency (Formula Auditing / Tracing Precedents)

    I am intrigued and will keep digging... until such time as we're put out of our collective misery by another of course !

    D.O

    EDIT: - adding link to XL00 article: http://support.microsoft.com/kb/215732
    Last edited by DonkeyOte; 01-24-2010 at 07:15 PM.

  15. #15
    Valued Forum Contributor
    Join Date
    07-29-2009
    Location
    Belgium
    MS-Off Ver
    Excel 2003/Excel 2010
    Posts
    534

    Re: Conditional sum

    Of course i agree with DO that it's not the best/most transparent approach to use the "topleft only" reference, this was done to draw attention to this intriguing aspect of sum_range.

  16. #16
    Forum Guru DonkeyOte's Avatar
    Join Date
    10-22-2008
    Location
    Northumberland, UK
    MS-Off Ver
    O365
    Posts
    21,531

    Re: Conditional sum

    Follow up...

    Thought I'd look at this again given the experience outlined by Martin & OP would to me at least seem to be "expected" ... ie given the altered cell is not explicitly referenced as a precedent one would not expect the formula to recalculate.... unless it was Volatile.

    Using the most basic of tests ...

    If we simplify further and create a file:

    Please Login or Register  to view this content.
    and save the file, close, reopen & close we are not prompted to save.

    If we re-open and subsequently add

    Please Login or Register  to view this content.
    and save the file, close, reopen & close we are now prompted to save...

    The "save" prompt when no physical changes have been made by the user is seen as a very basic (potential) indicator of volatility... it's by no means watertight as proven by INDEX which does the same though not volatile (ie marks cell as "dirty" hence prompt)
    To do more thorough testing would require more sophisticated tools but thought I'd highlight above in case anyone else was curious... I will look again tomorrow in more depth.

    Given the link I added via Edit to my prior post just now it would "seem" that up to and incl. XL2000 if the altered cell sat outside of the referenced sum_range but was included by virtue of the criteria_range dimensions the SUMIF itself would not recalculate ... however post XL2000 the implication would be that such SUMIF constructs become Volatile which I was never aware of.

    Again - this is all still somewhat speculative... hopefully there will be a more definitive answer to this somewhere.


    (@ WHER - yep I know - wasn't implying you were advocating in preference to the "standard" )

  17. #17
    Forum Guru DonkeyOte's Avatar
    Join Date
    10-22-2008
    Location
    Northumberland, UK
    MS-Off Ver
    O365
    Posts
    21,531

    Re: Conditional sum

    Final note...

    I just used Charles Williams' VolatileFuncs.xls to test the Volatility.

    His file would backup the above assertion that any SUMIF where the sum_range dimension is smaller than the dimension of the "criteria" range IS indeed Volatile
    (at least in all XL versions that I have access to)

    so

    Please Login or Register  to view this content.
    Last edited by DonkeyOte; 01-24-2010 at 07:44 PM.

  18. #18
    Forum Expert martindwilson's Avatar
    Join Date
    06-23-2007
    Location
    London,England
    MS-Off Ver
    office 97 ,2007
    Posts
    19,320

    Re: Conditional sum

    i use 97 as a working thing at home, 2003 at work and 2007 for 2007 things at home as i have to spark up my virtual pc to use it. so looks like 97 may be the issue. thanks for looking in donkeyote.

  19. #19
    Forum Guru DonkeyOte's Avatar
    Join Date
    10-22-2008
    Location
    Northumberland, UK
    MS-Off Ver
    O365
    Posts
    21,531

    Re: Conditional sum

    Martin, FYI, both Colin Legg & Charles Williams have confirmed this in XL2002, XL2003, XL2007... I don't have 2010 to test myself.
    Romperstomper is going to investigate the Mac side of things for us and will hopefully post here in the not too distant future with his findings.

    Up to and incl. 2000 we know this lack of recalculation is "by design" and not a bug etc...

    Anyway, all in all, just another good reason to be sure you reference the sum_range as fully as possible

  20. #20
    Forum Guru DonkeyOte's Avatar
    Join Date
    10-22-2008
    Location
    Northumberland, UK
    MS-Off Ver
    O365
    Posts
    21,531

    Re: Conditional sum

    It's "official": http://www.decisionmodels.com/calcsecretsi.htm

    thumbs up to EF.com for this one I think... wouldn't say we discovered it but at least this undocumented feature is now "out there" ...

  21. #21
    Forum Guru romperstomper's Avatar
    Join Date
    11-04-2008
    Location
    A1
    MS-Off Ver
    Most
    Posts
    12,302

    Re: Conditional sum

    I've tested in 2004 and 2008 and neither one recalculates if you use the one-cell sum range and change a cell that 'ought' to be in the implicit sum range.
    Remember what the dormouse said
    Feed your head

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

Tags for this Thread

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