MDX 用Ancestors得到Hierarchy中指定Level的值(附带SCOPE用法之一)

Posted 马丁叔叔

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了MDX 用Ancestors得到Hierarchy中指定Level的值(附带SCOPE用法之一)相关的知识,希望对你有一定的参考价值。

需求:用户想要用Excel,对比每月预算和整年预算,需要在两个的Hierarchy浏览时显示同一个值。财年季月日(FYQMD)和财年月日(FYMD)。

 

 自定义hierarchy

 

属性关系(Attribute Relationships)

 
下面我们用Ancestors来拿到当前member所在Hierarchy对应的Financial Year。

CREATE MEMBER CURRENTCUBE.MEASURES.[Full Year Budget] AS NULL;
 
SCOPE(MEASURES.[Full Year Budget]);
    THIS = SUM(
                Ancestors([Posting Date].[Date FYMD].CURRENTMEMBER, [Posting Date].[Date FYMD].[Financial Year]), 
                [Measures].[Budget]);
END SCOPE;

这里Ancestors的第二个参数指定了要得到当前member所在Hierarchy的[Financial Year] level的值。

 得到以下结果:

Budget是用户输入的Amount的SUM。

 

为了让两个维度显示同一个Measure,这里用到了Scope语句,这个东东,就像DAX里面的CALCULATE一样的牛牪犇!!!

先弄两个隐藏的Measure:

--99 - Full Year Budget FYMD 
CREATE MEMBER CURRENTCUBE.MEASURES.[99 - Full Year Budget FYMD] AS NULL, DISPLAY_FOLDER = \'Budget Full Year\'
, ASSOCIATED_MEASURE_GROUP = \'Finance Budget Transactions\',VISIBLE = 0; SCOPE(MEASURES.[99 - Full Year Budget FYMD]); THIS = SUM( Ancestors([Posting Date].[Date FYMD].CURRENTMEMBER, [Posting Date].[Date FYMD].[Financial Year]), [Measures].[Budget]); BACK_COLOR(THIS) = RGB(247,252,185); --light yellow END SCOPE; --99 - Full Year Budget FYQMD CREATE MEMBER CURRENTCUBE.MEASURES.[99 - Full Year Budget FYQMD] AS NULL, DISPLAY_FOLDER = \'Budget Full Year\'
, ASSOCIATED_MEASURE_GROUP = \'Finance Budget Transactions\',VISIBLE = 0; SCOPE(MEASURES.[99 - Full Year Budget FYQMD]); THIS = SUM( Ancestors([Posting Date].[Date FYQMD].CURRENTMEMBER, [Posting Date].[Date FYQMD].[Financial Year]), [Measures].[Budget]); BACK_COLOR(THIS) = RGB(247,252,79); --79 END SCOPE;

再用Scope把它们包起来,显示为[Full Year Budget]:

--Full Year Budget
CREATE MEMBER CURRENTCUBE.MEASURES.[Full Year Budget] AS NULL, DISPLAY_FOLDER = \'Budget Full Year\',  
ASSOCIATED_MEASURE_GROUP = \'Finance Budget Transactions\',VISIBLE = 1; SCOPE(MEASURES.[Full Year Budget]); --Fiscal YTD FYQMD SCOPE([Posting Date].[Date FYQMD].[Financial Year].MEMBERS); THIS = Measures.[99 - Full Year Budget FYQMD]; END SCOPE; SCOPE([Posting Date].[Date FYQMD].[Quarter].MEMBERS); THIS = Measures.[99 - Full Year Budget FYQMD]; END SCOPE; SCOPE([Posting Date].[Date FYQMD].[Month].MEMBERS); THIS = Measures.[99 - Full Year Budget FYQMD]; END SCOPE; SCOPE([Posting Date].[Date FYQMD].[Day].MEMBERS); THIS = NULL; END SCOPE; --Fiscal YTD FYMD SCOPE([Posting Date].[Date FYMD].[Financial Year].MEMBERS); THIS = Measures.[99 - Full Year Budget FYMD]; END SCOPE; SCOPE([Posting Date].[Date FYMD].[Month].MEMBERS); THIS = Measures.[99 - Full Year Budget FYMD]; END SCOPE; SCOPE([Posting Date].[Date FYMD].[Day].MEMBERS); THIS = NULL; END SCOPE; END SCOPE;

 

最终得到结果如下:

左边是FYQMD,右边是FYMD。(可以看到右边的Budget YTD没有用Scope这个trick--基于FYQMD建的Measure,没有显示正确,坏了,哈哈。)

 基于上面可以定义各种variance,和variance %。

 

 

 

 

例子:https://www.purplefrogsystems.com/blog/2010/08/calculate-run-rate-full-year-projection-in-mdx/

http://stackoverflow.com/questions/16545745/mdx-calculate-sum-at-a-fixed-level-in-hierarchy

https://bidrone.wordpress.com/2013/04/25/mdx-ancestorsumytd-function-target-calculation-for-different-time-levels/

以上是关于MDX 用Ancestors得到Hierarchy中指定Level的值(附带SCOPE用法之一)的主要内容,如果未能解决你的问题,请参考以下文章

MDX 向下钻取查询生成

MDX百分比计算方法

如何使用蒙德里安聚合单个维度的多个层次结构?

最近公共祖先 Lowest Common Ancestors

mdx 规范?

MDX 处理负值