Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Create Sum of calculated rows in Microsoft Reporting Services

This seems like it should be simple but I can't find anything yet. In Reporting Services I have a table with up to 6 rows that all have calculated values and dynamic visibility. I would like to sum these rows. Basically I have a number of invoice items and want to make a total. I can't change anything on the DB side since my stored procedures are used elsewhere in the system. Each row pulls data from a different dataset as well, so I can't do a sum of the dataset. Can I sum all the rows with a table footer? Similarly to totaling a number of rows in Excel? It seems very redundant to put my visibility expression from each row into my footer row to calculate the sum.

like image 407
kd7iwp Avatar asked Dec 08 '22 08:12

kd7iwp


1 Answers

A few ways you could achieve this:

1. Do the calculation in the SQL and sum that field, like so:

SELECT Quantity, Amount, Quantity * Amount As TotalAmount FROM MyTable

Then just use the TotalAmount field in your Detail row and sum it in the footer.

2. Create a second Dataset that calculates the total for you and use that in your footer instead of a sum:

=Sum(Fields!TotalAmount.Value, "MyTotalingDataset")

3. Do it using custom code. Right-click on the Layout space choose Properties and click on the Code tab. Put in the following code:

Public Dim TotalAmount As Double = 0

Public Function CalculateRowTotal(ThisValue As Double, ThatValue As Double) As Double
    TotalAmount = TotalAmount + (ThisValue * ThatValue)
    Return ThisValue * ThatValue
End Function

On the Detail band, make the column where you sum the field have this expression:

=Code.CalculateRowTotal(Fields!Quantity.Value, Fields!Amount.Value)

This will execute the code above and do your calculation plus calculate the total sum in the process.

The Footer band displays the total sum so the column has the expression:

=Code.TotalAmount

And you're done. Just be careful because you aren't guaranteed the order in which your code will execute and for some reports it will execute the footer first (for example, if you use the Sum of the rows in the Detail band) which would make the total zero as the Detail band calculations haven't happened yet, but for the general case this should work.

like image 68
Chris Latta Avatar answered Jun 06 '23 07:06

Chris Latta