Calculate a Weighted Average

xtrareports-weighted-average

Follow the steps below to calculate a weighted average for Unit Price (weight values are taken from the Units in Stock field):

  1. Open an existing reporting application or create a new one from scratch.
  2. Bind a report to the sample Northwind database's Products table (the nwind.mdb file is included in the XtraReports installation).
  3. Use the Group and Sort Panel to group the report data by the CategoryName data field and construct a layout like the following:

    report-multiple-summaries-initial-layout

  4. Add the Group Footer band to the report and drop a XRLabel control on this band to display the summary result. Click the label's smart tag, then click the Summary field's ellipsis button.

    xtrareports-label-smarttag

  5. In the invoked Summary Editor window:

    • Set the Summary Running property to Group.
    • Set the Summary Function property to Weighed average.
    • Set the Argument Expression property to [UnitPrice], and the Weight property to [UnitsInStock]

    report-multiple-summaries-running-property

  6. You can also use the control's Format String property to format the summary value. For instance, set this property to Weighted Average Price: {0:c2}.