如何使用MDX编写同比和环比
在做BI项目中,会经常提出类似这样的需求:1. 本月比上月的销售额增长了多少?百分比是多少?
2. 本季度第一个月比上季度第一个月的销售额增长了多少?百分比是多少?
环比
需求:本月比上个月的销售额增长了多少个百分点?
MDX语句如下:
WITH MEMBER Measures.[Amount Change Rate] AS
[Measures].[Internet Sales Amount]/
([Measures].[Internet Sales Amount],[Date].[Fiscal].CurrentMember.PrevMember) -1
, FORMAT_STRING = '0%'
SELECT
{[Measures].[Internet Sales Amount], Measures.[Amount Change Rate]} ON 0,
[Date].[Fiscal].[Month].Members ON 1
FROM [Adventure Works]
[Measures].[Internet Sales Amount]/
([Measures].[Internet Sales Amount],[Date].[Fiscal].CurrentMember.PrevMember) -1
, FORMAT_STRING = '0%'
SELECT
{[Measures].[Internet Sales Amount], Measures.[Amount Change Rate]} ON 0,
[Date].[Fiscal].[Month].Members ON 1
FROM [Adventure Works]
结果如下:
上面的输出结果有一个问题,就是第一行数据会发生除0的情况,为了防止这种情况,MDX修改如下:
WITH MEMBER Measures.[Amount Change Rate] AS
IIF(ISEMPTY(([Measures].[Internet Sales Amount],[Date].[Fiscal].CurrentMember.PrevMember)), "",
[Measures].[Internet Sales Amount]/
([Measures].[Internet Sales Amount],[Date].[Fiscal].CurrentMember.PrevMember) -1
), FORMAT_STRING = '0%'
SELECT
{[Measures].[Internet Sales Amount], Measures.[Amount Change Rate]} ON 0,
[Date].[Fiscal].[Month].Members
ON 1
FROM [Adventure Works]
IIF(ISEMPTY(([Measures].[Internet Sales Amount],[Date].[Fiscal].CurrentMember.PrevMember)), "",
[Measures].[Internet Sales Amount]/
([Measures].[Internet Sales Amount],[Date].[Fiscal].CurrentMember.PrevMember) -1
), FORMAT_STRING = '0%'
SELECT
{[Measures].[Internet Sales Amount], Measures.[Amount Change Rate]} ON 0,
[Date].[Fiscal].[Month].Members
ON 1
FROM [Adventure Works]
如果发现0为空,则显示空字符串。输出结果如下:
同比
需求:本季度每个月比上个季度同期销售额增长了多少个百分点?
WITH MEMBER Measures.[Change Amount Rate] AS
[Measures].[Internet Sales Amount]/
( [Measures].[Internet Sales Amount],
Cousin([Date].[Fiscal].CurrentMember, [Date].[Fiscal].CurrentMember.Parent.PrevMember)
) - 1, FORMAT_STRING = '0%'
SELECT {[Measures].[Internet Sales Amount], Measures.[Change Amount Rate]} ON 0,
[Date].[Fiscal].[Month].Members ON 1
FROM [Adventure Works]
[Measures].[Internet Sales Amount]/
( [Measures].[Internet Sales Amount],
Cousin([Date].[Fiscal].CurrentMember, [Date].[Fiscal].CurrentMember.Parent.PrevMember)
) - 1, FORMAT_STRING = '0%'
SELECT {[Measures].[Internet Sales Amount], Measures.[Change Amount Rate]} ON 0,
[Date].[Fiscal].[Month].Members ON 1
FROM [Adventure Works]
运行结果如下:
和环比相同,这里也出现了除0的情况,所以把MDX做如下修改:
WITH MEMBER Measures.[Change Amount Rate] AS
IIF(
([Measures].[Internet Sales Amount],
Cousin([Date].[Fiscal].CurrentMember, [Date].[Fiscal].CurrentMember.Parent.PrevMember)
)=0
, "",
[Measures].[Internet Sales Amount]/
( [Measures].[Internet Sales Amount],
Cousin([Date].[Fiscal].CurrentMember, [Date].[Fiscal].CurrentMember.Parent.PrevMember)
) - 1
), FORMAT_STRING = '0%'
SELECT {[Measures].[Internet Sales Amount], Measures.[Change Amount Rate]} ON 0,
[Date].[Fiscal].[Month].Members ON 1
FROM [Adventure Works]
IIF(
([Measures].[Internet Sales Amount],
Cousin([Date].[Fiscal].CurrentMember, [Date].[Fiscal].CurrentMember.Parent.PrevMember)
)=0
, "",
[Measures].[Internet Sales Amount]/
( [Measures].[Internet Sales Amount],
Cousin([Date].[Fiscal].CurrentMember, [Date].[Fiscal].CurrentMember.Parent.PrevMember)
) - 1
), FORMAT_STRING = '0%'
SELECT {[Measures].[Internet Sales Amount], Measures.[Change Amount Rate]} ON 0,
[Date].[Fiscal].[Month].Members ON 1
FROM [Adventure Works]
输出结果如下: