MDX计算。将每个成员除以总计

时间:2015-09-08 10:39:04

标签: sql-server ssas mdx olap cube-script

示例数据

我已按以下格式导出OLAP多维数据集(度量[Some Percent]用法为Average over time):

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

公式

我需要创建新的计算成员[Modified Percent],应按以下方式计算:

[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

带样本数据的公式

因此我的样本数据将是:

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

渴望输出

[Modified Percent]应该在以下内容中返回:

             [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脚本

目前我的MDX Script位于[Modified Percent]以下,但[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; 相同

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; 

也尝试了这个,但不幸的是,同样的结果:

{{1}}

看起来分裂部分归还1.你有什么想法如何解决它?如果事情不清楚 - 请问我,我会提供更多细节。

1 个答案:

答案 0 :(得分:1)

问题是计算是在总(全部)级别应用的,其中[Measures].[Net Weight]等于SUM([Vendor Invoice].[Vendor Invoice No].[All], [Measures].[Net Weight]),因此调整系数为1.0

尝试将整个块放在多维数据集的MDX计算脚本中:

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;

这会覆盖总数并告诉它总结孩子,而不是重新计算。