问题
I've been trying to read the previous row value(that displays hourly quantities) on the matrix table to compare it with current row value and highlight the background color to yellow if the difference between them is out of accepted tolerance range.
The matrix table has a combination of grouping on column and row(for only hourly information) that is shown on the picture.
Tried to use the custom code to achieve this, but I'm getting an incorrect result .
I've attached the report design image, and result of that along with the custome code.
Please help me to figure out what is wrong on the logic.
Images: Result: Code: custom code written under report properties -> Code
Public Shared previous as Object
Public Shared current as Object = Nothing
Public Shared Function GetCurrent(Item as Object) as Object
previous=current
current=Item
return current
End Function
Public Shared Function GetPrevious()
return previous
End Function
public Shared Function IsHourlyDiffAboveTolerance(previousValue as Integer, currentValue as Integer, variantPercent as Decimal, variantVolume as Decimal, sortOrder as Integer) As Object
If previousValue = Nothing Then
Return Nothing
Else
Dim diff as Integer
diff = abs(currentValue - previousValue)
Dim tolerance as decimal
tolerance = 0.0
If variantPercent <> 0 Then
tolerance = previousValue * (variantPercent/100)
Else If variantVolume <> 0 Then
tolerance = variantVolume
End If
If diff >= 0 And diff <= tolerance Then
return Nothing
Else
return 1
End If
End If
End Function
Caller Expression:
=IIF(
IsNothing(Code.IsHourlyDiffAboveTolerance(Code.GetPrevious(), Code.GetCurrent(Fields!ExpectedQty.Value),
Fields!VariantPercent.Value, Fields!VariantVolume.Value, Fields!SortOrder.Value)),
"No Color",
"Yellow")
Update: either variant volume or quantity can exist but not both.
Thanks
回答1:
I would use the new analytical function LEAD() and LAG() for SQL Server 2012. These functions access data from a subsequent row (for lead) and previous row (for lag) in the same result set without the use of a self-join. To use these functions with a matrix, you'll probably need to use PARTITION BY
in the clause for the grouping.
Example SQL:
USE AdventureWorks
GO
SELECT
s.SalesOrderID
, s.SalesOrderDetailID,s.OrderQty
, LeadValue = LEAD(SalesOrderDetailID) OVER (ORDER BY SalesOrderDetailID)
, LagValue = LAG(SalesOrderDetailID) OVER (ORDER BY SalesOrderDetailID)
FROM
Sales.SalesOrderDetail s
WHERE
SalesOrderID IN (43670, 43669, 43667, 43663)
ORDER BY
s.SalesOrderID
, s.SalesOrderDetailID
, s.OrderQty
GO
Reference Article
来源:https://stackoverflow.com/questions/49393537/ssrs-matrix-table-reading-previous-row-values