Search code examples
reporting-servicesssrs-2008dynamics-crm-2011ssrs-expression

How do I mix functions in complex SSRS expressions?


I'm writing a report against a data repository that has null values within some of the columns. The problem is building expressions is as temperamental as a hormonal old lady and doesn't like my mixing of functions.

Here's an expression I've written that does not work if the data in the field is null/nothing:

=IIF(
    IsNumeric(Fields!ADataField.Value),
    RunningValue(
        IIF(
            DatePart("q", Fields!CreatedOn.Value) = "2",
            Fields!ADataField.Value,
            0
        ),
    Sum,
    Nothing
    ),
    Sum(0)
)

(Pseudocode) "If the data is valid and if the data was created in the second quarter of the year, add it to the overall Sum, otherwise, add zero to the sum."

Looks pretty straight forward. And the individual pieces of the expression work by themselves. IE: IsNumeric(), DatePart(), etc. But when I put them all together, the expression throws an error.

I've attempted about every permutation of what's shown above, all to no avail. Null values in Fields!ADataField.Value cause errors.

The specific error generated:

"The Value expression for the textrun ‘’ uses a numeric aggregate function on data that is not numeric. Numeric aggregate functions (Sum, Avg, StDev, Var, StDevP, and VarP) can only aggregate numeric data."

Thoughts?


Solution

  • Ok everyone. I figured out the solution. Thanks much for all the great suggestions. Most of them I had already tried before I decided to post here. So the sanity check was of great value!

    For whatever illogical reason, what fixed this was wrapping the field in CInt() even after the field was confirmed already to have been numeric. It would not allow a Sum() to run against the field without it. Here's the code that finally took:

    =Sum(
        IIF(
            IsNumeric(Fields!ADataField.Value) And
            DatePart("q", Fields!createdon.Value) = "1",
            CInt(Fields!ADataField.Value),
            0
        )
    )
    

    Any and all other permutations that we've all tried here, including those with RunningValue() all work after having wrapped the "true" portion of IIF() with CInt().

    Just please don't axe me why this works and yet it doesn't without it. Anything but the most elementary expressions always seem to be extremely fickle.

    Thanks again everyone. Answered question goes to GShenanigan for going the extra mile during chat. Wish I could give it to everyone though. You guys are awesome.