码迷,mamicode.com
首页 > 其他好文 > 详细

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

时间:2017-03-09 18:39:31      阅读:226      评论:0      收藏:0      [点我收藏+]

标签:attribute   用户输入   rgb   question   run   stack   div   ytd   uart   

需求:用户想要用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用法之一)

标签:attribute   用户输入   rgb   question   run   stack   div   ytd   uart   

原文地址:http://www.cnblogs.com/lizardbi/p/6526899.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!