Im a bit stumped at the moment. I've created an expression that calculates the working days between 2 dates which works fine.
=(DateDiff(DateInterval.day,Fields!TCY_START_DATE.Value,
Fields!TCY_TENANCY_VISIT_DATE.Value))
- (DateDiff(DateInterval.WeekOfYear,Fields!TCY_START_DATE.Value,
Fields!TCY_TENANCY_VISIT_DATE.Value)*2)
- IIF(Weekday(Fields!TCY_START_DATE.Value,1) = 1,1,0)
- IIF(Weekday(Fields!TCY_START_DATE.Value,1) = 7,1,0)
- IIF(Weekday(Fields!TCY_TENANCY_VISIT_DATE.Value,1) = 1,1,0)
- IIF(Weekday(Fields!TCY_TENANCY_VISIT_DATE.Value,1) = 7,1,0)
The issue I have is that if the TCY_TENANCY_VISIT_DATE is blank then the results of this expression is #ERROR.
I need to tweak this expression so it will basically say if the visit date is blank then display nothing but still incorporate it into my working days formula
This is because SSRS evaluates the entire expression before it tries to find the actual path for the values it has. Therefore it will try and find the Weekday of Fields!TCY_TENANCY_VISIT_DATE.Value, even if it is blank, and will therefore throw an error.
The solution to this is to handle all your potential blank or empty values from the beginning. In this instance changing the line
IIF(Weekday(Fields!TCY_TENANCY_VISIT_DATE.Value,1) = 7,1,0)
to
iif(iif(isnothing(Fields!TCY_TENANCY_VISIT_DATE.Value),
"",
Fields!TCY_TENANCY_VISIT_DATE.Value), 1) = 7,
1,
0)
should do the trick. You may also need to repeat this process for your other values if there is a chance they could be null.
Related
Im trying to find the days gap between two dates using DateDiff function.
I have 2 datasets defined. If companycode is 'AB' then from one dataset else from another dataset I retrieve data.
Here is my expression. When I change to preview mode, it shows redmark to the first First(Fields!PeriodFrom.Value line. Why? (after generating report that field shows #Error
What Im doing wrong here?
=IIF(Parameters!CompanyCode.Value="AB",
DateDiff("d",First(Fields!PeriodFrom.Value, "ABReportData"), First(Fields!PeriodTo.Value, "ABReportData")),
DateDiff("d",First(Fields!PeriodFrom.Value, "XYReportData"), First(Fields!PeriodTo.Value, "XYReportData")))
I think there are two possible scenarios. First one is the expression
=First(Fields!PeriodFrom.Value, "ABReportData")
doesnt return a value. Add a column with this expression and check if you get a value.
If the value is correct, make sure that the DateDiff() function gets a date:
=IIF(Parameters!CompanyCode.Value="AB",
DateDiff("d",
CDate(First(Fields!PeriodFrom.Value, "ABReportData")),
CDate(First(Fields!PeriodTo.Value, "ABReportData"))
),
DateDiff("d",
CDate(First(Fields!PeriodFrom.Value, "XYReportData")),
CDate(First(Fields!PeriodTo.Value, "XYReportData"))
)
)
I cannot work out why these Total expressions don't work...
I am trying to add any cells that have a date later than today, with any cells that have "Not Reqd", and then divide that by the number of rows, to get a percentage.
All I'm getting is #Error.
These are the expressions I've tried:
=SUM(IIf(Fields!Jetter_Trng.Value >Today OR
Fields!Jetter_Trng.Value = "Not Reqd",1,0)))/(Count(Fields!Jetter_Trng.Value)
and
=Count(IIf(Fields!Jetter_Trng.Value >Today OR
Fields!Jetter_Trng.Value = "Not Reqd",1,Nothing)))/(Count(Fields!Jetter_Trng.Value)
The "Not Reqd" string has come from an expression that changes a date (01/01/1950) to "Not Reqd". Maybe this is messing things up:
=iif(Fields!Jetter_Trng.Value = "01/01/1950", "Not Reqd", Fields!Jetter_Trng.Value)
The current working expression (not looking for "Not Reqd") is:
=COUNT(IIF(Fields!Jetter_Trng.Value>Today,1,Nothing)))/(Count(Fields!Name.Value))
I'm a bit lost...
A couple of notes on your expression as it stands at present
Jetter_Trng appears to be a string representing either a date or “Not Reqd”. You can’t compare strings to dates without casting them to a date type first using CDATE()
The number of braces (( and )) do not match
The root of your problem though is that you are using Jetter_Trng to return either a Date, or the value “Not Reqd”.
When SSRS attempts to evaluate an expression it does it all at the same time. It doesn’t follow a path to find the answer, and ignore other paths. Therefore, when you are attempting to compare
Fields!Jetter_Trng.Value >Today
This is comparing a string to a date, and throwing the error, as this mean nothing
"Not Reqd" > Today
You won’t be able to do all that you want to using only one Field of type string.
Your options are to
Use two fields – the date and a flag indicating not required, or
Use one field – but have an “invalid date” (01/01/2100 perhaps) that you could then treat as the “Not Reqd” value, and check if the current date is less than that (which it always will be)
Using the second option here you could then use the following expression to create the desired calculation
=sum(iif(CDate(Fields!Jetter_Trng.Value) > Today, 1, 0)) /
Count(Fields!Jetter_Trng.Value)
Which would evaluate this dataset as follows
I've got a report outputting to an Excel file, with some fields in a database being empty.
I'm trying to filter these null values out using the following Iif expression in a cell:
=Iif(
IsNothing(Fields!START_DATETIME.Value),
0,
Fields!START_DATETIME.Value
)
This doesn't work, I get "#VALUE" in the cells where the data was null, and the time value of the cell where there was data.
If I use the same expression without an expression as the third parameter:
=Iif(
IsNothing(Fields!START_DATETIME.Value),
0,
1
)
I get 1s where there is a date in the table and 0s where there is nothing.
I'm sorry if this is a duplicate, I couldn't find anything that worked for me...
Edit:
I'm using VisualStudio Professional 2013
Update
What I've got for each entry in the database is: a datetime for authorization time, another for start time and another for stopped time. There is always an entry for authorization time, but not always for start and stopped.
I've split the date and time out from the various datetimes into columns like so:
Authorized date
Authorized time
Start time
Stop time
The idea is that if no start and stop time are present, just use the authorized time
The following expressions are put in the Start time column. Sorry, I don't have enough rep to post images or more than 2 links. I've put program output in this album:
http://imgur.com/a/zJSAY
Doing:
=Iif(Fields!START_DATETIME.Value is Nothing,
DateAdd(DateInterval.Day,693594,TimeValue(Fields!AUTH_DATETIME.Value)),
DateAdd(DateInterval.Day,693594,TimeValue(Fields!AUTH_DATETIME.Value))
)
Outputs the authorized time, so that works fine.
However, doing:
=Iif(Fields!START_DATETIME.Value is Nothing,
DateAdd(DateInterval.Day,693594,TimeValue(Fields!AUTH_DATETIME.Value)),
DateAdd(DateInterval.Day,693594,TimeValue(Fields!START_DATETIME.Value))
)
Outputs the start time where there is an entry, and #VALUE! (null or some such) when there isn't anything.
And doing:
=Iif(Fields!START_DATETIME.Value is Nothing,
DateAdd(DateInterval.Day,693594,TimeValue(Fields!START_DATETIME.Value)),
DateAdd(DateInterval.Day,693594,TimeValue(Fields!AUTH_DATETIME.Value))
)
Outputs the authorized time where there is an entry in start time, and #VALUE! when there isn't anything.
I can't figure this out. I understand (1) and (3), but what I need is for (2) to display the correct start time while there is one, and the authorized time when there isn't.
Update 2
It turns out that Iif() evaluates each conditions even if it is not selected, and any error encountered is passed on regardless. See comments to Why isn't my iif statement evaluating true when the condition is correct?
To fix this I did:
=DateAdd(
DateInterval.Day,
693594,
TimeValue(
Iif(
Fields!START_DATETIME.Value is Nothing,
Fields!AUTH_DATETIME.Value,
Fields!START_DATETIME.Value
)
)
)
This seems to work for me, and should have been the common sense way to do this in the first place...
Thanks again for your help guys.
Excel will guess the type of a column based on the first few cells. If there's typical DATETIME values in those, it'll guess the column is a date column, and it will error with "#VALUE! on values that don't match the type, such as "0".
You should ask yourself what you want to display in Excel for NULL values:
If you just want an empty cell, don't use an expression at all. Just Fields!START_DATETIME.Value will render a NULL value as an empty cell.
If you want a "baseline" datetime, make sure to use that instead of "0". Easiest is probably in your query with something like ISNULL(START_DATETIME, GETDATE()).
If you really want a "0", make sure it appears in one of the first few cells by ordering. Excel will see the "0" and not set the column type to date/time.
Hope your well.
I am working on a report and seem to get a #error. Seems like it is a divide by zero error but I can not work out a solution. The expression:
=( Sum(Fields!Line_Sell.Value) - Sum(Fields!Line_Cost.Value) ) / Sum(Fields!Line_Sell.Value)
I am relatively new to RS but have tried
ISNULL( )
but with no success.
Any help, I would be greatful.
Thanks
Let's say you have an expression set to x / y, where y has the potential to be zero. As you experienced, SSRS will change this value to #ERR (or sometimes NaN).
Your first instinct would be to test for zero
=Iif(y = 0, 0, x/y)
Unfortunately, this can result in a debugger warning that says you are attempting to divide by zero. This is because SSRS attempts to evaluate the entire statement during compilation.
As a result, we need to provide SSRS with a value that it can evaluate
=x * y ^ -1
This is the same as dividing by your y value, but SSRS sees this as dividing by infinity, and therefore can make an evaluation.
As a final step, you will want to make sure your code can properly export to Excel. Under 2012 SSRS (and possibly later, I haven't tested), SSRS has an issue where possible zero values export to Excel as 0.000000000000000 and cause loss of data errors.
To correct this, we explicitly output the value of zero
=Iif(x = 0 OR y = 0, 0, x * y ^ -1)
The expression in your comment doesn't look complete, so I can't tell what you tried next that didn't work. You definitely want to test for zero before performing division.
You can try this:
=iif( Sum(Fields!Line_Sell.Value) = 0, 0, (Sum(Fields!Line_Sell.Value) - Sum(Fields!Line_Cost.Value)) / Sum(Fields!Line_Sell.Value))
Or to check for an empty value also, you can try this:
=iif( (Sum(Fields!Line_Sell.Value) = 0 or IsNothing(Sum(Fields!Line_Sell.Value)), 0,Sum(Fields!Line_Sell.Value) - Sum(Fields!Line_Cost.Value)) / Sum(Fields!Line_Sell.Value))
If that's still not yielding results, break down the expression and run the report. Try Sum(Fields!Line_Sell.Value) by itself, run the report and see what you get. To be more efficient create a table with one column for Fields!Line_Sell.Value and another for Fields!Line_Cost.Value. In the table, include detail rows and add a total row so that you get the Sum function with each of those fields individually. You need to look at the detail records to try to extrapolate why the aggregate function isn't working. Bottom line - decompose your expression and test it piece by piece. The answer is in there somewhere.
I am currently working on a report which has data injected into it from a Web Service. This webservice sends it a "double?" permitting null values. In the report it displays the values returned which are later used to sum and average the numbers. My issue is in the expression to display the value (and i can see a similar issue coming up when i work on the calculations but one step at a time...) I am using an IIf statement checking if the value is numeric, if so display the value (converting it to a double) if not display an empty string. When its a numeric value the value displays properly however when its null I am getting the value #ERROR. It seems that the IIf statement evaluates both ends of the IIf statement!!!
Anyhow I have done some research and it seems people suggest to modify the sending code to check if its null and send a 0 instead BUT in my case this wont help (nor will a negative number as it also sends negative numbers). The reason for this not working in my case is that I have to calculate an average using all 0s but not null values. For example...
"100, 0, null" => this should be an average of 50... if that null
where to be converted to 0 it would average 25...
Anyways heres my code...
=IIf(IsNumeric(Fields!Ventas.Value), CDbl(Fields!Ventas.Value), "")
I have also tried with a switch statement and get the same issue...
=Switch(IsNumeric(Fields!Ventas.Value) = False, "", IsNumeric(Fields!Ventas.Value) = True, CDbl(Fields!Ventas.Value))
Also I have tested that the IIf condition is working properly by testing this:
=IIf(IsNumeric(Fields!Ventas.Value), 1, 0)
And that works properly... Anyways any help would be greatly appreciated as I dont know what else to try... below are some links I found regarding my issue but again they recommend sending a 0 instead of null which in my case is useless... And one link suggests using a switch but again that didnt work...
Link #1
Link #2
I just solved this issue heres how it can be done hope this helps someone else with this same problem!!
=IIf(IsNumeric(Fields!Ventas.Value), CDbl(IIf(IsNumeric(Fields!Ventas.Value), Fields!Ventas.Value, 0)), "")
You will have the same issue using expression with possible division by zero, like IIF([B]=0, Nothing, [A]/[B])
I used a simple workaround: IIF([B]=0, Nothing, [A]/IIF([B]=0, 1, [B]))
This error is also registered on the Microsoft Connect, but no answer from MS so far.