SSRS Calculated field Skip NULL - reporting-services

I am trying to make some calculations in a SSRS report.
I have times in the column Hours: "08:25:21" and some of them are NULL.
WORKS:
=IIF(IsNothing(Fields!Hours.Value), Nothing,Fields!Hours.Value)
I want to extract the hours from the time:
ERROR for blank:
=IIf(IsError(Hour(Fields!Hours.Value.toString())),Nothing,Hour(Fields!Hours.Value.toString()))
=IIf(IsNothing(Fields!Hours.Value),Nothing,Hour(Fields!Hours.Value.toString()))
How can I do this so I will get a Nothing when no Hours.value is present?
Danny

If your Hours field is a TimeSpan, you can use the TimeSpan.Hours property.
I've been able to make this work by adding a function to the code of the report.
Function GetHours(timeSpan as TimeSpan) As String
If timeSpan = TimeSpan.Zero
Return Nothing
Else
Return timeSpan.Hours
End If
End Function
Now you can use this expression: =Code.GetHours(Fields!Hours.Value).

Related

How to handle empty values in multi parameter SSRS field

I'm trying to modify a report that uses a multi value parameter (a|b|c|d). That parameter is split into multiple columns. The expression on the first column is =Split(Fields!AlternateVendorDetails.Value.ToString(),"|")(0). If that field is blank in the query, the field in the column is blank, which is right. However, the other columns evaluate to an error (=Split(Fields!AlternateVendorDetails.Value.ToString(),"|")(1)). I've tried =IIF(Fields!AlternateVendorDetails.Value.ToString()='','',Split(Fields!AlternateVendorDetails.Value.ToString(),"|")(1)), and I'm not sure what else will work.
SSRS evaluates expressions even when using Iif conditions and that causes the error.
You need custom code to overcome the limitation.
You can create a function that takes the string, the delimiter and array item index, and returns the value if string is not empty and value index exists
Public Function GetArrayItem( s As String, d As String, i As Integer) As String
If s = "" Or Split(s, d).Length < i+1 Then
Return Nothing
Else
Return Split(s, d)(i)
End If
End Function
I ended up putting a COALESCE(value1 | value2 | value3,' | | ') in the query. That created the necessary fillers to set the values to blank.

Show Value in row before the Previous Row in SSRS

I am building a report in SSRS and wondering how to show the value in the column 2 rows before. I have used the Previous expression to retrieve the one before, but now I need the one before that.
Any help would be much appreciated
Thank you
Sam
Shared Dataset looks like this:
https://i.stack.imgur.com/Ii5y6.jpg
You can use custom code to set and get the value.
Public Dim Previous2 As String
Public Dim Previous As String
Public Function SetPrevious2 ( s As String) As String
Previous2 = Previous
Previous = s
Return Previous2
End Function
For the cell use and expression like the one below
= Code.SetPrevious2(Previous(Fields!MyValue.Value))

SSRS Lookupset sum problem when one or more value are nothing

I got a little problem with Lookupset on a SSRS Report.
The report has a father-child structure in the group section and the datasets come from different tabular.
The problem occurs when one of the Product from Dataset1 is missing on Dataset2, the entire sum returns 0.
I tried this VB code in order to sum up all the values the Lookupset is able to return.
Function SumLookup(ByVal items As Object()) As Decimal
If items Is Nothing Then
Return Nothing
End If
Dim suma As Decimal = New Decimal()
suma = 0
For Each item As Object In items
suma += Convert.ToDecimal(item)
Next
Return suma
End Function
I expected the sum of every Product Lookupset is able to retrurn me but it is just returning 0 if one of the Products is missing in Dataset2.
Is there a way to manage the Nothing returned by Lookupset?
Sorry for the formatting and my poor english and thanks in advance!
I would handle this by checking for the value in your LOOKUPSET expression that SumLookup is summing.
I assume that your expression looks something like this:
=CODE.SumLookup(LookupSet(Fields!ProductID.Value,
Fields!ProductID.Value,
Fields!Price.value,
"Dataset2") )
Use an IIF with ISNOTHING to check for the NULL values and set them to zero:
=CODE.SumLookup(LookupSet(Fields!ProductID.Value,
Fields!ProductID.Value,
IIF(ISNOTHING(Fields!Price.value), 0.00, Fields!Price.value),
"Dataset2") )
The function returns a Decimal datatype. By a Decimal datatype Nothing and 0 are the same. You can test this.
Put a tablix into your report with year from 2017 to 2019. Then put the year in a column of the tablix as a number format, then write the following expression in the detail textbox:
=CDec(IIF(CDec(Fields!Year.Value) = 2017, 0, Nothing))
After executing your report you will notice that every value in the year column is 0.
The same goes for the check. Both of these expressions will always return Yes. I basically check for 0 and the second one for for Nothing:
=IIF(CDec(IIF(CDec(Fields!Jahr.Value) = 2017, 0, Nothing)) = 0, "Yes", "No")
=IIF(CDec(IIF(CDec(Fields!Jahr.Value) = 2017, 0, Nothing)) = Nothing, "Yes", "No")
But remember your textbox/column has the be a number format.
So if you want to return Nothing and you display it in a number format textbox, it will show you a 0.

How to display number values with commas in form

In my Access query, I have the query using a VBA function to figure the value that goes in the query field.
In the form, if the stringval textbox has a value, then I want to compute it, but if not, it should remain empty (null).
Function GetValue(stringval, numval)
Dim result
stringval= stringval & ""
result= IIf(stringval<> "", numval* 1.5, Null)
GetValue = Int(result)
End Function
Now, I have a form that uses this query, and on the form is a textbox that displays the query value. I want the value to be formatted with commas in the numbers for easy reading. Everything I've tried so far does not show any commas.
I've tried:
used Standard for the Format > Formatfor the textbox (in properties)
putting #,###.### in the textbox Format value
putting #,##0.0## in the textbox Format value
changing Data > Text Format but it only gives me Plain Text and Rich Text - no option for numbers.
returning a double from the function
Note: if I don't use a custom VBA function, and write the formula directly into the query, then it does display commas. But when I move the formula into the function then the commas are lost.
What do I do?
[update]
I tried Gustav's solutions and since they didn't work for me, I added those as items to my "what I've tried" list above.
Also, if I look at the query in datasheet view, the number values sort alphabetically instead of by the size of the value. When I used the forumulae directly in the query instead of using functions, it sorted by the value of the number. I hope this is a clue.
Numbers carries no format. A format is applied when displayed only.
But be sure to return a Double if not Null:
Function GetValue(stringval, numval)
Dim result
If stringval & "" <> "" Then
result = Int(CDbl(numval) * 1.5)
Else
result = Null
End If
GetValue = result
End Function
Then apply your Format to the textbox
#,##0.0##
Or force a formatted string to be returned:
If stringval & "" <> "" Then
result = Format(Int(CDbl(numval) * 1.5), "#,##0.0##")
Else
result = Null
End If
and skip formatting of the textbox.
The solution is this: the function has to be declared as a double.
That allows the query's datasheet view to know it is displaying numbers - and so you can set the field's format to Standard for the comma to display. This also allows the form to know it has a number and it will display the comma there, too. I knew it had to do with double, but didn't realize before that the function needed to be declared as such.
Function GetValue(stringval, numval) as double '<----THIS!!!!
Dim result
If stringval & "" <> "" Then
result = numval * 1.5
Else
result = 0 `<--can't return null here; use nz function in control source for textbox
End If
GetValue = int(result) 'to remove decimals
End Function
The problem I was having was in some of my functions I need to return double or null, because I wanted textboxes to remain blank if they contained no data. Now, at least I know how to make the numbers generated by functions to display commas.
And here is how to deal with the fact that you can't return null as the value of a double. The function is originally from here.
Put this function in a module so it is public, and then in the control source for the textbox, instead of just putting the field value, put Zn(fieldvalue). This works like a charm (although using functions in the control source seems to have a delay on the form display). This way you can keep the underlying value as a double and still get commas to display in both the form and the query whilst keeping the field blank if necessary.
Public Function Zn(pvar)
' Return null if input is zero or ""
If IsNull(pvar) Then
Zn = Null
ElseIf IsNumeric(pvar) Then
If pvar = 0 Then
Zn = Null
Else
Zn = pvar
End If
Else
If Len(pvar) = 0 Then
Zn = Null
Else
Zn = pvar
End If
End If
End Function

Sum of Averages in SSRS

I have a table created a report where there should be a field with sum of Avg.
Please find the below image
I am getting the average but i need to sum the average to display A.
Have you tried adding a custom function to the report, as outlined here?
This person wrote this custom function:
Dim Test as double
Public Function Testing(Byval value as Double, ByVal reset as Boolean) as Double
Test = Test + value
Testing = Test
If reset = TRUE Then
Test = 0
End If
End Function
At the footer of the group call Code.Testing([value], False) then call it again outside of the group with code.Testing([value],True) to reset. Worked for me :)