Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SSRS Conditional Summing

Tags:

I have an SSRS report that displays several pages of rows. In each row is a "TYPE" field. In that TYPE field there is either an "M" for the value or a "P" for the value. At the end of the report I want to summ up all the price values for the "P" TYPES. I tried this but it prioduced an #Error:

=Sum(iif(Fields!TYPE.Value = "P",Fields!EXT_QTY.Value * Fields!PRICE.Value ,0)) 

this summed all rows

=iif(Fields!PART_TYPE.Value = "P" ,  Sum(Fields!EXT_QTY.Value * Fields!PRICE.Value ),  0 ) 

I'm sure this is do-able. Any ideas? Thanks

like image 349
MikeTWebb Avatar asked Jan 18 '11 17:01

MikeTWebb


People also ask

How do I use IIF in SSRS expression?

Value = “CA”, “Bold”, “Italic”) SSRS iif statement The format of the IIF() statement is as follows: =IIF(Expression, Condition set when the expression is true, Condition set when the expression is false) It should be a Boolean expression, according to parameter 1.

How do I sum in SSRS report?

In the tablix data region row group area, right-click a cell in the column group area for which you want totals, then point to Add Total, and click Before or After. A new column outside the current group is added to the data region, and then a default total is added for each numeric field in the column.


2 Answers

Found the answer....

=SUM(IIF(Fields!PART_TYPE.Value ="P",CDbl(Fields!EXT_QTY.Value * Fields!PRICE.Value), CDbl(0.0))) 
like image 199
MikeTWebb Avatar answered Dec 06 '22 16:12

MikeTWebb


The SUM fails due to type comparison - you can't Sum values of different types, being the expression (probably a Double) with 0, an Integer. MikeTWebb's answer does explicit type conversion to get around this error. This is fine for this specific example, being a Sum, however this doesn't produce an accurate result if you want an average (being Sum / Count) of the values where the Type is P. That is because 0 is a value and would be included in the averaging calculation when you actually want those values excluded from the calculation.

Another option is to use Nothing instead of 0:

=Sum(IIF(Fields!TYPE.Value = "P", Fields!EXT_QTY.Value * Fields!PRICE.Value, Nothing)) 

This solves the type comparison error without needing explicit typecasting and is a better solution when you are using aggregations where whether the value exists or not is significant to the result, like Average.

like image 26
Chris Latta Avatar answered Dec 06 '22 15:12

Chris Latta