+ Reply to Thread
Results 1 to 5 of 5

COUNTIFS using OR logic on multiple fields

Hybrid View

  1. #1
    Registered User
    Join Date
    02-08-2023
    Location
    Anytown, USA
    MS-Off Ver
    2010
    Posts
    13

    COUNTIFS using OR logic on multiple fields

    I'm trying to figure out how to do a COUNTIFS using OR logic and tables.
    Using the tables below, how do I count how many instances in Table1 are there where Table1[Field1] is found in Table2[Field1] OR Table1[Field2] > Table3[Field1]?
    Answer is 5. I don't want to double count where both criteria are met.
    Thanks.

    Table1
    [Field1] [Field2]
    aaa 1
    bbb 6
    aaa 1
    ccc 2
    aaa 5
    ddd 4

    Table 2
    [Field1]
    aaa
    bbb

    Table3
    [Field1]
    3

  2. #2
    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
    48,344

    Re: COUNTIFS using OR logic on multiple fields

    Fast answers need visual help. Please read the yellow banner at the top of this page on how to attach a file and a mocked up solution.
    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


  3. #3
    Forum Guru
    Join Date
    04-13-2005
    Location
    North America
    MS-Off Ver
    2002/XP, 2007, 2024
    Posts
    16,413

    Re: COUNTIFS using OR logic on multiple fields

    As one who likes helper columns, I added a column to Table1 that would perform the OR() logic, then used that column in a COUNTIFS() function counting where the OR() logic returns TRUE.

    0) I copied the text from the OP and pasted into Excel followed by Text to Columns using space as the delimiter to get the data into Excel. Thus Table1 is in A2:A8, Table2 is in A11:A13, Table3 is in A16:A17.
    1) Build the OR() function in column C of Table1.
    1a) COUNTIFS($A$11:$A$13,A3) will return 0 when the Field1 text does not exist in Table2 and 1 or more if it does exist. COUNTIFS(...)>0 will therefore return TRUE when Field1 is in Table2, and FALSE when it is not.
    1b) A simple boolean comparison will test if the value in Field2 is larger than the value in Table3 =B3>$A$17.
    1c) Combine into an OR() function in C3. =OR(COUNTIFS(...)>0,B3>$A$17). Note the mix of relative and absolute references, and copy into C3:C8.
    2) The final COUNTIFS() is now simply =COUNTIFS(C3:C8,TRUE)

    I know we despise helper columns, but in cases like this, I find that a simple helper column makes the entire task rather simple to program in the spreadsheet.

    Edit to add: I expect that the DCOUNT() function could do this without the helper column. It seems to me that, while the Dabs() functions have been around for a long time, no one has ever really come to understand how the criteria range works when you want complex logic. If one spent the time to figure out the right criteria range, I expect DCOUNT() could handle this task.
    Last edited by MrShorty; 05-11-2023 at 12:42 PM.
    Quote Originally Posted by shg
    Mathematics is the native language of the natural world. Just trying to become literate.

  4. #4
    Registered User
    Join Date
    02-08-2023
    Location
    Anytown, USA
    MS-Off Ver
    2010
    Posts
    13

    Re: COUNTIFS using OR logic on multiple fields

    Many thanks!

  5. #5
    Forum Guru
    Join Date
    04-13-2005
    Location
    North America
    MS-Off Ver
    2002/XP, 2007, 2024
    Posts
    16,413

    Re: COUNTIFS using OR logic on multiple fields

    I took a few minutes to explore DCOUNT() and found that, if I build a criteria range like:
    [Field1] -- [Field2]
    aaa -- blank
    bbb -- blank
    blank -- >3
    
    in A11:B14
    Then DCOUNT(A2:B8,,A11:B14) would return 5. A quick check using Advanced Filter to see which records this criteria range is identifying, and it appears to be finding the exact same records that my previous formula would find.

+ 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. Logic within Countif/Countifs
    By Shirtbeast in forum Excel General
    Replies: 2
    Last Post: 01-05-2023, 05:29 AM
  2. [SOLVED] COUNTIFS with 2 data fields and in between 2 specified dates
    By TaylorGC in forum Excel Formulas & Functions
    Replies: 1
    Last Post: 10-20-2020, 09:36 AM
  3. COUNTIFS for several criteria with the AND logic.
    By Folshot in forum Excel Formulas & Functions
    Replies: 6
    Last Post: 01-27-2020, 01:17 PM
  4. COUNTIFs: Multiple ranges, single criterion with OR logic
    By A.Khan in forum Excel Formulas & Functions
    Replies: 10
    Last Post: 04-20-2017, 06:05 AM
  5. [SOLVED] Using date fields as reference with COUNTIFS?
    By dubcap01 in forum Excel General
    Replies: 3
    Last Post: 10-20-2015, 05:28 AM
  6. [SOLVED] CountIFS using criteria in one cell OR another - OR logic?
    By trubertiam in forum Excel Formulas & Functions
    Replies: 9
    Last Post: 06-01-2014, 08:48 PM
  7. Help with COUNTIFS logic with monthly date criteria.
    By cbreeze in forum Excel General
    Replies: 2
    Last Post: 02-16-2012, 12:16 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