Search code examples
sql-serverssasmdxolapcube-script

MDX calculation. Divide each member by grand total


SAMPLE DATA

I have exported OLAP cube in following format (Measure [Some Percent] usage is Average over time):

             [Net Weight]   [Some Percent]
             4 387          2,10%
             3 304          1,60%
Grand total: 7 691          1,85% -- Percent is AVG

FORMULA

I need to create new calculated member [Modified Percent] which should be calculated in following:

[Net Weight].[1] / [Net Weight].[Total] * [Some Percent].[1] + 
[Net Weight].[2] / [Net Weight].[Total] * [Some Percent].[2] +
[Net Weight].[n] / [Net Weight].[Total] * [Some Percent].[n] -- can be n rows

FORMULA WITH SAMPLE DATA

So with my sample data will be:

4 387 / 7691 * 2,10 +   |   1,20%
3 304 / 7691 * 1,60     |   0,69%
                    =   |   1,89% -- Sum of percent 

DESIRED OUTPUT

[Modified Percent] should be returned in following:

             [Net Weight]   [Some Percent]             [Modified Percent]
             4 387          2,10%                      1,20%
             3 304          1,60%                      0,69%
Grand total: 7 691          1,85% -- Percent is AVG    1,89%

MDX Script

For now I have MDX Script below, but [Modified Percent] returning the same values as [Some Percent]

CREATE MEMBER CURRENTCUBE.[Measures].[Modified Percent]
 AS ([Measures].[Net Weight] / sum([Vendor Invoice].[Vendor Invoice No].[All],[Measures].[Net Weight]))  * [Measures].[Some Percent], 
FORMAT_STRING = 'Percent', 
NON_EMPTY_BEHAVIOR = { [Net Weight] }, 
VISIBLE = 1;   

Also tried this, but unlucky, the same result too:

CREATE MEMBER CURRENTCUBE.[Measures].[Modified Percent]
 AS ([Vendor Invoice].[Vendor Invoice No].CurrentMember,[Measures].[Net Weight]) / 
     iif(
        ([Vendor Invoice].[Vendor Invoice No].CurrentMember.Parent,[Measures].[Net Weight]) = 0,
        ([Vendor Invoice].[Vendor Invoice No].CurrentMember,[Measures].[Net Weight]),
        ([Vendor Invoice].[Vendor Invoice No].CurrentMember.Parent,[Measures].[Net Weight])
        )
      * [Measures].[Some Percent], 
FORMAT_STRING = 'Percent', 
NON_EMPTY_BEHAVIOR = { [Net Weight] }, 
VISIBLE = 1; 

Looks like divide part returning 1. Have you any ideas how to solve It? If something is unclear - ask me, I will provide more details.


Solution

  • The problem is that the calculation is being applied at the total (All) level, where [Measures].[Net Weight] is equal to SUM([Vendor Invoice].[Vendor Invoice No].[All], [Measures].[Net Weight]) and hence the adjustment factor is 1.0

    Try placing this whole block in the cube's MDX calculation script:

    CREATE MEMBER CURRENTCUBE.[Measures].[Modified Percent]
         AS ([Measures].[Net Weight]
             / sum([Vendor Invoice].[Vendor Invoice No].[All], [Measures].[Net Weight]))
           * [Measures].[Some Percent], 
        FORMAT_STRING = 'Percent', 
        NON_EMPTY_BEHAVIOR = { [Net Weight] }, 
        VISIBLE = 1;  
    
    SCOPE ([Measures].[Modified Percent], [Vendor Invoice].[Vendor Invoice No].[All]);
        this = SUM([Vendor Invoice].[Vendor Invoice No].[All].children, [Measures].[Modified Percent]));
    END SCOPE;
    

    This overrides the total and tells it to sum the children, rather than recalculating.