I have a header section which is showing totals etc. I have two fields in another dataset (dataset estimates) which I want to pull into header dataset.
Fee (circled) = IIf(Right(Fields!ProjCategoryId.Value, 3) <> "EXP", Fields!Value.Value, 0)
3rd Party (circled) = IIf(Right(Fields!ProjCategoryId.Value, 3) = "EXP", Fields!Value.Value, 0)
I know you can sum datasets from another dataset with no issues, but how to use IIF etc. as well?
You can use custom code and a lookupset().
Code:
Function SumLookup(ByVal items As Object()) As Decimal
If items Is Nothing Then
Return Nothing
End If
Dim suma As Decimal = New Decimal()
Dim ct as Integer = New Integer()
suma = 0
ct = 0
For Each item As Object In items
suma += Convert.ToDecimal(item)
ct += 1
Next
If (ct = 0) Then return 0 else return suma
End Function
To add this, right-click on the blue background of the report and select Report Properties.
Click on the Code option.
Paste it into this window
Call this with =Code.SumLookup(lookupset("EXP", right(Fields!ProjCategoryId.Value, 3), Fields!Value.Value, "DatasetName")) in your expression where you want the number to appear
This will add up all the Values where the ProjCategoryId starts with "EXP".
For <> "EXP", you might need to do several calls to it to add them all up for each thing that it can start with. For example..
Code.SumLookup(lookupset("ONE", right(Fields!ProjCategoryId.Value, 3), Fields!Value.Value, "DatasetName")) + Code.SumLookup(lookupset("TWO", right(Fields!ProjCategoryId.Value, 3), Fields!Value.Value, "DatasetName"))
This is actually really simple. Find the Textbox name that contains the expression.
(When we say textbox this does not have to be a textbox from the toolbox, it can be a field within tablix). Where you want to reference the value simply add the expression and you can reference the result.
Related
I have data matrix with more Group. In first group contains values calculated using an expression (circled in blue in the picture). I add these values in a group "Total_worker" (for each group) using Custom code - below (circled red in the picture ) - expression: =formatnumber(Code.GetTotal(ReportItems!Textbox289.Value) & Code.ResetTotal(ReportItems!Textbox289.Value),1)
Now, I need SUM Value from group Total_worker to Total_workplace (circled black). How do I do that ?
How to change custom code and how to write an expression?
I use this Custom code:
Public Shared Value as decimal=1
Public Shared Function GetValue(Item as decimal) as decimal
value = value + item
return item
End Function
Public Shared Function GetTotal(item)
return value
End Function
Public Shared Function ResetTotal(item)
value=0
End Function
In group "Total_workplace" I expect add 7,4 + 4,7 = 12,1
How should I handle divide by zero error in rdl expression for SSRS 2017?
I have tried the below approaches but nothing works.
1.
=iif((iif((SUM(Fields!Consumption.Value)/
(iif((Fields!daysINDate.Value)<=0,1,Fields!daysINDate.Value))*30)<=0,0,
(SUM(Fields!Consumption.Value)/
(iif((Fields!daysINDate.Value)<=0,1,Fields!daysINDate.Value))*30)))=0,0,
SUM(Fields!ClosingStock.Value)/(iif((SUM(Fields!Consumption.Value)/
(iif((Fields!daysINDate.Value)<=0,1,Fields!daysINDate.Value))*30)<=0,0,
(SUM(Fields!Consumption.Value)/
(iif((Fields!daysINDate.Value)<=0,1,Fields!daysINDate.Value))*30))))*30)
(SUM(Fields!ClosingStock.Value)/
(iif((SUM(Fields!Consumption.Value)/
(iif(Fields!daysINDate.Value<=0,1,Fields!daysINDate.Value))*30)<=0,0,
(SUM(Fields!Consumption.Value)/
(iif(Fields!daysINDate.Value<=0,1,Fields!daysINDate.Value))*30)))*30)
You should only be checking the denominator for zero - not the division operation.
=IIF(Fields!daysINDate.Value = 0, 0, SUM(Fields!Consumption.Value))
/
IIF(Fields!daysINDate.Value = 0, 1, Fields!daysINDate.Value)
*30
SSRS checks both sides of the IIF statement... if either the THEN or ELSE part if the IIF is 0, it will throw up an error.
One way to get around it is to use VB code.
Right click on your report -> report properties -> code and paste the following code :
Public Function NDZ(Byval a As Decimal,Byval b As Decimal, Byval c As Decimal) As Decimal
' Fix for divide by zero problem in VB
' calculates a/b and either returns result or c if b = 0
if b = 0 then
return c
else
return a/b
end if
end function
Now on your report, enter the following expression in your text box you want to perform the calculation :
=code.NDZ(SUM(Fields!Consumption.Value), Fields!daysINDate.Value, 0)
so if either Fields!daysINDate.Value or SUM(Fields!Consumption.Value) is 0, then it will return 0 and not an error.
I have data in an SSRS report that groups and provides percentages. I need to change the font color for the percentages and I can't figure out how to create the expression.
Currently I obtain the percentage with this formula:
Sum(Fields!Time_Msg.Value)/Count(Fields!Driver.Value)
The Time_Msg field is obtained from SQL. I want the font expression to be the greater of three colors using this formula:
IIF(Fields!Time_Msg.Value = 1, "green",
IIF(Fields!Time_Msg.Value = 0
And Fields!Time.Value > Fields!TimeB.Value, "red", "yellow"))
So essentially if the record is on time the font will be green, if the record is late then red and early then yellow.
But what do you do when providing grouping sums? If from all the records the greatest value is green, then the font for the grouping sum should be green, etc.
Sorry for formatting as I'm new to this forum..
go to Textbox properties-> Font -> Color and type an expression like this
Instead of using IIF please try to use Switch statement.
= switch(Fields!Time_Msg.Value = 1, "green",
Fields!Time_Msg.Value = 0
And Fields!Time.Value > Fields!TimeB.Value, "red")
Note: Put your condition in switch statement.
Let me know if this works
You can use custom code in your project
Function SetColor(ByVal Value As Integer) As String
Dim Ret as string
Dim doubleVal As Double
Ret = "Blue"
Dim Val As Double
doubleVal = System.Convert.ToDouble(Value)
if Value >= .86 then
Ret = "OliveDrab"
ElseIf Value <=.86 then
Ret = "IndianRed"
end if
End Function
assign whatever numbers you want to whichever colours you are using (I'm using greater or less than 86%) and set the font colour expression to =Code.SetColor(Me.Value)
I have a 100% Stacked Bar chart. Students are banded based on their attendance,and the report simply tracks changes in the band populations as a percentage of the total student population. In Report Builder, this works totally fine (except in that it highlights our rubbish attendance of course...)
The problem arises when:
The report is exported from Report Builder to PDF/Word/Excel/whatever
The report is deployed to an SSRS server and run through the browser
You change to a subsequent page of the report, and then change back to the page with the graph.
In all case although the actual chart remains unchanged, the Legend loses its mind a little bit and shows the top three items as 100%:
I can't think of any reason that that should happen...the report was particularly finicky to make as a result of the underlying data structure (which regrettably is based on a Report Model, meaning I can't tweak it with SQL) and I had to use custom vb code in the end to get it to do what I wanted, but I can't see why any of that should change its behaviour either on the server or when exported.
So my question is; why does this happen, and how do I stop it happening?
EDIT: By Request:
The dataset inherently returns data in the format below. There's a row per learner ID per "Week Start Date".
The custom code I am using is pasted below (inept I know - no laughing!):
Private attendance_table As New System.Collections.Hashtable()
Private last_added_table As New System.Collections.Hashtable()
Public Function band_calc(ByVal attendance As Double) As String
REM Define the bands that I want to track
If attendance = 1 Then
Return "A"
ElseIf attendance >= 0.975 Then
Return "B"
ElseIf attendance >= 0.95 Then
Return "C"
ElseIf attendance >= 0.925 Then
Return "D"
ElseIf attendance >= 0.90 Then
Return "E"
ElseIf attendance >= 0.85 Then
Return "F"
ElseIf attendance >= 0.8 Then
Return "G"
Else
Return "X"
End If
End Function
Public Function get_attendance_band(ByVal week_start_date as String, ByVal learnerID As Integer, ByVal possibles As Integer, ByVal presents As Integer) As String
If attendance_table Is Nothing Then
Dim attendance_table As New System.Collections.Hashtable()
End If
If last_added_table Is Nothing Then
Dim last_added_table As New System.Collections.Hashtable()
End If
REM check if attendance_table has the Learner already
If attendance_table.ContainsKey(learnerID) Then
REM check if we've already added this week's data in
If attendance_table(learnerID).ContainsKey(week_start_date) Then
REM just return the band_calc for those data
Return band_calc(attendance_table(learnerID)(week_start_date)(1) / attendance_table(learnerID)(week_start_date)(0))
Else
REM Add in this week to the hashtable. Add this weeks data to the last weeks data
attendance_table(learnerID).Add(week_start_date, New Object() { possibles + attendance_table(learnerID)(last_added_table(learnerID))(0), presents + attendance_table(learnerID)(last_added_table(learnerID))(1)})
REM record that this is now the last date updated for this learner
last_added_table(learnerID) = week_start_date
REM show the band!
Return band_calc(attendance_table(learnerID)(week_start_date)(1) / attendance_table(learnerID)(week_start_date)(0))
End If
Else
attendance_table.Add(learnerID, New System.Collections.Hashtable())
attendance_table(learnerID).Add(week_start_date, New Object() {possibles, presents})
last_added_table.Add(learnerID, week_start_date)
Return band_calc(attendance_table(learnerID)(week_start_date)(1) / attendance_table(learnerID)(week_start_date)(0))
End If
End Function
For the series properties; The sort, group and label (which defines the Legend obviously) are all set to this:
I created a custom color palette for my charts using a technique described on TechNet.
I also have a series of drill-through column charts, where you click on one column and it passes a parameter through to the next chart and so on, giving the appearance of drill-down.
My graphs consist of 3 types of labor, and have three colors on the main chart. When I drill down to the next chart, some of the categories do not have all three types of labor that the main one has. So the first color in the palette is assigned to the series, even though it was the second color on the previous chart. I'd like to avoid this, if possible.
So a data value is green on the first chart (2nd in the color order) and yellow on the next chart (1st in the color order). How do I make the graphs "remember" the total number of series groups that were in the first chart?
This is Reporting Services 2005.
You cannot fix this using custom colour palettes.
What you can do is assign the labour type a colour in the database (using HEX is easiest). Then pass that in in your data set. Then set the color property to you hex value.
Unfortunately this is not possible. I've been looking for this for quite some time...
I was able to solve this because I was using a custom color palette, implemented as a hash table. I basically serialized this information and passed it to a hidden parameter on the subreport and then reinflated the data structure.
It's not perfect, but it works for now.
' Define some globals, including the color palette '
Private colorPalette As String() = _
{"#FFF8A3", "#A9CC8F", "#B2C8D9", "#BEA37A", "#F3AA79", "#B5B5A9", "#E6A5A4", _
"#F8D753", "#5C9746", "#3E75A7", "#7A653E", "#E1662A", "#74796F", "#C4384F", _
"#F0B400", "#1E6C0B", "#00488C", "#332600", "#D84000", "#434C43", "#B30023"}
' color palette pulled from SAP guidelines '
' http://www.sapdesignguild.org/resources/diagram_guidelines/color_palettes.html '
Private count As Integer = 0
Private colorMapping As New System.Collections.Hashtable()
' Create a custom color palette '
Public Function GetColor(ByVal groupingValue As String) As String
If colorMapping.ContainsKey(groupingValue) Then
Return colorMapping(groupingValue)
End If
Dim c As String = colorPalette(count Mod colorPalette.Length)
count = count + 1
colorMapping.Add(groupingValue, c)
Return c
End Function
' In custom actions of the data value, set the results of this '
' function to the mapping parameter in the next report '
Public Function PassColorMapping() As String
If colorMapping.Count = 0 Then
Return Nothing
End If
Try
' convert the hashtable to an array so it can be serialized '
Dim objHash As Object()() = ToJaggedArray(colorMapping)
' serialize the colorMapping variable '
Dim outStream As New System.IO.StringWriter()
Dim s As New System.Xml.Serialization.XmlSerializer(GetType(Object()()))
s.Serialize(outStream, objHash)
' move on to the next report '
Return outStream.ToString()
Catch ex As Exception
MsgBox(ex.Message)
End Try
End Function
I ran into an issue where I couldn't find the equivalent of the onLoad event for the report. Since I wasn't sure where to put this inflate code, I stuck it in the background color of the plot area. Hence I always return "WhiteSmoke". I'll change this if I can find the right place to put it.
' Call this function when the report loads to get the series groups '
' that have already been loaded into the custom color palette '
' Pass in the parameter used to store the color mapping '
Public Function InflateParamMapping(ByVal paramMapping As Parameter) As String
Try
If paramMapping.Value Is Nothing Then
Return "WhiteSmoke"
ElseIf colorMapping.Count = 0 Then
Dim pXmlized As String = paramMapping.Value
' deserialize the mapping parameter '
Dim s As New System.Xml.Serialization.XmlSerializer(GetType(Object()()))
' get the jagged array and convert to hashtable '
Dim objHash As Object()() = DirectCast(s.Deserialize(New System.IO.StringReader(pXmlized)), Object()())
' stick the result in the global colorMapping hashtable '
colorMapping = ToHashTable(objHash)
count = colorMapping.Count
End If
Catch ex As Exception
' MsgBox(ex.Message) '
End Try
Return "WhiteSmoke"
End Function
ToJaggedArray() and ToHashTable() are helper functions because a HashTable is not serializable since they implement an IDictionary. I was in a hurry so I just converted them to an array right quick. Code comes from the Collection Serialization in ASP.NET Web
Services article written by Mark Richman. I converted the code from C# to VB.NET to use in the report.
Public Function ToJaggedArray(ByVal ht As System.Collections.HashTable) As Object()()
Dim oo As Object()() = New Object(ht.Count - 1)() {}
Dim i As Integer = 0
For EAch key As Object in ht.Keys
oo(i) = New Object() {key, ht(key)}
i += 1
Next
Return oo
End Function
Public Function ToHashTable(ByVal oo As Object()()) As System.Collections.HashTable
Dim ht As New System.Collections.HashTable(oo.Length)
For Each pair As Object() In oo
Dim key As Object = pair(0)
Dim value As Object = pair(1)
ht(key) = value
Next
Return ht
End Function
Now in the report itself you need to do a couple things.
Add a reference to System.Xml in Report Properties in both reports.
In the Actions of your parent report, set the Parameter containing your data structure to =Code.PassColorMapping()
In the Plot Area section of your report, put this expression for the background: =Code.InflateParamMapping(Parameters!colorMapping)
And of course, in the Fill for your data Series Style on both charts put this expression: =Code.GetColor(Fields!Type.Value)
You can continue doing this for as many subreports as you want - I currently have 3 levels of drill-through and it works fine.
I solved that extremely easy.
In my parent report I have lets say 12 series fields, each one getting their own color in a chart, on my child report I just keep all series on the chart, for instance going from a column chart to a line chart using drill down, but I control the visibility of them...
So in the child report in Series Properties -> Visibility I just add an expression:
=(Fields!ContentType.Value <> Parameters!ContentType.Value)
This way the report only keeps the visibility of the clicked value and hides all the others and the colors remains the same :)