+ Reply to Thread
Results 1 to 25 of 25

Count if the sum of columns meet a condition

  1. #1
    Registered User
    Join Date
    10-22-2012
    Location
    CA
    MS-Off Ver
    Excel 2007
    Posts
    23

    Count if the sum of columns meet a condition

    Hi all,

    I need your help. I want to set up a formula (countif???) to count the number of customers for each year in row 12 (file attached).

    Some customers don't buy anything for a year and then come back, which should be included in the count for that year. For example, customer C, F and H should be included in the count for the years 2012, 2007, and 2012 & 2013 respectively.

    I'm thinking to include customer C in the count of 2012 if sum(K4:L4) >0, but I don't know how to set up the countif formula to do that.

    I know I could create a dummy worksheet and run a formula for each cell to place 1 on each cell if the sum of the subsequent cells in the row >0, and then sum all the cells (i.e. 1s) in each year/each column to have the number of customers. If there are so many customers, my file might get big with the dummy worksheet.

    So it'd be great to set up a formula in row 12, instead of the dummy worksheet.

    Please help.

    Thank you so much!
    Attached Files Attached Files

  2. #2
    Forum Guru MarvinP's Avatar
    Join Date
    07-23-2010
    Location
    Woodinville, WA
    MS-Off Ver
    Office 365
    Posts
    16,168

    Re: Count if the sum of columns meet a condition

    Hi Sunshine (in the morning makes me happy)

    See the attached for your formula.
    of =COUNTIF(B2:B11,">0")
    Attached Files Attached Files
    One test is worth a thousand opinions.
    Click the * Add Reputation below to say thanks.

  3. #3
    Forum Expert gmr4evr1's Avatar
    Join Date
    11-24-2014
    Location
    Texas
    MS-Off Ver
    Office 2010 and 2007
    Posts
    3,448

    Re: Count if the sum of columns meet a condition

    If you want to count the customers for each year, try this in cell B12 and copy/paste to the other cells in row 12
    Please Login or Register  to view this content.
    Dang it, Marvin the Martian beat me to it
    1N73LL1G3NC3 15 7H3 4B1L17Y 70 4D4P7 70 CH4NG3 - 573PH3N H4WK1NG
    You don't have to add Rep if I have helped you out (but it would be nice), but please mark the thread as SOLVED if your issue is resolved.

    Tom

  4. #4
    Forum Guru TMS's Avatar
    Join Date
    07-15-2010
    Location
    The Great City of Manchester, NW England ;-)
    MS-Off Ver
    MSO 2007,2010,365
    Posts
    44,457

    Re: Count if the sum of columns meet a condition

    Maybe:
    Formula: copy to clipboard
    Please Login or Register  to view this content.
    and drag across.


    Regards, TMS
    Trevor Shuttleworth - Retired Excel/VBA Consultant

    I dream of a better world where chickens can cross the road without having their motives questioned

    'Being unapologetic means never having to say you're sorry' John Cooper Clarke


  5. #5
    Administrator FDibbins's Avatar
    Join Date
    12-29-2011
    Location
    Duncansville, PA USA
    MS-Off Ver
    Excel 7/10/13/16/365 (PC ver 2310)
    Posts
    52,938

    Re: Count if the sum of columns meet a condition

    I am probably being a little slow here, but coud you provide a few sample answers (and why)?

    edit: told you I was being slow
    Last edited by FDibbins; 03-07-2015 at 06:39 PM.
    1. Use code tags for VBA. [code] Your Code [/code] (or use the # button)
    2. If your question is resolved, mark it SOLVED using the thread tools
    3. Click on the star if you think someone helped you

    Regards
    Ford

  6. #6
    Forum Guru MarvinP's Avatar
    Join Date
    07-23-2010
    Location
    Woodinville, WA
    MS-Off Ver
    Office 365
    Posts
    16,168

    Re: Count if the sum of columns meet a condition

    Hey Trevor,

    Can CountIf() ever return a negative number? I'm pondering the "<>0" in your answer above...

  7. #7
    Registered User
    Join Date
    10-22-2012
    Location
    CA
    MS-Off Ver
    Excel 2007
    Posts
    23

    Re: Count if the sum of columns meet a condition

    Thank you everyone, I tried that formula before posting the question here and the answers weren't correct. For example, column E count should be 7, because even Customer F didn't buy anything in column E (2007), but it's a customer. It came back in 2008 and bought something.

    Thanks,

  8. #8
    Forum Guru TMS's Avatar
    Join Date
    07-15-2010
    Location
    The Great City of Manchester, NW England ;-)
    MS-Off Ver
    MSO 2007,2010,365
    Posts
    44,457

    Re: Count if the sum of columns meet a condition

    I guess, in this specific example, it's not likely to But, maybe they refunded some money or paid compensation. But, I was just putting in the belt and braces.

    As a generality: sure.

    And no reason why "not equal to zero" is any better or worse than "greater than zero" in general terms. At least, I don't think so. Or maybe it forces Excel to do two comparisons, in which case maybe it's over-engineered. But, for 11 columns, I don't think it's a big deal. Open debate ensues ...

  9. #9
    Forum Guru TMS's Avatar
    Join Date
    07-15-2010
    Location
    The Great City of Manchester, NW England ;-)
    MS-Off Ver
    MSO 2007,2010,365
    Posts
    44,457

    Re: Count if the sum of columns meet a condition

    For example, column E count should be 7, because even Customer F didn't buy anything in column E (2007), but it's a customer. It came back in 2008 and bought something.
    In that case, the answer is 10 in every column because they were all customers in 2004, even if they didn't buy anything in later years

  10. #10
    Forum Guru TMS's Avatar
    Join Date
    07-15-2010
    Location
    The Great City of Manchester, NW England ;-)
    MS-Off Ver
    MSO 2007,2010,365
    Posts
    44,457

    Re: Count if the sum of columns meet a condition

    Is Customer A a customer in 2014 or not. He was every year up until 2013 but not in 2014, and we don't have any indication that he will be in 2015. What if a customer doesn't buy anything for two or three years? When is a customer not a customer.

  11. #11
    Registered User
    Join Date
    10-22-2012
    Location
    CA
    MS-Off Ver
    Excel 2007
    Posts
    23

    Re: Count if the sum of columns meet a condition

    As I don't know if a customer will come back in 2015 or later years, I'll just have to live with whatever numbers the formula give. Generally, I'll consider a company still a customer if I still see their purchases up to 2014. If a 2004 customer made purchases every year until 2013, and nothing in 2014, it's NOT included in the count for 2014. I might count that company if it come back and buy something in 2016, but NOT for now, as I don't see the 2016 data. If a customer made purchases from 2004 to 2007, then 2 years of nothing, then bought something every year from 2010 to 2014, I'd like to include it to the counts from 2004 to 2014.
    Thank you for the questions.

  12. #12
    Forum Guru TMS's Avatar
    Join Date
    07-15-2010
    Location
    The Great City of Manchester, NW England ;-)
    MS-Off Ver
    MSO 2007,2010,365
    Posts
    44,457

    Re: Count if the sum of columns meet a condition

    Sorry, no idea how to make that logic work ... I'm out.

    Good luck with someone else providing a solution that caters for the gaps in customer loyalty/continuity

  13. #13
    Administrator FDibbins's Avatar
    Join Date
    12-29-2011
    Location
    Duncansville, PA USA
    MS-Off Ver
    Excel 7/10/13/16/365 (PC ver 2310)
    Posts
    52,938

    Re: Count if the sum of columns meet a condition

    I am strugling with that too

    Im still waiting to see your WB updated with some sample answers (along with why)

  14. #14
    Forum Expert gmr4evr1's Avatar
    Join Date
    11-24-2014
    Location
    Texas
    MS-Off Ver
    Office 2010 and 2007
    Posts
    3,448

    Re: Count if the sum of columns meet a condition

    Ok, it sucks that a few are having a problem with this, but, it's nice to know that I am not the only one!

  15. #15
    Forum Expert daddylonglegs's Avatar
    Join Date
    01-14-2006
    Location
    England
    MS-Off Ver
    2016
    Posts
    14,675

    Re: Count if the sum of columns meet a condition

    Quote Originally Posted by my_sunshine View Post
    Some customers don't buy anything for a year and then come back, which should be included in the count for that year. For example, customer C, F and H should be included in the count for the years 2012, 2007, and 2012 & 2013 respectively.
    So the logic is to count all customers who had sales in that particular year.....but also any customers who have made purchases in both past and future years.

    Try this formula in B12 copied across

    =SUMPRODUCT(((B2:B11>0)+(COUNTIF(OFFSET($A2:A2,ROW(A2:A11)-ROW(A2),0),">0")*COUNTIF(OFFSET(C2:$Z2,ROW(B2:B11)-ROW(B2),0),">0")>0)>0)+0)

    That will cater for future expansion up to column Z
    Last edited by daddylonglegs; 03-07-2015 at 09:00 PM.
    Audere est facere

  16. #16
    Forum Guru TMS's Avatar
    Join Date
    07-15-2010
    Location
    The Great City of Manchester, NW England ;-)
    MS-Off Ver
    MSO 2007,2010,365
    Posts
    44,457

    Re: Count if the sum of columns meet a condition

    @DLL: that's brilliant ... but I would have thought 2012 would equal 4 not 5 although I think you're right as customers seem to be counted way into the future.

    Very clever. No idea how you come up with something like this.

    Regards, TMS

  17. #17
    Forum Guru TMS's Avatar
    Join Date
    07-15-2010
    Location
    The Great City of Manchester, NW England ;-)
    MS-Off Ver
    MSO 2007,2010,365
    Posts
    44,457

    Re: Count if the sum of columns meet a condition

    If every customer bought something in 2015, that would set the count to 10 for every year right back to the beginning. Is that the desired outcome?

  18. #18
    Administrator FDibbins's Avatar
    Join Date
    12-29-2011
    Location
    Duncansville, PA USA
    MS-Off Ver
    Excel 7/10/13/16/365 (PC ver 2310)
    Posts
    52,938

    Re: Count if the sum of columns meet a condition

    I guess we need to wait to see a reply and (preferably) a WB with some sample answers in it

  19. #19
    Forum Guru MarvinP's Avatar
    Join Date
    07-23-2010
    Location
    Woodinville, WA
    MS-Off Ver
    Office 365
    Posts
    16,168

    Re: Count if the sum of columns meet a condition

    OK then,

    Here is another answer that is a CSE (Control+Shift+Enter) array answer.

    In B12 put

    = SUMPRODUCT(((B2:B11=0))*($L2:$L11>0))+COUNTIF(B2:B11,">0")

    and then confirm it with a CSE.

  20. #20
    Registered User
    Join Date
    10-22-2012
    Location
    CA
    MS-Off Ver
    Excel 2007
    Posts
    23

    Re: Count if the sum of columns meet a condition

    Quote Originally Posted by daddylonglegs View Post
    So the logic is to count all customers who had sales in that particular year.....but also any customers who have made purchases in both past and future years.

    Try this formula in B12 copied across

    =SUMPRODUCT(((B2:B11>0)+(COUNTIF(OFFSET($A2:A2,ROW(A2:A11)-ROW(A2),0),">0")*COUNTIF(OFFSET(C2:$Z2,ROW(B2:B11)-ROW(B2),0),">0")>0)>0)+0)

    That will cater for future expansion up to column Z
    Awesome...thank you so much!!!

  21. #21
    Registered User
    Join Date
    10-22-2012
    Location
    CA
    MS-Off Ver
    Excel 2007
    Posts
    23

    Re: Count if the sum of columns meet a condition

    Quote Originally Posted by MarvinP View Post
    OK then,

    Here is another answer that is a CSE (Control+Shift+Enter) array answer.

    In B12 put

    = SUMPRODUCT(((B2:B11=0))*($L2:$L11>0))+COUNTIF(B2:B11,">0")

    and then confirm it with a CSE.
    This works too....You're my SUNSHINE...thanks a lot!!!!

  22. #22
    Registered User
    Join Date
    10-22-2012
    Location
    CA
    MS-Off Ver
    Excel 2007
    Posts
    23

    Re: Count if the sum of columns meet a condition

    "If every customer bought something in 2015, that would set the count to 10 for every year right back to the beginning. Is that the desired outcome?"

    Yes, the count should be 10 for every year. Thanks!

  23. #23
    Forum Guru TMS's Avatar
    Join Date
    07-15-2010
    Location
    The Great City of Manchester, NW England ;-)
    MS-Off Ver
    MSO 2007,2010,365
    Posts
    44,457

    Re: Count if the sum of columns meet a condition

    You're welcome. Thanks for the rep.

  24. #24
    Forum Expert daddylonglegs's Avatar
    Join Date
    01-14-2006
    Location
    England
    MS-Off Ver
    2016
    Posts
    14,675

    Re: Count if the sum of columns meet a condition

    Quote Originally Posted by MarvinP View Post
    = SUMPRODUCT(((B2:B11=0))*($L2:$L11>0))+COUNTIF(B2:B11,">0")
    Hello Marvin,

    You don't require CSE for this formula

    Note that although the results for this formula and my suggestion are the same for the sample given they might not always be if the data changes. For example your formula will count a customer who only bought something in 2014 as a customer in 2007. Mine will only count customers with both previous and future year purchases (as well as the ones in the current year)

  25. #25
    Registered User
    Join Date
    10-22-2012
    Location
    CA
    MS-Off Ver
    Excel 2007
    Posts
    23

    Re: Count if the sum of columns meet a condition

    Quote Originally Posted by daddylonglegs View Post
    Hello Marvin,

    You don't require CSE for this formula

    Note that although the results for this formula and my suggestion are the same for the sample given they might not always be if the data changes. For example your formula will count a customer who only bought something in 2014 as a customer in 2007. Mine will only count customers with both previous and future year purchases (as well as the ones in the current year)
    Thank you for pointing that out. In fact, I put some dummy data for 2015 and the result from your formula for column 2007 is more correct than that from Marvin's. Thanks a lot for your formula!
    Attached Files Attached Files

+ 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. A formula to count subtotals if they meet a qualifying condition
    By JC_LA_1979 in forum Excel Formulas & Functions
    Replies: 4
    Last Post: 01-16-2015, 01:03 PM
  2. Count unique values within a range that meet a specific condition
    By CSS in forum Excel Formulas & Functions
    Replies: 13
    Last Post: 06-24-2013, 02:00 PM
  3. [SOLVED] count number of cells which meet a particular condition for an individual
    By rohit43 in forum Excel Formulas & Functions
    Replies: 2
    Last Post: 04-18-2013, 11:20 AM
  4. Count text strings that meet a condition
    By dcgrove in forum Excel General
    Replies: 4
    Last Post: 09-14-2010, 03:38 PM
  5. count rows that meet two criteria in two different columns?
    By dsk3808 in forum Excel Formulas & Functions
    Replies: 6
    Last Post: 06-28-2006, 04:20 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