如何避免SSRS中的#Error和NaN

时间:2016-08-23 18:57:26

标签: vb.net tsql reporting-services

我在报告中有这个表达式来计算比率:

=
SUM(Fields!PaidLosses.Value) / 
Lookup(Fields!YearNum.Value & Fields!MonthNum.Value, Fields!YearStartRisk.Value & Fields!MonthNum.Value, Fields!EarnedPremium.Value, "EarnedAllCoverages")

当我选择数据不足的参数时,我得到了#Error和NaN。 NaN的结果是在“ - ”值上划分“ - ”。并且#Error的结果是在0值上划分“ - ”。 enter image description here

1 个答案:

答案 0 :(得分:1)

尝试:

=SUM(Fields!PaidLosses.Value) / 
IIF(
ISNOTHING(
  Lookup(Fields!YearNum.Value & Fields!MonthNum.Value,
  Fields!YearStartRisk.Value & Fields!MonthNum.Value,
  Fields!EarnedPremium.Value, "EarnedAllCoverages")
) OR 
Lookup(Fields!YearNum.Value & Fields!MonthNum.Value,
Fields!YearStartRisk.Value & Fields!MonthNum.Value,
Fields!EarnedPremium.Value, "EarnedAllCoverages") = 0,1,
Lookup(Fields!YearNum.Value & Fields!MonthNum.Value,
Fields!YearStartRisk.Value & Fields!MonthNum.Value,
Fields!EarnedPremium.Value, "EarnedAllCoverages")
)

这是因为你不能将任何数字除以零或nothing(SSRS空值)

此外,您可以使用this great answer中提供的此自定义代码来安全地执行分割。

Report菜单/ Report Properties... / Code标签

中的自定义代码textarea中添加以下代码
Public Function SafeDivide(ByVal Numerator As Decimal, ByVal Denominator As Decimal) As Decimal
    If Denominator = 0 Then
        Return 0
    End If
    Return (Numerator / Denominator)
End Function

然后在你的tablix中,只需用分子和分母作为参数调用函数:

=Code.SafeDivide(
  SUM(Fields!PaidLosses.Value),
  Lookup(
    Fields!YearNum.Value & Fields!MonthNum.Value,
    Fields!YearStartRisk.Value & Fields!MonthNum.Value,
    Fields!EarnedPremium.Value, "EarnedAllCoverages"
    )
  )

如果有帮助,请告诉我。