You are currently offline, waiting for your internet to reconnect

ACC: How to Exclude Zero Values When You Calculate Averages

This article was previously published under Q109364
Retired KB Content Disclaimer
This article was written about products for which Microsoft no longer offers support. Therefore, this article is offered "as is" and will no longer be updated.
Moderate: Requires basic macro, coding, and interoperability skills.
SUMMARY
When you use the Avg() function in a report to average a set of values, thefunction uses records that contain zero values in the calculation. In somecases you may not want to include records with zero values in acalculation.

This article includes two examples of how to calculate an average for allthe non-zero values in a set by counting the number of non-zero values inthe set of values and then using that total with a running sumcalculation.

This article assumes that you are familiar with Visual Basic forApplications and with creating Microsoft Access applications using theprogramming tools provided with Microsoft Access. For more informationabout Visual Basic for Applications, please refer to your version of the"Building Applications with Microsoft Access" manual.

NOTE: Visual Basic for Applications is called Access Basic in MicrosoftAccess versions 1.x and 2.0. For more information about Access Basic,please refer to the "Introduction to Programming" manual in MicrosoftAccess version 1.x or the "Building Applications" manual in MicrosoftAccess version 2.0.
MORE INFORMATION

Example One

In this example, one text box displays the number of non-zero values, andthe other text box displays the average for the set:

  1. Open the sample database Northwind.mdb (or NWIND.MDB in version 1.x or 2.0).
  2. Use a Report Wizard to create a new Groups/Totals report based on the Order Details table. This report will calculate the average discount for each Product ID.
  3. Include the ProductID and Discount fields on the report. Group the report on the ProductID field, grouped as Normal.

    NOTE: In versions 1.x and 2.0, there is a space in the Product ID field name.
  4. Open the report in Design view.
  5. Add an unbound text box to the report's detail section. Position this text box to the left of the Discount text box. The new text box will display the count of non-zero values. Set the text box's ControlSource property to:
          =IIf([Discount]=0 or [Discount] is null,0,1)						
    This expression returns 0 if the value of the Discount field is equal to zero or Null; otherwise, it returns 1. The Avg() function automatically excludes nulls as well.
  6. Set the text box's RunningSum property to Over Group and its Name property to CountOfData.

    NOTE: In version 1.x, the Name property is called the ControlName property.
  7. Add another text box to the report's footer section. This text box will display the result of the average calculation.
  8. Set the ControlSource property of this text box to:
          =Sum([Discount])/[CountOfData]						
  9. Preview the report.
The left column displays a running count of non-zero (and non-null)Discounts and the group footer displays an average Discount based on therunning count.

Example Two

This example uses a user-defined function that is the functional inverse ofthe NullToZero() function in the Northwind sample database:

  1. Open the sample database Northwind.mdb (or NWIND.MDB in versions 1.x and 2.0).
  2. Create a module and type the following line in the Declarations section if it is not already there:
    Option Explicit
  3. Type the following procedure:
          Function ZeroToNull( MyValue As Variant) As Variant         If MyValue = 0 Or MyValue = Null Then            ZeroToNull = Null         Else            ZeroToNull = MyValue         End If      End Function						
  4. Repeat the steps in Example One, but use the following expression in place of the expression in step 5:
          =ZeroToNull([Discount])						
    You can also use the ZeroToNull() function in a query.
REFERENCES
For more information about the Avg() function, search the Help Index forAvg function.
nonzero pure
Properties

Article ID: 109364 - Last Review: 01/18/2007 23:41:21 - Revision: 2.3

  • Microsoft Access 1.0 Standard Edition
  • Microsoft Access 1.1 Standard Edition
  • Microsoft Access 2.0 Standard Edition
  • Microsoft Access 95 Standard Edition
  • Microsoft Access 97 Standard Edition
  • kbhowto kbprogramming kbusage KB109364
Feedback