+ Reply to Thread
Results 1 to 13 of 13

Adding a condition to a formula to filter data

  1. #1
    Registered User
    Join Date
    04-09-2013
    Location
    Devon, England
    MS-Off Ver
    Excel 2010
    Posts
    25

    Unhappy Adding a condition to a formula to filter data

    I am using Excel 2007 at work. I want to add a condition to a formula that updates a cell in another worksheet (in the same workbook) if an entry is made within a specific month. So... I currently have the following formula;
    =(A4-E4)/365
    'A' = current system date, 'E' = input date of birth - This formula calculates current age and returns it to 'F'.
    'C' is date of record entry.

    =COUNTIF('Data input'!F$4:F$1003,">=16")-COUNTIF('Data input'!F$4:F$1003,">=18") - this formula splits the returned data on a separate worksheet into a range of ages. What I need to do is add a condition that only counts them if 1) the conditions in the COUNTIF formula are met AND the entry is on a specific date. I tried;
    =COUNTIF('Data input'!F$4:F$1003,">=16")-COUNTIF('Data input'!F$4:F$1003,">=18")-COUNTIF('Data input'!C$4:C$1003,">=01/04/2013")-COUNTIF('Data input'!C$4:C$1003,">=30/04/2013")

    I get a return of '0' but on inputting today's date, where it should change to '1', I get nothing...
    Any help appreciated

  2. #2
    Valued Forum Contributor Lemice's Avatar
    Join Date
    04-13-2013
    Location
    Somewhere.
    MS-Off Ver
    Excel 2016
    Posts
    696

    Re: Adding a condition to a formula to filter data

    Hello,

    If you are using Excel 2007, I suggest trying COUNTIFS. If you wish the formula to also work on Excel 2003, try SUMPRODUCT.
    If you can provide a sample file, it will be the best.
    (copy pasta from Ford)
    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

    Regards,
    Lem

  3. #3
    Registered User
    Join Date
    04-09-2013
    Location
    Devon, England
    MS-Off Ver
    Excel 2010
    Posts
    25

    Re: Adding a condition to a formula to filter data

    The relevant sheets are; Data input and Report Monitoring...

    Attachment 228380

  4. #4
    Valued Forum Contributor Lemice's Avatar
    Join Date
    04-13-2013
    Location
    Somewhere.
    MS-Off Ver
    Excel 2016
    Posts
    696

    Re: Adding a condition to a formula to filter data

    If you are using Excel 2007, you should be able to use this
    Please Login or Register  to view this content.
    On both Excel 2007 and Excel 2003, you can try this
    Please Login or Register  to view this content.
    Hope this help.

  5. #5
    Registered User
    Join Date
    04-09-2013
    Location
    Devon, England
    MS-Off Ver
    Excel 2010
    Posts
    25

    Re: Adding a condition to a formula to filter data

    Hi Lem
    The sumproduct is certainly returning a number in the cell. Does it automatically change from month to month or do I need to configure the formula for each of the cells where I need a different monthly answer...? What I was looking to do is report back on a monthly basis on the changing figures- 30 April would report all those who had registered in April, 30 May would report those in May and so on... I would also have a cumulative yearly total which would simply be the product of all those totals...
    Martin

  6. #6
    Registered User
    Join Date
    04-09-2013
    Location
    Devon, England
    MS-Off Ver
    Excel 2010
    Posts
    25

    Re: Adding a condition to a formula to filter data

    Doh! Is the '4' on the end of the formula a clue?

  7. #7
    Valued Forum Contributor Lemice's Avatar
    Join Date
    04-13-2013
    Location
    Somewhere.
    MS-Off Ver
    Excel 2016
    Posts
    696

    Re: Adding a condition to a formula to filter data

    I was actually lazy so I abused the function MONTH, the 4 at the end is actually the month you want to check.
    In the formula posted above, that 4 means 4th month, aka April.
    If you want to check everyone from first of May to 31th of May, you can change it to 5.
    The downside is that you won't be able to give criteria such as, from 15th of April to 15th of May.

  8. #8
    Registered User
    Join Date
    04-09-2013
    Location
    Devon, England
    MS-Off Ver
    Excel 2010
    Posts
    25

    Re: Adding a condition to a formula to filter data

    That works... one more question, if I may (which is related).
    On the age ranges, I have;
    16-17 (but under 18)=COUNTIF('Data input'!F$4:F$1003,">=16")-COUNTIF('Data input'!F$4:F$1003,">=17")
    18-21 (but under 22)=COUNTIF('Data input'!F$4:F$1003,">=18")-COUNTIF('Data input'!F$4:F$1003,">=21")
    22-24 (but under 25)=COUNTIF('Data input'!F$4:F$1003,">=22")-COUNTIF('Data input'!F$4:F$1003,">=24")
    25-49 (but under 50)=COUNTIF('Data input'!F$4:F$1003,">=25")-COUNTIF('Data input'!F$4:F$1003,">=49")
    50-64 (but under 65)=COUNTIF('Data input'!F$4:F$1003,">=50")-COUNTIF('Data input'!F$4:F$1003,">=64")
    65+(nominally under 100)=COUNTIF('Data input'!F$4:F$1003,">=65")-COUNTIF('Data input'!F$4:F$1003,">=100")
    Would this reflect the right data sets or would I need to encompass the ages differently e.g. 16-18, 18-22 and so on?
    Probably a dopey question...
    Martin

  9. #9
    Valued Forum Contributor Lemice's Avatar
    Join Date
    04-13-2013
    Location
    Somewhere.
    MS-Off Ver
    Excel 2016
    Posts
    696

    Re: Adding a condition to a formula to filter data

    for 16-17, it should be =COUNTIF('Data input'!F$4:F$1003,">=16")-COUNTIF('Data input'!F$4:F$1003,">=18") (You want all age from 17.00 to 17.99, they are under 18), and so on.
    Mathematically, they are correct, and they will give you the results you want.
    Last edited by Lemice; 04-16-2013 at 06:42 AM.

  10. #10
    Registered User
    Join Date
    04-09-2013
    Location
    Devon, England
    MS-Off Ver
    Excel 2010
    Posts
    25

    Re: Adding a condition to a formula to filter data

    Lem... You are a STAR!!!!

  11. #11
    Valued Forum Contributor Lemice's Avatar
    Join Date
    04-13-2013
    Location
    Somewhere.
    MS-Off Ver
    Excel 2016
    Posts
    696

    Re: Adding a condition to a formula to filter data

    Glad I can help.

  12. #12
    Registered User
    Join Date
    04-09-2013
    Location
    Devon, England
    MS-Off Ver
    Excel 2010
    Posts
    25

    Re: Adding a condition to a formula to filter data

    Uhhhh.... Lem?
    Sorry my friend but I tried to adapt the formula so that I could capture other numbers such as 'male/female/transgender/not disclosed' by changing it to;
    =SUMPRODUCT(--('Data input'!G4:G1003>="Male"),--('Data input'!G4:G1003<"Male"),--(MONTH('Data input'!C4:C1003)=4))
    I made a boo boo somewhere I think...
    Any suggestions?
    M

  13. #13
    Valued Forum Contributor Lemice's Avatar
    Join Date
    04-13-2013
    Location
    Somewhere.
    MS-Off Ver
    Excel 2016
    Posts
    696

    Re: Adding a condition to a formula to filter data

    Hello Martin,

    You can try this formula
    Please Login or Register  to view this content.
    When you use logic statement such as this = that, here > there, etc you get "True" or "False" as a result. And by adding "--" in front of them, and put them in a bracket, they will return "1" or "0" instead, making it easy to calculate.
    The idea is "If true, return 1, if not, return 0", and you will have 2 lists of "1" and "0".
    For example, {1,1,0,1,0,0} and {1,0,0,1,0,1}. What SUMPRODUCT will do afterward is multiply them together, and showing you {1,0,0,1,0,0}, then finally add them, showing you the result of 2.
    To make it clearer, what it did was "Check list with criteria 1", and you have {1,1,0,1,0,0}, meaning the 1st, 2nd and 4th value in that list fits the criteria. Similarly, we have list 2, {1,0,0,1,0,1}, meaning 1st, 4th and 6th value fits the criteria.
    So, can you guess what happen when you multiply them together? You have {1,0,0,1,0,0}, the 1st and 4th value fit both the criteria.
    In the end, it counts, and shows you there are 2 such value.
    What it does is nothing different from a series of SUMIF, SUM IF, COUNTIF, COUNTIFS.

    Hope this help

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

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