Monday 9 May 2011

SSRS - SUM problem in SSRS 2005 Report

In my SSRS 2005 report, I have one group in my table. In that group, I have a field that for each company record in my dataset, this field value is repeated for each record. This is a transactions table so you'd expect several records in the dataset for each company..that's not the problem.

example data from dataset:

TransID          CompanyID        Amount

1                                1000                       100
2                                1000                       100
3                                1000                       100
4                                2000                       400
5                                2000                       400


My SSRS 2005 Report has:

Group1 fields:

CompanyID                                            Amount

=Fields!CustomerNumber.Value              =Fields!CustAmount.Value

The output when previewed looks like this:


Company                Amount

1000                         100

2000                         400

Footer 209409730.83 (totals up all amounts! not just unique instances!)

Should be 500!!!

 I have a footer, and this is where the problem comes in. I am not able to sum 100 + 400 because if I do a SUM(=Fields!CustAmount.Value) It doesn't just sume up 100 + 400 but rather (100 + 100 + 100) + (400 + 400)

 SOLUTION!

Use list instead of table, then create a function in custom code to take in the reportItems!CustAmount.value (assumed textbox name is CustAmount) and do a manual sum up to a locally declared variable. Then in the show sum place just call the variable that stored the sum up value.
Below is the code to put in the Custom Code part in the SSRS report:

Public sum_of_cust As Integer
Public Function SumUp(ByVal Value As Integer)
sum_of_cust = sum_of_cust + Value
End Function
then add a textbox next to your CustAmount textbox that will have the following code:
=Code.SumUp(ReportItems!CustAmount.Value)
then to output the sum, use the following code:
=Code.sum_of_cust

No comments:

Post a Comment