How to create Alternative Row Background colors in SSRS for values in a group - sql

I am using this expression to get alternative colors for my row group:
=iif(RunningValue(Fields!Status_Reason.Value,CountDistinct,Nothing) Mod 2, "LightBlue", "White")
And everything works fine except for some rows:
I am assuming its because of some values are "-", which are 0.
What could be the work around in this situation?
This is my groups:

I use some VB code for Alternating the row color. It's a little more work to set up at first, but it always works right and you can re-use the code in other reports by copying the VB code.
Expression:
=code.AlternateColor("AliceBlue", "White", 1, 1)
=code.AlternateColor("AliceBlue", "White", 0, 1)
The first column should have the first expression - the first 1 in the argument tells it to change color. The remaining columns use the second expression with the 0 indicating that the color won't change.
VB CODE:
Private bOddRow(10) As Boolean
Function AlternateColor(ByVal OddColor As String, ByVal EvenColor As String, ByVal Toggle As Boolean, ByVal Type AS INTEGER) As String
If Toggle Then bOddRow(Type) = Not bOddRow(Type)
If bOddRow(Type) Then
Return OddColor
Else
Return EvenColor
End If
End Function
If you have multiple levels of grouping in one table, you would change the second number of the expression so the rows are unique for each group. In the below example, the main grouping is colored in white and AliceBlue and the sub group is whitesmoke and a lighter blue.

Related

IIF false expression impacting truthiness?

I am developing an SSRS tablix report and struggling to remove some error messages from certain cells. The Expression in the cell is as follows:
=Iif(IsNothing(First(Fields!Details.Value)) , "", Join(Code.RemoveDuplicates(LookupSet(Fields!studyTitle.Value, Fields!studyTitle.Value, Fields!Link.Value ,"DataSet1")), vbCrLf ))
The custom function that's referenced is as follows:
Public Shared Function RemoveDuplicates(ByVal items As Object()) As String()
System.Array.Sort(items)
Dim k As Integer = 0
For i As Integer = 0 To items.Length - 1
If i > 0 AndAlso items(i).Equals(items(i - 1)) Then
Continue For
End If
items(k) = items(i)
k += 1
Next
Dim unique As [String]() = New [String](k - 1) {}
System.Array.Copy(items, 0, unique, 0, k)
Return unique
End Function
The problem I'm encountering is that cells in the output will display "#ERROR" if they are being run through the custom function with multiple blank values.
The goal of the initial IIF statement is to test those rows first to make sure that the function is only applied to those rows that have values, and the other cells should just be blank. The problem is that I'm still seeing errors for those cells, as if the false condition is applied anyway.
However, if I edit my IIF statement to have the same test condition but different true-part and false-part i.e.:
Iif(IsNothing(First(Details.Value)), "true", "false"))
Then suddenly I see exactly the rows I would expect to see marked true and false. If this is the case then I can't figure out where the errors are coming from in my initial statement.
I know the issue has to be somewhere with the RemoveDuplicates function because when I take that part out and just Join my duplicated values then I see no errors, and the false rows are the same as identified above.
Does anyone see something I'm missing that is generating this error or causing this seemingly inconsistent behavior from the IIf function?
Use If instead of IIf. The If construct shortcuts the evaluation, while the IIf evaluates all expressions.
=If(IsNothing(First(Fields!Details.Value)) , "", Join(Code.RemoveDuplicates(LookupSet(Fields!studyTitle.Value, Fields!studyTitle.Value, Fields!Link.Value ,"DataSet1")), vbCrLf ))
This is only available in VB.net and is not available in VBA. In VBA, you would use a standard If-Then-Else construct.

Find smallest number in calculated field

In a MSAccess report, I have fields like: CurrentHours, Insp1DueTime, Insp2DueTime...Inspn3DueTime, etc. I want a calculated field which outputs the smallest value of (Insp1DueTime-CurrentHours), (Insp2DueTime-Currenthours), (Insp3DueTime-CurrentHours), etc.
Is there a VBA command which will do this, similar to 'smallestvalue'((currenthours-insp1duetime), (Currenthours-insp2duetime),...(currenthour-InspnDueTime))
Nope, but you can make one really easily, using a ParamArray:
Public Function SmallestValue(ParamArray Values())
Dim Value
For Each Value In Values
If IsEmpty(SmallestValue) Or SmallestValue > Value Then
SmallestValue = Value
End If
Next
End Function
A paramarray takes any input, and puts it into an array. You iterate over that array, and test if things are larger or smaller.
This works with any input, e.g.: SmallestValue(2,1,3,4) returns 1, SmallestValue(Now(), Date(), #1/1/2001#) returns #1/1/2001#, SmallestValue("banana", "apple", "pear") returns "apple"

Alternating row color expression in SSRS matrix not working correctly

In the only row group I am trying to get a alternate row color with the following expression:
Expression for background color : =IIf( RunningValue (Fields!SP.Value, CountDistinct, Nothing) MOD 2, "White", "blue")
SQL code:
select
ROW_NUMBER() OVER (ORDER BY DataDate) AS SSRSRowNumber
,datepart(dw,datadate) SSRSDateFilter
,DataDate
,SP
,sum(TMI) as TotalCI
from table
where DataDate>GETDATE()-20
group by DataDate,SP
order by 1, 2
The result is the picture below, what's wrong in the expression listed above?
Edit-:Solution
The missing dates in your data is causing the issues with the background row color not working correctly.
You can waste lots of time trying to make your query work.
Or you could just use the Alternating Row Color function.
Private bOddRow As Boolean
'*************************************************************************
' -- Display green-bar type color banding in detail rows
' -- Call from BackGroundColor property of all detail row textboxes
' -- Set Toggle True for first item, False for others.
'*************************************************************************
Function AlternateColor(ByVal OddColor As String, _
ByVal EvenColor As String, ByVal Toggle As Boolean) As String
If Toggle Then bOddRow = Not bOddRow
If bOddRow Then
Return OddColor
Else
Return EvenColor
End If
End Function
For the first column that controls the color:
=Code.AlternateColor("AliceBlue", "White", True)
For the remaining columns, don't toggle with the third argument:
=Code.AlternateColor("AliceBlue", "White", False)
You may need to switch the colors in the first column in a matrix.
SSRS Alternating row color issues when some rows are not visible
This has occurred for me where I don't have an intersection between my row groups and column groups.
Example:
- I have stored procedures 1 and 2
On 1/1, both 1 and 2 ran, so I record them
On 1/2, only 1 need to run, so I only record 1
On 1/3, they both ran again, so I recorded both
I'm going to have a messed up looking cell on 1/2 for sp 2 (the color won't change) because SSRS can't calculate a new value for the running total (it's still 1). On 3, it recognizes a value and gets that the running value is now 3, so coloring resumes as expected.
To solve this, you could perform a cross apply to get the cartesian product of all dates and SPs so that every distinct date has a row for every distinct date and store this in a temp table. Store your current query in a temp table or CSV and join onto your cross apply based query. This will ensure no missed cells. Note, this will have a performance impact so test accordingly (it is most likely negligible).
Let me know if you need help writing the queries.

Use formula for multiplication?

I have two fields on a record ("Qty" and "Harga").
How do I multiply the two and save result into another field in a ListView?
The ListView control has no built-in ability to perform calculations for you, like a spreadsheet. It just displays whatever data you give it to display. If you want it to display the product of a multiplication equation, you will need to do that calculation yourself in the code and then add the result to the ListView column. For instance:
Public Sub AddItem(description As String, total As Integer, count As Integer)
Dim i As ListViewItem = ListView1.Items.Add(description)
i.SubItems.Add(total.ToString())
i.SubItems.Add(count.ToString())
Dim product As Integer = total * count
i.SubItems.Add(product.ToString())
End Sub

rdlc skip hidden rows on condition

I know this may sound trivial but I just can't find an answer to it.
I have a rdlc report in which I like to alternate row background color and for this I've used the following formula:
=iif(RowNumber(Nothing) Mod 2, "#e5e5e5", "White")
I also need to hide some rows and for this I use the following formula:
= Fields!MeanAeb.Value <> ""
where MeanAeb is a field in my report. My problem is that rowNumber also counts the hidden rows, so my table may have two consecutive rows with the same background. is there a way to take only visible rows into account?
So if anyone has the same problem, I have an answer;
in the Code section of your ReportProperties add the following
Dim customRowNumber as Integer = 0
Dim previousRowNumber as integer = 0
Function CustomRowCounter(conditionToTest as Boolean, rowNumbner as Integer) as Integer
if(conditionToTest and rowNumbner <> previousRowNumber)
customRowNumber = customRowNumber + 1
previousRowNumber = rowNumbner
end if
return customRowNumber
End Function
then on the background field in your column properties add this condition:
=iif(Code.CustomRowCounter(Fields!MeanAeb.Value="",RowNumber(nothing)) Mod 2, "#e5e5e5", "White")
this is nice because you can add any condition you like in place of Fields!MeanAeb.Value="". Just remember to use the inverse of the condition in your rowVisibility field, otherwise you may cause strange effects.
Oh and if you want a chess board look to your report just drop the previousRowNumber :)