+ Reply to Thread
Results 1 to 14 of 14

automating sumproduct based on dynamic total (a sumif formula)

  1. #1
    Registered User
    Join Date
    08-16-2016
    Location
    singapore
    MS-Off Ver
    2010
    Posts
    81

    automating sumproduct based on dynamic total (a sumif formula)

    The objective is to automate sumproduct via multiple logic. Let me explain:

    Fruit .... Total ...... average price
    apple .... 17 ... $3.24 ....( =sumproduct(D10:D12,E10:E12)/17)
    pear ..... 14 .... $4.57

    *17 apple total is based on sumif formula


    (fruit) (Quantity ) Price
    apple... 10... $2.00
    apple ... 2 ... $ 5.00
    apple ... 5... $ 5.00
    pear ... 4 ... $ 3.50
    pear ... 10 ... $ 5.00

    Instead of manually typing in a sumproduct formula to find the weighted average price I would like to automate it.

    At present to find average price for apple I would add up quantity times price (10*$2)+(2*$5)+(5*$5) then
    divide this by sum of apple quantity (10+2+5). I do this via a sumproduct formula.

    I think this could be achieved by comparing the sum of apples at each row....(quantity 5 is less than 17(total) so then add 2....is 7 less than 17 yes...and so on...... to the total I have for apples (17).
    This is a dynamic total. So tomorrow the total could be 45 and the sumproduct formula would have to adjust.

    Any thoughts on automating this via a macro would be appreciated.
    Last edited by jim808; 08-16-2016 at 05:57 AM.

  2. #2
    Forum Expert
    Join Date
    09-20-2011
    Location
    England
    MS-Off Ver
    Excel 2010
    Posts
    2,278

    Re: automating sumproduct based on dynamic total (a sumif formula)

    Hello
    I don't understand why you're having to manually change the SUMPRODUCT formula. Could you not link the divisor '17' to the cell with the SUMIF formula.

    Screenshot.jpg

    C2 would have the formula:

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


    This will change as your values in the table change. The table could be made dynamic to accept new data.

    DBY

  3. #3
    Registered User
    Join Date
    08-16-2016
    Location
    singapore
    MS-Off Ver
    2010
    Posts
    81

    Re: automating sumproduct based on dynamic total (a sumif formula)

    Sorry it was simplified. I have an extra row for dates. So for example :


    1/2/2016 ....apple.....2....$4
    2/8/2016 ....apple.....2....$5
    2/8/2016 ....apple.....10...$6

    I only want to get sumproduct of the most recent price judging from the date column.
    sumif would give total 14 for apple but assuming the last two row for apple was latest date

    I would not want to include the row with date 1/2/2016 in the sumproduct formula.
    Last edited by jim808; 08-16-2016 at 09:46 AM.

  4. #4
    Forum Expert
    Join Date
    09-20-2011
    Location
    England
    MS-Off Ver
    Excel 2010
    Posts
    2,278

    Re: automating sumproduct based on dynamic total (a sumif formula)

    You can get the Max date of each fruit and do a calculation from that. For the Sumifs:

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


    and the SUMPRODUCT:

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


    You could also use a Pivot table by selecting the last date, see screenshot:

    Screenshot.jpg

  5. #5
    Registered User
    Join Date
    08-16-2016
    Location
    singapore
    MS-Off Ver
    2010
    Posts
    81

    Re: automating sumproduct based on dynamic total (a sumif formula)

    Thank you DBY. But I would still encounter problems with this approach . For example.

    Fruit .... Total ...... average price
    apple .... 14 ... ............???...


    *14 apple total is based on sumif formula


    (fruit) (Quantity ) Price
    1/8/2016.....apple....(-1).....1.3
    2/8/2016 ....apple.....5....$1.2
    2/8/2016 ....apple.....10....$1.4

    If an apple is sold its quantity is stated with a minus sign in front in this case -1.
    If total is positive number only positive quantity should be included in formula for sumproduct and vice versa.

    In this case divisor would be 14 but it should be 15 as sumproduct should only be performed on these two rows:

    2/8/2016 ....apple.....5....$1.2
    2/8/2016 ....apple.....10....$1.4

    Thank you for reading.
    Last edited by jim808; 08-17-2016 at 02:09 AM.

  6. #6
    Forum Expert
    Join Date
    09-20-2011
    Location
    England
    MS-Off Ver
    Excel 2010
    Posts
    2,278

    Re: automating sumproduct based on dynamic total (a sumif formula)

    You seem to be adding extra criteria with each post, so we're going round in circles here. It perhaps would be better if you could upload a sample workbook showing some data and various anticipated outcomes.

    DBY

  7. #7
    Registered User
    Join Date
    08-16-2016
    Location
    singapore
    MS-Off Ver
    2010
    Posts
    81

    Re: automating sumproduct based on dynamic total (a sumif formula)

    Hi sorry that would be all the criteria.

    I had omitted the detail previously as I thought it would make it too complex but as I saw
    you were able to solve, I provided the full example this time.

  8. #8
    Forum Expert
    Join Date
    09-20-2011
    Location
    England
    MS-Off Ver
    Excel 2010
    Posts
    2,278

    Re: automating sumproduct based on dynamic total (a sumif formula)

    Hi
    If you add another criteria to the SUMPRODUCT column G > 0. Do these return the correct values? For the 'Total':

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


    and the 'Average':

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


    These ranges are based on my previous screenshot attachment.

    DBY

  9. #9
    Registered User
    Join Date
    08-16-2016
    Location
    singapore
    MS-Off Ver
    2010
    Posts
    81

    Re: automating sumproduct based on dynamic total (a sumif formula)

    Quote Originally Posted by DBY View Post
    Hi
    If you add another criteria to the SUMPRODUCT column G > 0. Do these return the correct values? For the 'Total':

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


    and the 'Average':

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


    These ranges are based on my previous screenshot attachment.

    DBY
    Yes that worked perfectly .Amazing job. Many Thanks!

  10. #10
    Registered User
    Join Date
    08-16-2016
    Location
    singapore
    MS-Off Ver
    2010
    Posts
    81

    Re: automating sumproduct based on dynamic total (a sumif formula)

    Quote Originally Posted by DBY View Post
    Hi
    If you add another criteria to the SUMPRODUCT column G > 0. Do these return the correct values? For the 'Total':

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


    and the 'Average':

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


    These ranges are based on my previous screenshot attachment.

    DBY
    DBY could you please explain how the formula is working ?

    =SUMIFS($G$2:$G$6,$E$2:$E$6,MAX(INDEX(($F$2:$F$6=A2)*($E$2:$E$6),0)),$F$2:$F$6,A2,$G$2:$G$6,">0")

    I see that ($F$2:$F$6=A2) is referring to only look at apple(A2) in range F2:F6 but I do not understand how you are only getting
    the most recent dates with MAX(INDEX(($F$2:$F$6=A2)*($E$2:$E$6),0)) I see that ($E$2:$E$6 is referring to the range of dates.

    Could you please also breakdown this formula:

    SUMPRODUCT(($E$2:$E$6=MAX(INDEX(($F$2:$F$6=A10)*($E$2:$E$6),0)))*($F$2:$F$6=A10)*($G$2:$G$6>0)*($G$2:$G$6)*($H$2:$H$6))/B10

    Many Thanks

  11. #11
    Registered User
    Join Date
    08-16-2016
    Location
    singapore
    MS-Off Ver
    2010
    Posts
    81

    Re: automating sumproduct based on dynamic total (a sumif formula)

    Quote Originally Posted by DBY View Post
    Hi
    If you add another criteria to the SUMPRODUCT column G > 0. Do these return the correct values? For the 'Total':

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


    and the 'Average':

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


    These ranges are based on my previous screenshot attachment.

    DBY
    Hi, I just wondered can I also do this for : G < 0 ie if the sumifs total is a minus number then I only want to sumproduct of minus numbers, in G range to get weighted average price of the minus quantities
    how would I account for this in same formulas?



    Fruit .... Total ...... average price
    apple .... -14 ... ............???...


    *(-14) apple total is based on sumif formula


    date......(fruit)....(Quantity ).....Price
    1/8/2016.....apple....1.........$1.3
    2/8/2016 ....apple.....(-5)......$1.2
    2/8/2016 ....apple.....(-10)......$1.4

    something like this IF statement? I am not sure how to incorporate it though , please advise.

    IF($G$2:$G$6>0)*($G$2:$G$6)*($H$2:$H$6)), ($G$2:$G$6<0)*($G$2:$G$6)*($H$2:$H$6))

    Thanks
    Last edited by jim808; 08-18-2016 at 04:02 AM.

  12. #12
    Forum Expert
    Join Date
    09-20-2011
    Location
    England
    MS-Off Ver
    Excel 2010
    Posts
    2,278

    Re: automating sumproduct based on dynamic total (a sumif formula)

    The INDEX function brings back an array of values based on the range criteria. This is what it actually returns:

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


    The zeros are non matches of 'Apple' in this instance. So put that in to the MAX function and it returns the maximum date relating to 'Apples'. This becomes the match criteria in both the SUMIFS and SUMPRODUCT functions.

    With SUMPRODUCT the arguments with '=' return Boolean values True/False, which are coerced to 1's and 0's. As you can see from this table if all arguments are True then you get 1 which then multiplies the other arrays of values to give you a final sum.

    0 1 1 0
    1 1 1 1
    1 1 0 0

    Here's a link to a MS help page:

    https://support.office.com/en-gb/art...c-4d2145a2fd2e

    For the negative values try these. For the Total:

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


    and for the Average:

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


    I've named the ranges for ease of reference. Try these out to see if they return the values you'd expect. I hope all this makes some sense.

    DBY

  13. #13
    Registered User
    Join Date
    08-16-2016
    Location
    singapore
    MS-Off Ver
    2010
    Posts
    81

    Re: automating sumproduct based on dynamic total (a sumif formula)

    Thank you for your reply and information DBY.

    I just had one unforeseen problem I should have mentioned:

    Fruit .... Total ...... average price
    apple .... -14 ... ............???...


    *(-14) apple total is based on sumif formula


    date......(fruit)....(Quantity ).....Price
    1/8/2016.....apple....1.........$1.3
    2/8/2016 ....apple.....(-5)......$1.2
    10/8/2016 ....apple.....(-10)......$1.4

    as the max function would only take most recent date : 10/8/2016
    2/8/2016 ....apple.....(-5)......$1.2
    10/8/2016 ....apple.....(-10)......$1.4

    But I would want to include 2/8/2016 as this would yield a result closer to *(-14) apple total than only
    this row:

    10/8/2016 ....apple.....(-10)......$1.4

    I wonder how formula should adjust for this please?

  14. #14
    Forum Expert
    Join Date
    09-20-2011
    Location
    England
    MS-Off Ver
    Excel 2010
    Posts
    2,278

    Re: automating sumproduct based on dynamic total (a sumif formula)

    Working with the negative values, if you want to return all dates, then you can remove the references to dates from the formulas:

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


    and

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


    With this:

    2/8/2016 ....apple.....(-5)......$1.2
    10/8/2016 ....apple.....(-10)......$1.4

    Returns average price of $1.33 with a count of 15. Is this correct?

+ 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 or SUMPRODUCT formula to SUM totals based on multiple criteria
    By relmasri in forum Excel Formulas & Functions
    Replies: 13
    Last Post: 06-01-2016, 01:08 PM
  2. [SOLVED] How to total a range based on 3 criteria, using sumif
    By windrain in forum Excel Formulas & Functions
    Replies: 4
    Last Post: 05-04-2016, 11:19 PM
  3. [SOLVED] Formula to total a sum based on an item (Sumif?)
    By elfman66 in forum Excel Formulas & Functions
    Replies: 6
    Last Post: 10-14-2015, 02:39 PM
  4. [SOLVED] SUMPRODUCT(SUMIF(INDIRECT with Dynamic Worksheet Range
    By decipher in forum Excel Formulas & Functions
    Replies: 3
    Last Post: 10-07-2014, 05:40 AM
  5. sumif based on sumproduct
    By leem in forum Excel Formulas & Functions
    Replies: 3
    Last Post: 08-27-2014, 03:34 PM
  6. Replies: 10
    Last Post: 06-24-2014, 09:36 AM
  7. [SOLVED] sumproduct of total based on condition
    By aroyale15 in forum Excel Formulas & Functions
    Replies: 5
    Last Post: 09-30-2013, 12:20 AM

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