Monday, January 8, 2007

Compute Subtotals Using RunningValue Function

I was working on a report last week. The report looked simple, but I was stuck when I needed to sum a subtotal of some column for the first N records and another for the rest in each group after grouping all the records by some columns. It caused me trouble because I wanted to access the database only once and let SQL Server Reporting Services take care of the presentation.

I found the function, RunningValue(Expression, Function, Scope) handy and it solved my problem. For the subtotal of the First N records, the RunningValue function was called as follows,
= RunningValue(IIF( Fields!RowNum.Value <= N, Fields!SomeCol.Value , Nothing), SUM, "Group1")

Notes:

  1. One needs to have the row number ready before passing it to SQL Server Reporting Services, because RunningValue can't access the row numbers computed using RowNumber function.
  2. One has to use the 'Nothing' parameter in the IIF function. It can not be zero, or Reporting Services would throw an error at run time. That took me a while to find the solution here.
  3. The SQL Server Reporting Services forum is really helpful.