Question

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?

Was it helpful?

Solution 4

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.

OTHER TIPS

You could handle the NULL value occurring using:

Iif(Fields!ADataField.Value Is Nothing, 0, Fields!ADataField.Value)

That way you can replace any Null values with something else (0 in my example above). To put this into the context of your expression, it should look something like this:

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

Try using this:

=SUM(IIF(ISNothing(Fields!ADataField.Value), 0, 
(IIF(IsNumeric(Fields!ADataField.Value), IIF(DatePart("q", Fields!CreatedOn.Value) = "2", Fields!ADataField.Value, 0), 0)
)))

The VB in expressions in SSRS does not do short circuit evaluation. That is, all three parameters for an IIF statement are evaluated and if any of them throws an exceptions (#Error) then the entire expression throws an error.)

So no matter what the value for ADataField is, this part will be evaluated:

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

This will happen even if the code has no way of returning the result such as when IsNumeric(Fields!ADataField.Value) is false.

So you are always going to be executing the RunningValue function, If there is ever non-numeric data in ADataField when the CreatedOn field is in Quarter 2, then this will all generate an error for every call.

Try this as an alternative:

=IIF(
    IsNumeric(Fields!ADataField.Value),
    RunningValue(
        IIF(
            DatePart("q", Fields!CreatedOn.Value) = "2"
              AND IsNumeric(Fields!ADataField.Value),
            Fields!ADataField.Value,
            0
        ),
    Sum,
    Nothing
    ),
    Sum(0)
)
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top