如何获得正确汇总的计算量度?

时间:2010-10-18 14:51:00

标签: mdx business-intelligence ssas

我创建了一个计算度量,计算所有贷款当前余额的加权比率,这些贷款属于各种风险评级:

WITH
MEMBER [MEASURES].[Weighted Ratio] AS
    (Measures.[Current Balance] * [Risk Rating].[Loan Rating Code].CurrentMember.member_value) / 
    (
      [Measures].[Current Balance],
         Ancestor([Risk Rating].[Loan Rating Code].CurrentMember, 1)
    ), FORMAT_STRING = "Standard"
SELECT
    {[Measures].[Current Balance], [Measures].[Weighted Ratio]} on COLUMNS,
    [Risk Rating].[Loan Rating Code].MEMBERS ON ROWS
FROM
    [Test Cube]

问题在于结果仅在子级别返回,而不是总结:

     Current Balance     Weighted Ratio
All  $3,245,749,329.46   (null)
0   $661,388.20          0.00
1   $17,341,801.10       0.01
2   $117,188,374.06      0.07
3   $596,477,812.37      0.55
4   $1,266,047,841.51    1.56
5   $986,789,425.72      1.52
6   $119,055,732.16      0.22
7   $133,050,242.69      0.29
8   $9,136,711.65        0.02

如何将总和显示在All级别而不是(null)?

2 个答案:

答案 0 :(得分:0)

在您编写

时,您似乎没有考虑过根成员
Ancestor([Risk Rating].[Loan Rating Code].CurrentMember, 1)

当谈到Root(All Level)上面的函数时,试着找到它的祖先,这可能就是你在那个级别得到null的原因。您应该决定在[Risk Rating].[Loan Rating Code]维度的成员“所有级别”时该怎么做。

看起来你需要在根级别返回一个常量值“1”。如果是这样,您应该更改您的查询,如下所示;

WITH
MEMBER [MEASURES].[Weighted Ratio] AS
( Case

        When [Risk Rating].[Loan Rating Code].CurrentMember.Level.Ordinal = 0
        Then 1
        Else (Measures.[Current Balance] * [Risk Rating].[Loan Rating Code].CurrentMember.member_value) / 
        (
             [Measures].[Current Balance],
             Ancestor([Risk Rating].[Loan Rating Code].CurrentMember, 1)
        )
  End
), FORMAT_STRING = "Standard"
SELECT
    {[Measures].[Current Balance], [Measures].[Weighted Ratio]} on COLUMNS,
    [Risk Rating].[Loan Rating Code].MEMBERS ON ROWS
FROM
    [Test Cube]

如果要在根级别返回其他结果,只需更改Then部分的逻辑。

答案 1 :(得分:0)

我能够使用IsLeaf()和递归计算得到这个:

WITH
MEMBER [Measures].[Weighted Ratio] AS
IIF(
    IsLeaf([Risk Rating].[Loan Rating Code].CurrentMember),
    ([Measures].[Current Balance] * [Risk Rating].[Loan Rating Code].CurrentMember.member_value) / 
    ([Risk Rating].[Loan Rating Code].[All], [Measures].[Current Balance]),
    SUM([Risk Rating].[Loan Rating Code].CurrentMember.Children, [Measures].[Weighted Ratio])
), FORMAT_STRING = "Standard"
SELECT
    {[Measures].[Current Balance], [Measures].[Weighted Ratio]} on COLUMNS,
    [Risk Rating].[Loan Rating Code].MEMBERS ON ROWS
FROM
    [Test Cube]

现在结果看起来像我想要的那样:

     Current Balance     Weighted Ratio
All  $3,245,749,329.46    4.24
0    $661,388.20          0.00
1    $17,341,801.10       0.01
2    $117,188,374.06      0.07
3    $596,477,812.37      0.55
4    $1,266,047,841.51    1.56
5    $986,789,425.72      1.52
6    $119,055,732.16      0.22
7    $133,050,242.69      0.29
8    $9,136,711.65        0.02