MDX的几种百分比的计算方法

实际应用中,特别是一些分析报表,经常需要计算数据百分比、份额、平均值、累计占比等,在数据仓库飞速发展的今天,我们需要了解一些经常编写的MDX语句的写法,以满足工作中的需要。

由于自己写的带有很大的个性特点,不适合作为标准案例来研究,下面就通过借鉴微软示例库来介绍一下这些常用的MDX写法,以下MDX语句可以在SSAS的示例库:Adventure Works中运行。

例子模型
以下的MDX中用到的Hierarchy如下:
MDX之百分比MDX之百分比
百分比
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销售额的百分比。

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

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

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

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

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

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

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

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

MDX之百分比WITH MEMBER Measures.[Avg Gross Profit Margin] AS
MDX之百分比   [Measures].[Internet Sales Amount]/
MDX之百分比   COUNT(Descendants([Ship Date].[Fiscal].CurrentMember, [Ship Date].[Fiscal].[Date]), INCLUDEEMPTY)   
MDX之百分比
MDX之百分比SELECT
MDX之百分比  {[Measures].[Internet Sales Amount], Measures.[Avg Gross Profit Margin]} ON COLUMNS,
MDX之百分比  [Ship Date].[Fiscal].[month].Members ON ROWS
MDX之百分比FROM [Adventure Works]

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

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

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

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

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

MDX之百分比WITH MEMBER Measures.[Average Internet Sales Amount] AS
MDX之百分比   AVG(LastPeriods(3, [Date].[Calendar].CurrentMember), 
MDX之百分比       [Measures].[Internet Sales Amount]) 
MDX之百分比SELECT
MDX之百分比  {[Measures].[Internet Sales Amount], Measures.[Average Internet Sales Amount]} ON COLUMNS,
MDX之百分比 NON EMPTY ([Product].[Product Categories].[Category].Members, 
MDX之百分比    DESCENDANTS([Date].[Calendar].[Calendar Year].&[2002], [Date].[Calendar].[Month], SELF)
MDX之百分比  ) ON ROWS
MDX之百分比FROM [Adventure Works]

相关文章:

  • 2021-12-19
  • 2021-12-28
  • 2022-12-23
  • 2022-03-10
  • 2021-12-22
  • 2021-11-27
  • 2022-12-23
猜你喜欢
  • 2021-08-12
  • 2022-12-23
  • 2022-01-11
  • 2022-12-23
  • 2021-07-20
  • 2022-12-23
  • 2021-12-25
相关资源
相似解决方案