如何在MDX中创建一段时间段测量?

时间:2012-03-27 18:43:13

标签: mdx olap mondrian

我对MDX查询还不熟悉,而且我无法理解如何计算周期测量的周期。例如,我说我有一个像收入这样的指标,而且我在一个月到一个月的基础上打破了这个收入。我如何计算该收入的月份与月份之间的变化百分比?现在让我们说我想在任何时期,季度到季度,每年,每月,或甚至将它与前几年的相同时期进行比较。 2011年第一季度对2012年第一季度,2011年第二季度对2012年第二季度等等。

Time Dimension

架构定义:

<Dimension type="TimeDimension" highCardinality="false" name="Time">
    <Hierarchy name="yearQuarterMonth" caption="Year/Quarter/Month" hasAll="true" primaryKey="id">
        <Table name="Time"/>
        <Level name="Year" column="year" type="Numeric" uniqueMembers="true" levelType="TimeYears" hideMemberIf="Never"/>
        <Level name="Quarter" column="quarter" type="Numeric" uniqueMembers="false" levelType="TimeQuarters" hideMemberIf="Never"/>
        <Level name="Month" column="month" type="Numeric" ordinalColumn="month" uniqueMembers="false" levelType="TimeMonths" hideMemberIf="Never"/>
        <Level name="Day" column="day" type="Numeric" uniqueMembers="false" levelType="TimeDays" hideMemberIf="Never"/>
    </Hierarchy>
    <Hierarchy name="fiscalYearQuarterMonth" caption="Fiscal Year/Quarter/Month" hasAll="true" primaryKey="id">
        <Table name="Time"/>
        <Level name="Fiscal Year" column="fiscal_year" type="Numeric" uniqueMembers="true" levelType="TimeYears" hideMemberIf="Never"/>
        <Level name="Quarter" column="quarter" type="Numeric" uniqueMembers="false" levelType="TimeQuarters" hideMemberIf="Never"/>
        <Level name="Month" column="month" type="Numeric" ordinalColumn="month" uniqueMembers="false" levelType="TimeMonths" hideMemberIf="Never"/>
        <Level name="Day" column="day" type="Numeric" uniqueMembers="false" levelType="TimeDays" hideMemberIf="Never"/>
    </Hierarchy>
    <Hierarchy name="yearMonth" caption="Year/Month" hasAll="true" primaryKey="id">
        <Table name="Time"/>
        <Level name="Year" column="year" type="Numeric" uniqueMembers="true" levelType="TimeYears" hideMemberIf="Never"/>
        <Level name="Month" column="month" type="Numeric" ordinalColumn="month" uniqueMembers="false" levelType="TimeMonths" hideMemberIf="Never"/>
        <Level name="Day" column="day" type="Numeric" uniqueMembers="false" levelType="TimeDays" hideMemberIf="Never"/>
    </Hierarchy>
    <Hierarchy name="fiscalYearMonth" caption="Fiscal Year/Month" hasAll="true" primaryKey="id">
        <Table name="Time"/>
        <Level name="Fiscal Year" column="fiscal_year" type="Numeric" uniqueMembers="true" levelType="TimeYears" hideMemberIf="Never"/>
        <Level name="Month" column="month" type="Numeric" ordinalColumn="month" uniqueMembers="false" levelType="TimeMonths" hideMemberIf="Never"/>
        <Level name="Day" column="day" type="Numeric" uniqueMembers="false" levelType="TimeDays" hideMemberIf="Never"/>
    </Hierarchy>
</Dimension>

1 个答案:

答案 0 :(得分:2)

对于Period period期间计算,您需要查看使用PrevMember函数... http://msdn.microsoft.com/en-us/library/ms144719.aspx

以下是一个片段,可以为您提供一个良好的开始时间段...

WITH
MEMBER [Measures].[WO Actual Amount PP] AS
    (
         [Order Date].[Calendar].CurrentMember.PrevMember
        ,[Measures].[WO Actual Amount]
    )
    ,FORMAT_STRING = "Currency"
MEMBER [Measures].[Prior Period Growth %] AS
    IIF(
         [Measures].[WO Actual Amount PP] = 0
        ,'N/A'
        ,([Measures].[WO Actual Amount]-[Measures].[WO Actual Amount PP])/[Measures].[WO Actual Amount PP]
    )
    ,FORMAT_STRING = "Percent"
SELECT
    NON EMPTY {
        [Measures].[WO Actual Amount],
        [Measures].[WO Actual Amount PP],
        [Measures].[Prior Period Growth %]
    } ON 0,
    NON EMPTY {
        [Order Date].[Calendar Year].[Calendar Year].Members
    } ON 1
FROM
    [<<cube name>>]

对于相同时段上一年或计算,您将需要查看ParallelPeriod函数... http://msdn.microsoft.com/en-us/library/ms145500.aspx

下面是一个片段,可以为你提供一年一年的良好开端......

WITH
MEMBER [Measures].[WO Actual Amount YoY] AS
    (
         ParallelPeriod(
              [Order Date].[Calendar].[Calendar Year]
             ,1
             ,[Order Date].[Calendar].CurrentMember
         )
        ,[Measures].[WO Actual Amount]
    )
    ,FORMAT_STRING = "Currency"
MEMBER [Measures].[Prior Period Growth %] AS
    IIF(
         [Measures].[WO Actual Amount YoY] = 0
        ,'N/A'
        ,([Measures].[WO Actual Amount]-[Measures].[WO Actual Amount YoY])/[Measures].[WO Actual Amount YoY]
    )
    ,FORMAT_STRING = "Percent"
SELECT
    NON EMPTY {
        [Measures].[WO Actual Amount],
        [Measures].[WO Actual Amount YoY],
        [Measures].[Prior Period Growth %]
    } ON 0,
    NON EMPTY {
        [Order Date].[Calendar Month].[Calendar Month].Members
    } ON 1
FROM
    [<<cube name>>]