Results 1 to 6 of 6
  1. #1
    jset818 is offline Advanced Beginner
    Windows 7 64bit Access 2010 64bit
    Join Date
    Sep 2014
    Posts
    38

    Average of 3 fields

    I have 3 fields, numeric, they're all working days. Some of the fields are blank. I'm currently using this formula, it's look fine to me on paper but when I run it, it only works when all 3 fields have value. I get no average if field 2 or 3 is blank. See screenshot.



    Field 1= Availability. Field 2= 2nd Sourcing Time. Field 3= 3rd Sourcing Time

    Average: IIf([Availability]="",Null,IIf([2nd Sourcing Time]="",Null,IIf([3rd Sourcing Time]="",Null,([Availability]+[2nd Sourcing Time]+[3rd Sourcing Time])/(IIf(IsNull([Availability]),0,1)+IIf(IsNull([2nd Sourcing Time]),0,1)+IIf(IsNull([3rd Sourcing Time]),0,1)))))

    Click image for larger version. 

Name:	average.JPG 
Views:	10 
Size:	52.9 KB 
ID:	18523

  2. #2
    June7's Avatar
    June7 is offline VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    53,770
    Use Nz(): (Nz(field1,0) + Nz(field2,0) + Nz(field3,0)) / ...

    Do you want the field to be counted if it is Null?
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

  3. #3
    NTC is offline VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    Nov 2009
    Posts
    2,392
    a blank will not perform math; it is not viewed as 0 which is why J7's advice is forcing in a 0 if null.

    beyond that point is the definition of 'average' for you; as to whether it should always divide by 3 or must dynamically decide what is to be the denominator value which is a bit trickier but do-able

  4. #4
    jset818 is offline Advanced Beginner
    Windows 7 64bit Access 2010 64bit
    Join Date
    Sep 2014
    Posts
    38
    Edit: I found this module from another thread on this forum. The original poster had the same issue as me. The module works perfectly however I wondering if there's a way to make a small change to it. I'm not very experienced in VBA modules. When all fields are blank, rather than leaving the Average 0, can we change it to null?

    Public Function Average_Of_Fields(ParamArray flds() As Variant) As Double
    Dim i As Byte, sumFields As Long, numFields As Byte

    For i = LBound(flds) To UBound(flds)
    If Len(flds(i) & vbNullString) > 0 Then
    sumFields = sumFields + flds(I)
    numFields = numFields + 1
    End If
    Next

    If numFields <> 0 Then
    Average_Of_Fields = sumFields / numFields
    End If
    End Function

    Click image for larger version. 

Name:	average.JPG 
Views:	7 
Size:	41.7 KB 
ID:	18528
    Last edited by jset818; 10-28-2014 at 10:15 AM.

  5. #5
    June7's Avatar
    June7 is offline VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    53,770
    I use functions found at http://support.microsoft.com/kb/209839
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

  6. #6
    NTC is offline VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    Nov 2009
    Posts
    2,392
    looping code is not what I would recommend; it won't scale in large record sets having to run loop code for each record.

    One can do it with nested IIF statements but is complicated to trouble shoot and so is often easier to spread things out in separate fields; make 3 new fields: F1, F2, F3 whereby
    F1: iif(IsNull([Availability]),0,1)
    repeat that idea for F2, F3 for 2nd & 3rd sourcing

    so now you will have side by side either 1s or 0s for F1-3 depending on whether on not there are values in those fields when you run the query

    then add a new field
    Denominator: F1+F2+F3

    and now you can calc your average.... in large record sets this will run a lot faster

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

Similar Threads

  1. Average of multiple fields
    By glmtd in forum Queries
    Replies: 3
    Last Post: 07-24-2012, 02:09 PM
  2. Calculating the Total of Average Fields
    By DDEB in forum Queries
    Replies: 1
    Last Post: 05-09-2012, 06:26 PM
  3. Count Unique Fields and then get Average
    By Kirsti in forum Queries
    Replies: 5
    Last Post: 02-19-2012, 07:25 PM
  4. Replies: 13
    Last Post: 05-28-2010, 11:57 AM
  5. Average of multiple fields on one row
    By Lockrin in forum Access
    Replies: 3
    Last Post: 05-18-2010, 11:14 AM

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