Help with SSRS code to get AVG from Total.

Posted on 2011-09-07
Last Modified: 2012-05-12

I have field that totals up a column on a report. The expression in this field is:


This field with the expresion is named TOTAL_COST_PER_UNIT_AREA1. I like to get an average of the subtotals in this field. So, I have this custom code (see code) that is in this field, and I call the custom code via the following:

=Code.CalculateAverage(AVG(Fields!TOTAL_COST_PER_UNIT_AREA.Value), InScope("TOTAL_COST_PER_UNIT_AREA"))

The problem is TOTAL_COST_PER_UNIT_AREA is not the field I want to average. I want to average what is in TOTAL_COST_PER_UNIT_AREA1, the subtotals. However, TOTAL_COST_PER_UNIT_AREA1 is not recongized as a field. What can I change to get the average of the subtotal?
Private m_total As Double
Private m_count As Integer

Public Function CalculateAverage(average As Double, inTotalCostAcre as Boolean) As Double

If Not inTotalCostAcre Then
   ' Average of TotalCostAcre
    Return average

  Else If inTotalCostAcre Then

   ' Subtotal of inTotalCostAcre
   m_total = m_total + subtotal
   m_count = m_count + 1
   Return subtotal

        ' Average of Subtotal
       Dim avg as Double
       avg = m_total / m_count

       m_count = 0
       m_total = 0
        Return avg
End If
End Function

Open in new window

Question by:gtrapp
1 Comment
LVL 17

Accepted Solution

dbaSQL earned 500 total points
ID: 36502013
>>> However, TOTAL_COST_PER_UNIT_AREA1 is not recongized as a field. What can I change to get the average of the subtotal?
I believe you just need a totals value.  Right-click on your group, Add Totals, this should allow you to put your Avg(TOTAL_COST_PER_UNIT_AREA1) in the cell.
Here's a more detailed write up on the same:

I think MAYBE for v2005 you'd need to add the subtotal column, use =IIF(Inscope("TOTAL_COST_PER_UNIT_AREA"),SUM(Fields!Amount.value),AVG(Fields!Amount.value))
For v2008, you could just modify the total column cell expression like this: =AVG(Fields!Amount.value)

I say 'maybe' because I cannot test it.  but, the totals value should get you there.


Featured Post

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Locating SQL Data Tools 2012 11 43
SSRS Graph - Stacked Bar 2 54
Ssrs disable the home lonk 1 79
SSRS Report 2 35
Introduction As you'll probably know, a data region in a SQL Server Reporting Services report can be linked to only one dataset.  This makes it troublesome when you need to display data from more than one dataset in the same data region.  SQL Serve…
Time Corrections for Reports Working with a report, we made some interesting discoveries about the time corrections/updates We are using the following Parameters: Starting Entered Date (Date) formatted as Data type: "Date/Time" Ending Entered …
This Micro Tutorial hows how you can integrate  Mac OSX to a Windows Active Directory Domain. Apple has made it easy to allow users to bind their macs to a windows domain with relative ease. The following video show how to bind OSX Mavericks to …
In a recent question ( here at Experts Exchange, a member asked how to add page numbers to a PDF file using Adobe Acrobat XI Pro. This short video Micro Tutorial sh…

770 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question