1

I'm fairly new to MDX queries, and I'm having trouble understanding how I might compute a period over period measurement. For example say I have a metric like revenue, and I'm breaking that revenue out over time say on a Month to Month basis. How would I calculate the change from Month to Month of that revenue as a percent? Now let's say I want to calculate that generically over any period, quarter to quarter, year to year, month to month, or even compare it with identical periods from prior years. 2011 Q1 vs 2012 Q1, 2011 Q2 vs 2012 Q2, etc.

Time Dimension

Schema definition:

<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>
chubbsondubs
  • 37,646
  • 24
  • 106
  • 138

1 Answers1

2

For Period over Period calculations, you will want to look into using the PrevMember function... http://msdn.microsoft.com/en-us/library/ms144719.aspx

Below is a snippet that should give you a good start on Period over Period...

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>>]

For Same Period Prior Year or calculation, you'll want to look into the ParallelPeriod function... http://msdn.microsoft.com/en-us/library/ms145500.aspx

Below is a snippet that should give you a good start on Year over Year...

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>>]
Bill Anton
  • 2,920
  • 17
  • 23
  • I tried translating the first example, but I get an empty PP column. How is Order Date structure to do this? I have a date dimension with multiple hierarchies underneath (Year/Quarter/Month, Year/Month, FY/Quarter/Month, FY/Month). So my member looked like: with member [Measures].[Head Count PP] as ([employmentDate.yearMonth].CurrentMember.PrevMember, [Measures].[Head Count]), but it never seems to return anything. In the table I don't have a timestamp or date column, but Year, FY, Quarter, Month broken out in integer fields. Do I need a true date column for this stuff to work? – chubbsondubs Mar 28 '12 at 16:01
  • can you update your original post with a screenshot of your date dimension so that I can get a better idea of what you're working with? – Bill Anton Mar 28 '12 at 16:23
  • i think that should be fine...the zero-month number is a little odd, but shouldn't matter. Did you place any members from your [employmentDate] dimension on either the column or row axis when you tried to replicate the calculated member? If not, then the calculated member won't return anything...because it references the CurrentMember member-function so there needs to be some context – Bill Anton Mar 30 '12 at 16:37
  • Thank you! I think I must have been missing the inclusion of the employmentDate. I was hoping there was a generic solution for any period, but I think this can be adapted to a generic solution using templates. – chubbsondubs Apr 20 '12 at 03:38