Carrying out a SUMIF like operation using SQL Server Report Builder
-
14-06-2021 - |
Question
I'm trying to produce a conditional sum in SQL Server Report Builder 3.0.
My expression looks like this:
=Sum(Iif(Fields!ProjectTypeID.Value=2,Fields!kWp.Value,0))
I'd hoped that this expression would produce a sum of the kWp of all projects of type 2.
Unfortunately, it is not to be. And I can't seem to work out why. It just returns a 0 result, even though I know that there are non-zero values in the kWp column, and the column does not contain nulls.
A colleague did manage to get a positive result by replacing the
Fields!kWp.Value
with
1 * Fields!kWp.Value
But we have no idea why this works, and therefore, can't really trust the answer.
How can I get this conditional sum to behave itself?
Solution
The data type of the column 'kWp' is Decimal so you need to either convert the default value to 0.00 or cast the column to double
SUM(iif(Fields!ProjectTypeID.Value = 2,cdbl(Fields!kWp.Value),0.00))
OTHER TIPS
To get the sum
of the kWp of all projects of type 2, the expression is as follows,
=IIf(Fields!ProjectTypeID.Value=2,sum(Fields!kWp.Value),0)
I hope this will help u.
I had similar problem, this worked for me:
=Sum(iif(Fields!date_break.Value = "0001-01-01",Fields!brkr_fee.Value, nothing))
zb
To get the conditional sum you can try this expression
=sum(IIf(Fields!balance.Value > 0,(Fields!balance.Value),0))
It sums only positive numbers otherwise it adds 0 to the total, you can do it wise versa.