Results 1 to 8 of 8
  1. #1
    Daezle is offline Novice
    Windows 7 32bit Access 2010 32bit
    Join Date
    Mar 2016
    Posts
    3

    Expression Builder and Count

    Hi All,



    I just joined the forum as I'm going to be using quite a lot of new queries over the next few months, but I've become a bit stuck so hopefully someone can point me in the right direction.

    I want to count each record in a row only if it is not null. So basically i want to end up with a calculated column that shows me what % of the target other columns are complete (not null).

    Unique Order Number Colour Paper Size Cost % Complete
    1 Red A4 75%
    2 Blue A3 2.75 100%
    A2 3.00 50%
    4 25%


    I've tried using the count function and still can't manage to get it right. This is the latest attempt to simply count the non-null values (in the expression builder):

    Expr1: Sum(IIf(IsNull([table1]![UniqueOrderNumber]),0,1)+IIf(IsNull([table1]![Colour]),0,1)+IIf(IsNull([table1]![PaperSize]),0,1)+IIf(IsNull([table1]![Cost]),0,1))

    Any help would be greatly appreciated.

    David
    Last edited by Daezle; 03-31-2016 at 07:11 AM.

  2. #2
    ranman256's Avatar
    ranman256 is offline VIP
    Windows Vista Access 2010 32bit
    Join Date
    Apr 2014
    Location
    Kentucky
    Posts
    9,525
    2 queries: 1 counts null rows, 1 counts not null.

  3. #3
    JoeM is offline VIP
    Windows 7 32bit Access 2007
    Join Date
    Jun 2012
    Posts
    3,904
    I've tried using the count function and still can't manage to get it right. This is the latest attempt to simply count the non-null values (in the expression builder):

    Expr1: Sum(IIf(IsNull([table1]![UniqueOrderNumber]),0,1)+IIf(IsNull([table1]![Colour]),0,1)+IIf(IsNull([table1]![PaperSize]),0,1)+IIf(IsNull([table1]![Cost]),0,1))
    Instead of adding those values inside your SUM statement, try multiplying them. Then, it will only return 1 if all are NOT null, but if any are null, it will return zero and won't be included in your count.


    EDIT: OP changed the wording of their original question, rendering this reply pretty much worthless, as it was based on the original wording.

  4. #4
    Daezle is offline Novice
    Windows 7 32bit Access 2010 32bit
    Join Date
    Mar 2016
    Posts
    3
    Quote Originally Posted by JoeM View Post
    Instead of adding those values inside your SUM statement, try multiplying them. Then, it will only return 1 if all are NOT null, but if any are null, it will return zero and won't be included in your count.
    Thanks for your response Joe, that does work for the intended purpose but not quite what i originally wanted. I've edited my post to correct the wording.

  5. #5
    JoeM is offline VIP
    Windows 7 32bit Access 2007
    Join Date
    Jun 2012
    Posts
    3,904
    Thanks for your response Joe, that does work for the intended purpose but not quite what i originally wanted. I've edited my post to correct the wording.
    In the future, it is probably best to just clarify it in a response, and not go back and change the original wording, as that pretty much can kill the flow of the thread and makes previous responses look out-of-place and confusing.

    If you simply want to know for each record the percent that is complete, just add up your IIF statements and divide by 4, i.e.
    Code:
    Expr1: (IIf(IsNull([table1]![UniqueOrderNumber]),0,1)+IIf(IsNull([table1]![Colour]),0,1)+IIf(IsNull([table1]![PaperSize]),0,1)+IIf(IsNull([table1]![Cost]),0,1))/4
    Note, you would NOT use SUM on this function. In Access, you only use SUM when aggregating multiple records, not multiple fields within a single record.

  6. #6
    Daezle is offline Novice
    Windows 7 32bit Access 2010 32bit
    Join Date
    Mar 2016
    Posts
    3
    Quote Originally Posted by JoeM View Post
    In the future, it is probably best to just clarify it in a response, and not go back and change the original wording, as that pretty much can kill the flow of the thread and makes previous responses look out-of-place and confusing.

    If you simply want to know for each record the percent that is complete, just add up your IIF statements and divide by 4, i.e.
    Code:
    Expr1: (IIf(IsNull([table1]![UniqueOrderNumber]),0,1)+IIf(IsNull([table1]![Colour]),0,1)+IIf(IsNull([table1]![PaperSize]),0,1)+IIf(IsNull([table1]![Cost]),0,1))/4
    Note, you would NOT use SUM on this function. In Access, you only use SUM when aggregating multiple records, not multiple fields within a single record.
    Thanks for clarifying. That is what i was looking for thank you very much JoeM.

  7. #7
    NoellaG's Avatar
    NoellaG is offline VIP
    Windows 7 Access 2010 (version 14.0)
    Join Date
    Jun 2010
    Location
    Belgium
    Posts
    1,044
    Hi,

    tip: instead the Iff(Isnull(.... construction, try the NZ function

  8. #8
    JoeM is offline VIP
    Windows 7 32bit Access 2007
    Join Date
    Jun 2012
    Posts
    3,904
    tip: instead the Iff(Isnull(.... construction, try the NZ function
    Since they need to return either a 1 or 0, I am not sure that helps in this scenario.
    NZ will return either the value or some other designated value if the value is Null.

Please reply to this thread with any new information or opinions.

Similar Threads

  1. expression builder
    By frustratedwithaccess in forum Access
    Replies: 7
    Last Post: 11-12-2014, 09:15 AM
  2. Expression Builder Help
    By Vibhor in forum Access
    Replies: 4
    Last Post: 12-11-2013, 11:42 AM
  3. Replies: 7
    Last Post: 06-28-2013, 12:15 PM
  4. Replies: 2
    Last Post: 11-20-2012, 03:21 AM
  5. Expression Builder
    By mistaken_myst in forum Access
    Replies: 2
    Last Post: 05-07-2008, 01:30 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Other Forums: Microsoft Office Forums