views:

26

answers:

2

I have created a calculated measure that computes a weighted ratio for the current balance of all loans that fall into various risk ratings:

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]

The problem is that the results come back only at the child level, instead of being summed up:

     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

How can I get the sum to show up at the All level instead of (null)?

A: 

It looks like you didn't consider root member when you writing

Ancestor([Risk Rating].[Loan Rating Code].CurrentMember, 1)

When it comes to Root (All Level) above function try to find its ancestor and that's probably why you get null at that level. You should decide what to do when it comes to member "All Level" of [Risk Rating].[Loan Rating Code] dimension.

It looks like you just need to return a constant value "1" when it comes to the root level. If it is so, you should change your query as below;

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]

If you want to return another result when it comes to root level just change the logic at the Then section.

orka
Hmmmm... but at the root (All level), I don't want a constant value "1" - the value there is supposed to be the sum of the children which (usually) will not add up to 1.
David McClelland
What should the ancestor of the root level member value be assumed? you are multiplying the balance by the current LoanRatingCode member value and divide by parent's balance. There is no parent balance when it comes to root level. If we assume it as itself then the result is just "1".
orka
The root level would be the sum of the child values, in this case 4.24.
David McClelland
A: 

I was able to get this to work using IsLeaf() and recursion in my calculated measure:

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]

Now the results look like I wanted:

     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
David McClelland