计算SSRS中的百分比

时间:2019-05-07 16:17:38

标签: reporting-services ssrs-2012 reportbuilder3.0 percentile

我们需要基于共享数据集在SSRS报告中计算百分位(第95位和第99位),该数据集将以15分钟为间隔返回汇总数据。百分位数必须是整个交易日的价格,但要根据合同。

为了论证,可以说这是我们的数据:

select
    'Test' as [ContractName],
    1 as [Value]

union all

select
    'Test' as [ContractName],
    3 as [Value]

union all

select
    'Test' as [ContractName],
    4 as [Value]

union all

select
    'Test' as [ContractName],
    6 as [Value]

union all

select
    'Test' as [ContractName],
    7 as [Value]

union all

select
    'Test' as [ContractName],
    36 as [Value]

union all

select
    'Test' as [ContractName],
    56 as [Value]

union all

select
    'Test' as [ContractName],
    798 as [Value]

union all

select
    'Test' as [ContractName],
    324 as [Value]

union all

select
    'Test' as [ContractName],
    456 as [Value]

union all

select
    'Test' as [ContractName],
    657 as [Value]

union all

select
    'Test' as [ContractName],
    658 as [Value]

这几乎是排序的(那是我创建时所用的),但是数据本身不在实际应用程序中。

我需要能够在不更改SQL的情况下从该数据集中获取百分比,因为它是用于合同报告的共享数据集,并且无法更改。共享数据集用于确保整个报告套件的一致性。因此,解决方案必须在最终报告输出的tablix中。

我看过SSRS expression - query from dataset, with group,发现它与Find the median of a calculated field in SSRS 2012https://blogs.msdn.microsoft.com/robertbruckner/2008/07/21/using-group-variables-in-reporting-services-2008-for-custom-aggregation/具有相同的基本用法,并且需要一些隐藏的行和组变量,以后不容易维护/编辑。

1 个答案:

答案 0 :(得分:3)

可以将以下自定义代码添加到报告中:

Public Shared Function Centile(ByVal items As Object(), ByVal k As Decimal) As Decimal
    If items Is Nothing Then
        Return Nothing
    End If

    Dim counter as Decimal = items.Length
    If counter = 0 Then
        Return 0
    End If

    System.Array.Sort(items)

    Dim Position As Decimal = (k * (counter - 1))

    Dim FirstIndex As Integer = Floor(Position)
    Dim SecondIndex As Integer = Ceiling(Position)

    Dim Remainder As Decimal = Position - FirstIndex

    Dim FirstValue As Integer = items(FirstIndex)
    Dim SecondValue As Integer = items(SecondIndex)


    Return (FirstValue + (Remainder * (SecondValue - FirstValue)))

End Function

可以在表达式中使用lookupset()来调用哪个

=Code.Centile(lookupset(Fields!ContractName.Value, Fields!ContractName.Value, Fields!Value.Value, "DS_Centile_LKP"), 0.95)

这会通过在ContractName字段中查找来回调自身以从同一数据集中获取值。 lookupset()返回一个要在此函数中排序的数组,而不是到处都有变量和东西-这是显示值的tablix文本框中的一个简单函数。 它还允许为您要使用的百分位数使用不同的值-在上面的示例中为95%,即0.95。