Thursday, June 26, 2008

Referencing Totals in Reporting Services

I had the common task of creating a Reporting Services report that showed sales along with the percentages of sales. Like this:


The trick was the grand total figure was not available until the report footer. In the past, I have attacked this problem by using SQL statements that provided the grand total with each line of detail. Doing it that way is OK until you start needing additional percentages - like percentage by region, country, and state. Then the SQL becomes more tricky and processor intensive.

I read about a better solution today in a forum post at databasejournal.com - the author's name was not in the post. They suggested naming the textbox that contains the total, and then referencing it in the formula that calculates the percentage. Brilliant! I had assumed that the total figure wouldn't be available until after the report had been processed. This is not the case, aparently. There is a pre-processing step that occurs in Reporting Services. Cool, lucky me!

I names the total text box "GrandTotal". So - now the formulas for the percentage columns can look like this:

=(Fields!Sales.Value / ReportItems!GrandTotal.Value)

Sweet.