DateDiff function in SSRS (report server) gives error? - reporting-services

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"))
)
)

Related

SSRS Expression - Subtracting the Sums, when one group is a parameter

I am trying to Subtract two scenarios from each other in a SSRS report. One scenario is a constant, 'Actuals' the other is a dynamic forecast scenario and set by a parameter. Here is the code I am trying:
=sum(iif(Fields!Scenario_Name.Value=Parameters!ScenarioName.Value,CDbl(Fields!CAD.Value),0))-
sum(iif(Fields!Scenario_Name.Value="Activity Actuals",CDbl(Fields!CAD.Value),0))
I have also tried:
=sum(iif(Fields!Scenario_Name.Value=Parameters!ScenarioName.Value,Fields!CAD.Value,0))-
sum(iif(Fields!Scenario_Name.Value="Activity Actuals",Fields!CAD.Value,0))
The report runs but I get an error in the line where this expression is entered.
The CDEC was important to both the CAD.value and the 0. The Parameter value needs a (0) as in the final expression that works.
enter code here=Sum(IIf(Fields!Scenario_Name.Value=Parameters!ScenarioName.Value,CDEC(Fields!CAD.Value),CDEC(0))) -
Sum(iif(Fields!Scenario_Name.Value="Activity Actuals",CDEC(Fields!CAD.Value),CDEC(0)))

SSRS switch and datediff

I am writing a report that needs to display a different datediff calculation based on a specific Value from a column. If the order is "Active" start time to Now or if the order is ceased start time to stop time.
The individual DateDiff expressions work but when I try to combine them in one expression using either switch or IIF I get errors.
Any suggestions would be great.
=IIF(Fields!OrderStatus.Value="Active", DateDiff("d",Fields!OrderStartTime.Value, "Now"()),
IIF(Fields!OrderStatus.Value="Discontinued", DateDiff("d",Fields!OrderStartTime.Value, Fields!DiscontinueTime.Value)
)
)
=Switch(Fields!OrderStatus.Value="Active",
DateDiff("d",Fields!OrderStartTime.Value, "Now"()),
Fields!OrderStatus.Value="Discontinued",
DateDiff("d",Fields!OrderStartTime.Value, Fields!DiscontinueTime.Value),
)
You are missing the third argument for your second iif statement (in this case, it'll be {Nothing}. This should work:
=IIF(Fields!OrderStatus.Value="Active",DateDiff("d",Fields!OrderStartTime.Value,Today()),IIF(Fields!OrderStatus.Value="Discontinued",DateDiff("d",Fields!OrderStartTime.Value,Fields!DiscontinuedTime.Value),Nothing))

How do I create a interval of 7 days without 'w','week' or 'WeekOfYear'?

I am trying to sort data by the week it was created. I can not use 'w' in DateAdd, 'week' or 'WeekOfYear' without an error stating that function/expression is inaccessible.
What is the best way to create a substitute expression? I want the expression to group the dates based on the week it was created.
I am trying to replicate this:
http://imgur.com/Pk8YjUv
Based on the expected results you posted in the edition of your question I have reproduced the tablix.
I've used the following dataset:
I am supposing you are going to use a tablix component so I added this one with the following data arrangement.
In the Row Groups panel right click on date group.
In group properties, add a group expression and set this:
=DATEADD("d",7-DATEPART(DateInterval.Weekday,Fields!date.Value),Fields!date.Value)
Also in the tablix you have to set the same expression:
In the columns you want to sum you can use the Sum function or any aggregation function. I've used =Sum(Fields!value.Value) to Sum values within the same week.
This is the result it will preview:
Let me know if this was helpful.

SSRS Expression with parameter in WHERE clause

In SSRS, I'm trying to calculate the average number of months a client used a program. The programID is the parameter for the whole report. I'm trying to achieve this (not written with real syntax):
=Avg(Fields!length_of_stay.Value, 0))/30.0 WHERE programid = #ProgramID
Using this question, I came up the the following code which is producing an incorrect answer. I tested in SSMS to get the actual values to compare to SSRS results.
=Avg(IIF(Fields!programid.Value = Parameters!ProgramID.Value, Fields!Length_of_Stay.Value, 0))/30.0
The "/30" is used since the value is in days and I need months. I think the issue is using the parameter value chosen; this is my first report trying to calculate expressions with parameters.
Avg returns the average of all non-null numeric values. 0 is not null so it gets included in the average, distorting your result for every row with a different PragramId. Try using Nothing instead:
=Avg(IIF(Fields!programid.Value = Parameters!ProgramID.Value, Fields!Length_of_Stay.Value, Nothing))/30.0

Check null and split value in textbox in SSRS report

I am facing very simple issue but not getting solution over it.
I have textbox in my ssrs report, I am passing value "1;prashant" or null to it. Now, if I pass value "1;prashant" to textbox then textbox should show only "prashant" and If I am passing nothing then it should be blank.
I have tried following IIF condition:
=IIF(IsNothing(FieldS!WIAPPORVER.Value),"",Split(Fields!WIAPPORVER.Value,"#")(1).ToString())
But, I above code is giving an error ["#error" shows in textbox] if I am passing blank value.
Please let me know, where I am wrong in this.
Thanks
There are probably better ways of doing this, but this is what my head came up with at the time:
=IIF(
IsNothing(Fields!WIAPPROVER.Value)
,""
,Right(Fields!WIAPPROVER.Value,Len(Fields!WIAPPROVER.Value) -InStr(Fields!WIAPPROVER.Value,";"))
)
I believe SSRS is trying to compute everything in the report at runtime, so in your case it is still trying to fetch index 1 from an array even though there is nothing in it and it crashes.
Edit: Changed parameters to Fields. I created a parameter to remake the issue at my side.
Just get the split out of the iif. Then in your iif, if field is nothing create a string that when split will return ""
=Split(IIF(IsNothing(FieldS!WIAPPORVER.Value),"#", Fields!WIAPPORVER.Value),"#")(1)
You are relying on the IIf expression short circuiting, but SSRS IIf expressions do not short circuit - the expression will try and work out Split(Fields!WIAPPORVER.Value,"#")(1).ToString() for all rows and fail when this value doesn't exist.
You can get this going by using text expressions, which don't get this error.
With test data:
And a simple table:
I have added columns with both your existing expression and a new expression:
=Right(Fields!WIAPPORVER.Value, Len(Fields!WIAPPORVER.Value) - InStr(Fields!WIAPPORVER.Value, "#"))
This new expression works for NULL values, empty strings and strings with no delimiter present: