简体   繁体   中英

SSRS how to use If and format statement together for negative values

I am trying to display seconds as minutes. Sometimes the value I get is negative. This causes problems. I wanted to check if the value is negative so that I can assign a default value. But I can not get If and Format statement work together.

This is what I have done to display the values without converting into minutes:

=Fields!RateLossSeconds.Value

在此处输入图片说明

This is what I have done to convert the seconds into minutes:

=Format(DateAdd("s", Fields!RateLossSeconds.Value, "00:00"), "mm:ss")

在此处输入图片说明

This is the code I used to add a condition:

=IIF(Fields!RateLossSeconds.Value < 0, 0, Format(DateAdd("s", Fields!RateLossSeconds.Value, "00:00"), "mm:ss"))

As you can see the if statement does not work as desired.

在此处输入图片说明

The #Error is caused due to SSRS evaluate the whole IIF() expression before the IIF logic flow, so even if you are handling the negative values it checks both branches for errors.

Try using this expression:

=IIF(
  Fields!RateLossSeconds.Value < 0, 0,
  Format(
    DateAdd("s",IIF(Fields!RateLossSeconds.Value<0,0,Fields!RateLossSeconds.Value),"00:00"),
    "mm:ss"
  )
)

It checks if there is a negative value in the False branch of the first IIF(). In this case I replace the negative values by zero but you can use any value you want to.

Let me know if this helps.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM