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)
Related
I have a columns with numbers that determine a job and suffix:
Example:
|Jobnumber|Suffix|
x001 1 - white
x001 2 - grey
x001 2 - grey
x001 3 - white
x002 1 <---- where it would break the alternating color, Should be grey!
x002 1 - should be grey
x002 1 - should be grey
x002 2 - should be white
x002 2 - should be white
x003 1 - should be grey
I have the report set up to accept a range of Jobnumbers and a range of Suffixes so each Jobnumber has a set of suffixes ordered by ascending. I have the expression code alternate by the different Suffix numbers by color but it breaks when the next Jobnumber and Suffix is up next in the report.
How can I get the rows to still alternate by color when the next set of Jobnumbers and Suffixes come up in the report?
If you want your alternating row colours to ignore the jobnumber and suffix then base your expression on ROWNUMBER instead Suffix.
e.g. =IIF(ROWNUMBER(Nothing) MOD 2, "LightBlue", Nothing)
UPDATE: BASED ON REVISED QUESTION
There may be a more elegant way of doing this but the following should work.
The first thing to do is go to the report properties and add two report variables to your report. Add AltBackColour with a value of 1 and LastRownumber with a value of -1. Switch off the read-only check box
Next, click on the Code tab add the following function.
Public Function BackColour (ByVal cuRowNumber AS Integer, ByVal lastJobNumber AS String, ByVal curJobNumber AS String, ByVal lastSuffix AS Integer, ByVal curSuffix AS Integer) as Integer
' Check if EITHER the job or suffix have changed, if so, switch the backcolour flag and update the report variable.
Dim switch as boolean
switch = ((lastJobNumber <> curJobNumber) or (lastSuffix <> curSuffix)) and cuRowNumber <> Report.Variables!LastRowNumber.Value
If Switch = true THEN
IF Report.Variables!AltBackColour.Value = 0 THEN
Report.Variables!AltBackColour.Value = 1
ELSE Report.Variables!AltBackColour.Value = 0
END IF
End if
Report.Variables!LastRowNumber.Value = cuRowNumber
BackColour = Report.Variables!AltBackColour.Value
End Function
Finally, set the BackgroundColor property of the row to the following expression.
=IIF(
Code.BackColour(
RowNumber(Nothing)
, Previous(Fields!JobNumber.Value)
, Fields!JobNumber.Value
, Previous(Fields!Suffix.Value)
, Fields!Suffix.Value
) = 0
, Nothing
, "LightGreen"
)
I did this with your sample data and got the following result.
Basically the code checks to see if either the job of suffix have changed and switches a flag between 1 and 0 to indicate which colour to use. It also checks that we are not checking the same row as previously. We do this because even though we set the row's backcolor property, what actually happens is each cell gets is backcolor property set meaning the code gets called for each cell and will keep flipping the flag. By checking if the row has changed we get round this.
I have text box expression below, the result look like this:
Newtoy - Oldtoy - Notoy
I can get all text to become blue color, but I want the "dash sign" (-) become black color.
My expression is incorrect . Can you help to see what went wrong. Thank you.
="font color = 'blue' size = '1'"
& iif(First(Fields!ID.Value, "DataSet2") = " " ,"None", First(Fields!ID.Value, "DataSet2"))+ "/font",
= iif(First(Fields!ID.Value, "DataSet2") = "-" ,"black", "blue",
And this is the query populating the dataset:
SELECT STUFF((SELECT CAST(b.branch as varchar) +' '+ ' | ' +' '
FROM printers p
full join branch b on p.branchid = b.branchid
where p.printername is null
order by b.branch FOR XML PATH(''), TYPE).value('.', 'varchar(max)'),2,1,'');
Alright, so now that I see why the data is formatted as it is, I've found a solution that should work for you. Basically, to make this work, you'll need to modify the query from the dataset to the following:
SELECT CAST(ISNULL(b.branch, 'None') as varchar)
FROM printers p
FULL JOIN branch b ON p.branchid = b.branchid WHERE p.printername IS NULL
ORDER BY b.branch
This will provide the b.branch values in a way that they can be joined in SSRS with font formatting in between each value. It also accounts for NULL values with the ISNULL function which will check each ID and select NULL values as None when it returns the values.
The expression should begin with the font color set to blue. Next, you'll need to use two SSRS functions, LookupSet will return all of the values of Fields!ID.Value in an array which we can put into one string with Join. Join requires values to join and a delimiter, which in this case is your chance to format the text color correctly. Delimiting by "</font><font color = 'black' size = 1>-</font><font color = 'blue' size = 1>" will close the first <font> tag which should make the first ID blue, inserts a black dash, and opens a new <font> tag for the next ID, and so on. Finally, you add a closing </font> tag to finish coloring the final ID in blue.
="<font color = 'blue' size = 1>"
& Join(LookupSet(1, 1, Fields!ID.Value, "DataSet2"), "</font><font color = 'black' size = 1>-</font><font color = 'blue' size = 1>") & "</font>"
I've tested this and it seems to work as it should. However, you'll need to be sure that you use this expression in a Placeholder with Markup type set to HTML to Interpret HTML tags as styles for this to work.
Here's an example of how it looks when I use the expression above with a lighter color to demonstrate:
I have a matrix in ssrs 2008 like below:
What I want is to change background color of the 1st and the 4th rows. To provide this, I added the code below to the report:
Function AlternateColor(Byval rowNumber as integer) As String
Dim blue As String = "LightBlue"
Dim plum As String = "Plum"
Dim white As String = "White"
If rowNumber = 1 then
Return blue
Else if rowNumber = 4 then
Return plum
Else
Return white
End If
End Function
And I added this line as the background color expression for each cells including Fields!.Value:
=Code.AlternateColor(rownumber(nothing))
After all, this is an example from the result:
This is the first row, and I was expecting the see it all blue, and the 4th row all plum, the others should be all white. Changing rownumber("Tablix14") in the expression did not help as well, where matrix's name is Tablix14. Any help regarding to fix the issue here would be so appreciated. Thanks.
Changing the expression as below fixed the issue.
=Code.AlternateColor(Runningvalue(Fields!FiscalQuarter.Value,CountDistinct,"Tablix14"))
I am using this for my alternating background colors
IIF((RowNumber(Nothing) Mod 2),"GAINSBORO", "WHITE")
but I need to hide one of my rows in the Tablix which then causes this to happen
What's the best way to continue the alternating color of rows with the hidden row?
Based on this answer to another question I have another method for alternating the row colours, this time taking the hidden row into consideration.
You need to add the following Custom Code to the report
Dim CurrentColour As String
Dim LastRowNumber AS Integer
Function SwitchColour(ThisRowNumber As Integer) As String
If CurrentColour = "" Then
CurrentColour = "Red"
End If
If LastRowNumber = 0 Then
LastRowNumber = ThisRowNumber
End If
If LastRowNumber <> ThisRowNumber Then
' Change the colour
If CurrentColour = "Red" Then
CurrentColour = "Yellow"
Else
CurrentColour = "Red"
End If
LastRowNumber = ThisRowNumber
End If
Return CurrentColour
End Function
This is effectively saving a value for the CurrentColour to persist across the elements in the report. When the SwitchColour function is run it determines if the row number has changed (LastRowNumber <> ThisRowNumber), and if so changes the colour for the cells.
Using this sample data as “DataSet1”
Val | Colour
----+-------
36 | Red
22 | Red
55 | Green
23 | Red
74 | Red
And setting the cells BackgroundColour to
=Code.SwitchColour(RowNumber(“DataSet1”))
Give this result
When you then apply a visibility expression to the Row to negate the value “Green” for example using this expression
=iif(Fields!Colour.Value = "Green", True, False)
The result is like this
Note the colouring still changes, despite there being a record absent from the middle of the table
Is this the sort of behaviour you would like, and is it something you can apply to your project? If you need further assistance, please let me know.
Have you seen this previous answer regarding alternating row colours?
You can use a combination of CountDistinct and RunningValue to calculate a row number in the background that you can then use to set the BackgroundColor property of your rows, assuming there is a unique value to each of the rows you are reporting on.
For example the following expression in the BackgroundColor property of the tablix row
=iif((RunningValue(CountDistinct(Fields!Serial.Value), Sum, "DataSet1") mod 2) = 0, "Tomato", "LimeGreen")
Sets this table to have alternating red and green rows.
I have a doubt here,
I need to show a pie-chart in SSRS, for the student results according to their status(Pass/Fail).......I have only 4 conditions Male-pass,Male-fail,Female-pass,Female-fail,I need to show these things with my own color,
for this am using the switch condition as
=Switch(
((Fields!Gender.Value = "Male")&(Fields!Status.Value="Pass")), "Blue",
((Fields!Gender.Value = "Male")&(Fields!Status.Value="Fail")), "HotPink",
((Fields!Gender.Value = "Female")&(Fields!Status.Value="Fail")), "Orange",
((Fields!Gender.Value = "Female")&(Fields!Status.Value="Pass")),"LimeGreen" )
but in the preview it shows only the default color set, not the customized one, can anyone fix this one...thanks in advance
Try using something like
=IIf((Fields!Gender.Value = "Male") and (Fields!Status.Value="Pass"),"Green",
IIf((Fields!Gender.Value = "Male") and(Fields!Status.Value="Fail"),"Red" ,
IIf((Fields!Gender.Value = "Female") and (Fields!Status.Value="Fail") ,"Blue",
(Fields!Gender.Value = "Female") and (Fields!Status.Value="Pass"),"Yellow","Orange"
,"#00000000"))))
You should be able to get it working using the Switch statement as well. The problem with your expression is that the logical "and" operator in SSRS is And, not ampersand. In SSRS, a single ampersand is used for concatenating strings. So your expression is concatenating the string representation of the two boolean results, resulting in strings like TrueFalse. This should actually giving an error on the Switch evaluation.
A correct Switch statement would be this:
=Switch(
Fields!Gender.Value = "Male" And Fields!Status.Value="Pass", "Blue",
Fields!Gender.Value = "Male" And Fields!Status.Value="Fail", "HotPink",
Fields!Gender.Value = "Female" And Fields!Status.Value="Fail", "Orange",
Fields!Gender.Value = "Female" And Fields!Status.Value="Pass","LimeGreen"
, True, "SomeOtherColor"
)
I've also added an "else" part to the switch in case some records are not covered by the other conditions. If you're 100% sure that won't happen, you can remove the line that starts with "True". But it shouldn't hurt to keep it either.
More info: Pie Chart Techniques (look for Custom Coloring chapter)