24

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?

Daniel Neal
  • 4,165
  • 2
  • 20
  • 34

4 Answers4

31

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))
flash
  • 388
  • 4
  • 15
praveen
  • 12,083
  • 1
  • 41
  • 49
2

I had similar problem, this worked for me:

=Sum(iif(Fields!date_break.Value = "0001-01-01",Fields!brkr_fee.Value, nothing))

zb

Zalek Bloom
  • 551
  • 6
  • 13
1

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.

Pedram
  • 6,256
  • 10
  • 65
  • 87
Venaikat
  • 197
  • 2
  • 5
  • 20
  • Thanks for your answer. Are you sure this will only sum the values for which this is true? Written this way round, it looks like the if will be performed first - and then return the sum of all the fields. – Daniel Neal Jun 14 '12 at 10:55
  • I tried and the result was the same as =SUM(Fields!kWp.Value) – HEDMON Aug 16 '19 at 07:17
0

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.

pacholik
  • 8,607
  • 9
  • 43
  • 55