+ Reply to Thread
Results 1 to 13 of 13

How to subtotal only if certain conditions apply

  1. #1
    Registered User
    Join Date
    10-30-2014
    Location
    Riga
    MS-Off Ver
    Excel 2010
    Posts
    11

    How to subtotal only if certain conditions apply

    Hi,

    I found similar questions online but none of the answers worked for me.

    I need in BD to make an average of the values in color BB7:BB10000, but only if 2 conditions are met (in this case, if the "route" in AP is the same, and if the "week" in AT is the same).

    Normally, I would do like this (in row 7):

    =AVERAGEIFS($BB$7:$BB$10000;$AP$7:$AP$10000;AP7;$AT$7:$AT$10000;AT7) and drag the formula all the way down.

    Of course Average Ifs would work perfectly on a static array of numbers, but I need the calculation to adapt to the filtering, so I need to use subtotal somehow.

    I found this formula but I wasn't able to make it work, probably because I do not know how to apply it properly

    {=AVERAGE(IF(SUBTOTAL(2,OFFSET(B2,ROW(B2:B100)-ROW(B2),0)),IF(A2:A100="x",B2:B100)))}

    Any suggestions?

    Thank you

    Giovanni
    Attached Images Attached Images

  2. #2
    Forum Guru Kaper's Avatar
    Join Date
    12-14-2013
    Location
    Warsaw, Poland
    MS-Off Ver
    most often: Office 365 in Windows environment
    Posts
    8,650

    Re: How to subtotal only if certain conditions apply

    Hi Giovanni,

    Try to start your array formula with:
    Formula: copy to clipboard
    Please Login or Register  to view this content.


    Anyway ... it woulkd be easier to answer, if you attach a sample workbook. Make sure there is just enough data to demonstrate your need. Include a BEFORE sheet and an AFTER sheet in the workbook if needed to show the process you're trying to complete or automate. Make sure your desired results are shown, mock them up manually if necessary.

    Remember to desensitize the data.

    Click on GO ADVANCED and use the paperclip icon to open the upload window.View Pic
    Last edited by Kaper; 07-13-2015 at 05:41 AM.
    Best Regards,

    Kaper

  3. #3
    Registered User
    Join Date
    10-30-2014
    Location
    Riga
    MS-Off Ver
    Excel 2010
    Posts
    11

    Re: How to subtotal only if certain conditions apply

    Hi Kaper,

    so, I rephrase it:

    in F2:F15 I have this pretty straightforward formula:

    =AVERAGEIFS($E$2:$E$15;$A$2:$A$15;C4;$B$2:$B$15;D4)

    It calculates the average of all the values in col E on certain conditions (for all those values for which column C matches column A, and for which column D matches column B)

    I want this formula to change its output based on how I filter the data, as it happens when I use subtotal.

    As you can see in the example, if I apply a filter, the average doesn't make sense anymore, as it still applies to the old array of numbers.

    On the other hand, if I would use subtotal I would not be allowed to set any condition.

    How can I do it?

    Thank you!

    Giovanni
    Attached Files Attached Files

  4. #4
    Forum Guru Kaper's Avatar
    Join Date
    12-14-2013
    Location
    Warsaw, Poland
    MS-Off Ver
    most often: Office 365 in Windows environment
    Posts
    8,650

    Re: How to subtotal only if certain conditions apply

    I'd add supplementary column with
    Formula: copy to clipboard
    Please Login or Register  to view this content.

    (see G2 in attachment)
    and then main forrmula (F2 and down):
    Formula: copy to clipboard
    Please Login or Register  to view this content.


    At the moment I see no simple way to achieve this without this helper column.

    Note that F9 result is #DIV/0! error (not 2 as you suggested in H19), because after filtering in column E, there is no visible data, where columns A and B are 2 and 8 resp.

    See attachment
    Attached Files Attached Files

  5. #5
    Registered User
    Join Date
    10-30-2014
    Location
    Riga
    MS-Off Ver
    Excel 2010
    Posts
    11

    Re: How to subtotal only if certain conditions apply

    Thank you, I managed to solve it myself

    =SUMPRODUCT(SUBTOTAL(3;OFFSET($BC$7:$BC$8902;ROW($BC$7:$BC$8902)-ROW($BC$7);0;1));--($BC$7:$BC$8902=BC7);($BB$7:$BB$8902)) / SUMPRODUCT(SUBTOTAL(3;OFFSET($BC$7:$BC$8902;ROW($BC$7:$BC$8902)-ROW($BC$7);0;1));--($BC$7:$BC$8902=BC7))

    where in BC I have the data to compare and in BB I have the data to sum up (first part of the formula) and to count (second part of the formula). Dividing the two I get to the average I wanted.

    This formula sums up all data in BB that have the same value in BC, then it counts them so that dividing the two I get to the average. And it uses subtotal, so it recalculates when I filter the database.

    Giovanni

  6. #6
    Forum Guru Kaper's Avatar
    Join Date
    12-14-2013
    Location
    Warsaw, Poland
    MS-Off Ver
    most often: Office 365 in Windows environment
    Posts
    8,650

    Re: How to subtotal only if certain conditions apply

    Going that path, you could skip third argument in first sumproduct by using subtotal(9->"sum" instead of subtotal(3->"count":
    Formula: copy to clipboard
    Please Login or Register  to view this content.

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


    if (as in the opening post) there are two conditions (let's say second is in BD column) the approach to convert boolean values to 1/0 with multiplication could be especially usefull:

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

  7. #7
    Registered User
    Join Date
    10-30-2014
    Location
    Riga
    MS-Off Ver
    Excel 2010
    Posts
    11

    Re: How to subtotal only if certain conditions apply

    Thank you Kaper,

    good points! I will see which solution is faster, as this formula uses an awful lot of resources.

    Giovanni

  8. #8
    Forum Guru Jonmo1's Avatar
    Join Date
    03-08-2013
    Location
    Bryan, TX
    MS-Off Ver
    Excel 2010
    Posts
    9,763

    Re: How to subtotal only if certain conditions apply

    Quote Originally Posted by RunwayRiga View Post
    as this formula uses an awful lot of resources.
    All the more reason to go with the helper column solution suggested in Post #4
    Helper columns are not a bad thing, that's why they have the word 'Help' in their name.
    Excel gives you thousands of columns, might as well use a couple of them.


    Also, once you have 1 formula that you want to be dependant on visible cells only
    It's very likely you'll then have another, then another....
    Each additional formula you create using the array version is duplicating the work of the subtotal.

    If you use the helper column method, the subtotal bit is only done once, and each subsequent formula then refers to that column.

    MUCH more efficient.
    Last edited by Jonmo1; 07-15-2015 at 09:13 AM.

  9. #9
    Forum Expert GeneralDisarray's Avatar
    Join Date
    09-15-2011
    Location
    Pittsburgh, PA, USA
    MS-Off Ver
    Windows Excel 2016
    Posts
    1,416

    Re: How to subtotal only if certain conditions apply

    =SUMPRODUCT(SUBTOTAL(9;OFFSET($...

    Dumb question - how are you using semi colons to separate arguments of a function? Is that a regional setting?
    Remember, saying thanks only takes a second or two. Click the star icon(*) below the post you liked, to give some Rep if you think an answer deserves it.

    Please,mark your thread [SOLVED] if you received your answer.

  10. #10
    Forum Expert Tony Valko's Avatar
    Join Date
    12-31-2011
    Location
    Pittsburgh
    MS-Off Ver
    2002, 2007:2013
    Posts
    18,890

    Re: How to subtotal only if certain conditions apply

    Quote Originally Posted by GeneralDisarray View Post

    Dumb question - how are you using semi colons to separate arguments of a function? Is that a regional setting?
    Yes, some international locations use semi-colons as the argument separator.
    Biff
    Microsoft MVP Excel
    Keep It Simple Stupid

    Let's Go Pens. We Want The Cup.

  11. #11
    Registered User
    Join Date
    10-30-2014
    Location
    Riga
    MS-Off Ver
    Excel 2010
    Posts
    11

    Re: How to subtotal only if certain conditions apply

    Norway, to be precise.

  12. #12
    Registered User
    Join Date
    10-30-2014
    Location
    Riga
    MS-Off Ver
    Excel 2010
    Posts
    11

    Re: How to subtotal only if certain conditions apply

    Quote Originally Posted by Jonmo1 View Post
    All the more reason to go with the helper column solution suggested in Post #4
    Helper columns are not a bad thing, that's why they have the word 'Help' in their name.
    Excel gives you thousands of columns, might as well use a couple of them.


    Also, once you have 1 formula that you want to be dependant on visible cells only
    It's very likely you'll then have another, then another....
    Each additional formula you create using the array version is duplicating the work of the subtotal.

    If you use the helper column method, the subtotal bit is only done once, and each subsequent formula then refers to that column.

    MUCH more efficient.
    Yes, I tried it now (the solution with the dummy subtotal, by the very helpful Kaper) and it works great! Thanks

    I was not using it at the beginning because
    1 - I am stubborn
    2 - I hadn't fully understood it, but now I think I have:

    I think that, when counting a column with subtotal, in particular a column that is not visible, the result is 0, right? That is why, if you add in the conditions that the result of the subtotal(2;xx) must be 1, you immediately cut out all the rows that are not visible. I hope this is the explanation)

    Giovanni

  13. #13
    Forum Guru Jonmo1's Avatar
    Join Date
    03-08-2013
    Location
    Bryan, TX
    MS-Off Ver
    Excel 2010
    Posts
    9,763

    Re: How to subtotal only if certain conditions apply

    You're welcome.

    Quote Originally Posted by RunwayRiga View Post
    I think that, when counting a column with subtotal, in particular a column that is not visible, the result is 0, right? That is why, if you add in the conditions that the result of the subtotal(2;xx) must be 1, you immediately cut out all the rows that are not visible. I hope this is the explanation)
    Yes, that's exactly how it works, except it's for visible rows, not columns...

+ 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. Line when missing after apply Subtotal functions
    By szern9356 in forum Excel Formulas & Functions
    Replies: 1
    Last Post: 04-30-2013, 02:55 AM
  2. [SOLVED] Apply subtotal when I have a different range
    By Ashrak in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 01-25-2013, 10:16 AM
  3. Apply filter then subtotal
    By staples in forum Excel General
    Replies: 5
    Last Post: 10-13-2009, 01:54 PM
  4. How to apply conditions on charts
    By aman1234 in forum Excel Programming / VBA / Macros
    Replies: 11
    Last Post: 03-12-2009, 09:57 AM
  5. Subtotal Macro how can I apply #rounding?
    By Casey in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 04-04-2005, 02:08 PM

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