MDX常见计算方法(百分比/分配/平均值/基于时间的计算)

本文介绍了一些经常编写的MDX语句的写法。以下MDX语句可以在SSAS的示例库:Adventure Works中运行。

例子模型
以下的MDX中用到的Hierarchy如下:

百分比
1)某个子项占总体的百分比。比如:每种Product的销售额占所有Product销售额的百分比。
WITH  MEMBER  [ Measures ] . [ Sale Amount Ratio ]   AS  
' [Measures].[Internet Sales Amount]/([Measures].[Internet Sales Amount], [Product].[Product Categories].[All]) '  , FORMAT_STRING  =   ' 0.00% '    
SELECT  
{
[ Measures ] . [ Internet Sales Amount ] [ Measures ] . [ Sale Amount Ratio ] ON   0 ,
NON EMPTY 
[ Product ] . [ Product Categories ] . [ Product Name ] .Members  ON   1
FROM   [ Adventure Works ]

2)某个子项占其父项的百分比。比如:每种Product的销售额占其所属的SubCategory销售额的百分比。

WITH  MEMBER  [ Measures ] . [ Sale Amount Ratio ]   AS  
' [Measures].[Internet Sales Amount]/
([Measures].[Internet Sales Amount], [Product].[Product Categories].CurrentMember.Parent)
'
, FORMAT_STRING 
=   ' 0.00% '    
SELECT  
{
[ Measures ] . [ Internet Sales Amount ] [ Measures ] . [ Sale Amount Ratio ] ON   0 ,
NON EMPTY CROSSJOIN(
[ Product ] . [ Subcategory ] . [ Subcategory ] .Members, 
    [ Product ] . [ Product Categories ] . [ Product Name ] .Members)  ON   1
FROM   [ Adventure Works ]

3)某个子项占其祖先的百分比。比如:每种Product的销售额占其所属的Category销售额的百分比。

WITH  MEMBER  [ Measures ] . [ Sale Amount Ratio ]   AS  
' [Measures].[Internet Sales Amount]/
([Measures].[Internet Sales Amount], 
ANCESTOR([Product].[Product Categories].CurrentMember, [Product].[Product Categories].[Category]))
'
, FORMAT_STRING 
=   ' 0.00% '    
SELECT  
{
[ Measures ] . [ Internet Sales Amount ] [ Measures ] . [ Sale Amount Ratio ] ON   0 ,
NON EMPTY CROSSJOIN(
[ Product ] . [ Category ] . [ Category ] .Members,  [ Product ] . [ Product Categories ] . [ Product Name ] .Members)  ON   1
FROM   [ Adventure Works ]

分配、分摊数量
1)根据一个Measure值来分配数量。比如:按照每种Product占总体的销售额多少来分摊成本。

WITH  MEMBER  [ Measures ] . [ Product Cost ]   AS  
' ([Measures].[Internet Total Product Cost], [Product].[Product Categories].[All])*
[Measures].[Internet Sales Amount]/
([Measures].[Internet Sales Amount], [Product].[Product Categories].[All])
'   
, FORMAT_STRING 
=   ' 0.00 '  
SELECT  
{
[ Measures ] . [ Internet Sales Amount ] [ Measures ] . [ Product Cost ] ON   0 ,
NON EMPTY 
[ Product ] . [ Product Categories ] . [ Product Name ] .Members  ON   1
FROM   [ Adventure Works ]

2)根据一个Hierarchy来分配数量。比如:在Product Hierarchy中计算每种Category的成本的时候,可以根据每种Category下有多少个产品来进行分配。

WITH  MEMBER  [ Measures ] . [ Product Cost ]   AS  
' ([Measures].[Internet Total Product Cost], [Product].[Product Categories].[All])/
Count(
    Descendants (
        [Product].[Product Categories].CurrentMember,
        [Product].[Product Categories].[Product Name],
        SELF
    ),
    INCLUDEEMPTY
)
'   
, FORMAT_STRING 
=   ' 0.00 '  
SELECT  
{
[ Measures ] . [ Internet Sales Amount ] [ Measures ] . [ Product Cost ] ON   0 ,
NON EMPTY 
[ Product ] . [ Product Categories ] . [ Category ] .Members  ON   1
FROM   [ Adventure Works ]

平均值
1)简单平均值。比如:计算一个月中每天平均的销售额是多少。

WITH  MEMBER Measures. [ Avg Gross Profit Margin ]   AS
   
[ Measures ] . [ Internet Sales Amount ] /
   
COUNT (Descendants( [ Ship Date ] . [ Fiscal ] .CurrentMember,  [ Ship Date ] . [ Fiscal ] . [ Date ] ), INCLUDEEMPTY)   

SELECT
  {
[ Measures ] . [ Internet Sales Amount ] , Measures. [ Avg Gross Profit Margin ] ON  COLUMNS,
  
[ Ship Date ] . [ Fiscal ] . [ month ] .Members  ON  ROWS
FROM   [ Adventure Works ]

2)加权平均值。没有想到好的例子。

基于时间的计算
1)同比和环比。比如:今年每月的销售额和去年同期相比的变化
这里要补充的是,在同比MDX中,采用COUSIN或ParallelPeriod都可以,但是采用ParallelPeriod更好一些。

2)累计到当前的统计。比如:得到一年中每一个月的累计销售额。

WITH  MEMBER Measures. [ Additive Internet Sales Amount ]   AS
   
SUM (
       PeriodsToDate(
[ Ship Date ] . [ Fiscal ] . [ Fiscal Year ] , [ Ship Date ] . [ Fiscal ] .CurrentMember), 
       
[ Measures ] . [ Internet Sales Amount ]
   ) 
SELECT
  {
[ Measures ] . [ Internet Sales Amount ] , Measures. [ Additive Internet Sales Amount ] ON  COLUMNS,
  
[ Ship Date ] . [ Fiscal ] . [ month ] .Members  ON  ROWS
FROM   [ Adventure Works ]

3)移动平均值。比如:计算一种Category过去三个月的平均销售额合计。

WITH  MEMBER Measures. [ Average Internet Sales Amount ]   AS
   
AVG (LastPeriods( 3 [ Date ] . [ Calendar ] .CurrentMember), 
       
[ Measures ] . [ Internet Sales Amount ]
SELECT
  {
[ Measures ] . [ Internet Sales Amount ] , Measures. [ Average Internet Sales Amount ] ON  COLUMNS,
 NON EMPTY (
[ Product ] . [ Product Categories ] . [ Category ] .Members, 
    DESCENDANTS(
[ Date ] . [ Calendar ] . [ Calendar Year ] . & [ 2002 ] [ Date ] . [ Calendar ] . [ Month ] , SELF)
  ) 
ON  ROWS
FROM   [ Adventure Works ]
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值