Warm tip: This article is reproduced from stackoverflow.com, please click
mdx olap olap-cube calculation mdx-query

MDX

发布于 2020-03-29 20:58:29

I want to compare actual sales values with sales values of last year. The difficulty in this comparison is the compliance of the week days and thereby of the leap-year.

Example on day level:

2016-02-04 (thursday): actual sales: 580,- last year sales: 1.008,-

comparison with

2015-02-05 (thursday): actual sales: 1.008,-

So i want to compare the same week days in the month and not only the same dates.

Example on month level:

2016: (leap-year) 01.02.2016 - 29.02.2019 (february 2016) actual Sales: 19.300,- : last year Sales value: 19.000,-

comparison with

2015: (no leap year) 02.02.2015 - 02.03.2015 (february 2015 on week day logic) actual sales value: 19.000,-

I want not only compare February 2016 with february 2015 rather exactly the week day sales values summed.

I tried to write this calculation with an date calculation dimension and it works but only on the day level. Name of the calculation dimension: Date Calculations Week Day name of the member: ComparisonWD

SCOPE ( 
    [Date].[Year - Quarter - Month - Date].MEMBERS,
    [Date].[Date].MEMBERS );                    


( [Date Calculations Week Day].[ComparisonWD].[Previous Year], 
      [Date Calculations Week Day].[AggregationWD].Members ) 
          = ( [Date Calculations Week Day].[ComparisonWD].DefaultMember,
              ParallelPeriod( [Date].[Year - Quarter - Month - Date].[Date],
                              364,
                              [Date].[Year - Quarter - Month - Date].CurrentMember ) );            
END SCOPE;

Result 2015:

enter image description here

Result 2016:

enter image description here

It works on day level, but as you seen not on the month and not onthe year level.

enter image description here

How can I achieve this?

Questioner
Alexo
Viewed
15
Alexo 2020-01-31 22:26

I got the solution:

Cube Calculation Code for this problem:

// ------------------------------------------------------------------------
//
//    Comparison Week Day - Date.Calendar
//
// ------------------------------------------------------------------------
SCOPE ( 
    [Date].[Year - Quarter - Month - Date].MEMBERS,
    [Date].[Date].MEMBERS );                    

    ///////////////////////////////////////////////////////////////////////////////////////
    ( [Date Calculations Week Day].[ComparisonWD].[Previous Year], 
      [Date Calculations Week Day].[AggregationWD].Members ) 
          = SUM({ParallelPeriod([Date].[Year - Quarter - Month - Date].[Date], 364, Descendants( [Date].[Year - Quarter - Month - Date].CurrentMember , [Date].[Year - Quarter - Month - Date].[Date]).item(0)) :
                     Parallelperiod ([Date].[Year - Quarter - Month - Date].[Date], 364,  Descendants( [Date].[Year - Quarter - Month - Date].CurrentMember , [Date].[Year - Quarter - Month - Date].[Date]).item((Descendants( [Date].[Year - Quarter - Month - Date].CurrentMember , [Date].[Year - Quarter - Month - Date].[Date])).Count - 1))}, [Date Calculations Week Day].[ComparisonWD].DefaultMember );            
END SCOPE;    


SCOPE ([Date Calculations Week Day].[ComparisonWD].[Diff. Over Previous Year]);                   
    THIS = IIF( IsEmpty( [Date Calculations Week Day].[ComparisonWD].DefaultMember ) 
                         OR IsEmpty( [Date Calculations Week Day].[ComparisonWD].[Previous Year] ),
                NULL,
                [Date Calculations Week Day].[ComparisonWD].DefaultMember 
                - [Date Calculations Week Day].[ComparisonWD].[Previous Year] );                   
    NON_EMPTY_BEHAVIOR(THIS) = [Date Calculations Week Day].[ComparisonWD].DefaultMember;                   
    FORE_COLOR(THIS) = IIF( [Date Calculations Week Day].[ComparisonWD].[Diff. Over Previous Year] < 0, 255, 0);                     // 255 = RED
END SCOPE;                   

SCOPE ([Date Calculations Week Day].[ComparisonWD].[Diff. % Over Previous Year]);                   
    THIS = IIF( IsEmpty( [Date Calculations Week Day].[ComparisonWD].[Diff. Over Previous Year] ) 
                         OR IsEmpty( [Date Calculations Week Day].[ComparisonWD].[Previous Year] ),
                NULL,
                [Date Calculations Week Day].[ComparisonWD].[Diff. Over Previous Year]
                / [Date Calculations Week Day].[ComparisonWD].[Previous Year] );                   
    NON_EMPTY_BEHAVIOR(THIS) = [Date Calculations Week Day].[ComparisonWD].DefaultMember;                   
    FORMAT_STRING(THIS) = 'Percent';                   
    FORE_COLOR(THIS) = IIF( [Date Calculations Week Day].[ComparisonWD].[Diff. % Over Previous Year] < 0, 255, 0);                     // 255 = RED
END SCOPE;                   

///////////////
// Tuple (All years, All Months) is the default number (keeps compatibility with OWC11)
( [Date].[Year].[All], [Date].[MonthYear].[All],
  Except( [Date Calculations Week Day].[ComparisonWD].[ComparisonWD].MEMBERS, [Date Calculations Week Day].[ComparisonWD].DefaultMember ) ) = [Date Calculations Week Day].[ComparisonWD].DefaultMember;   

Now with summed values on every level:

enter image description here

With this Date Calculations Week Day Dimension you can show for every Measure the Prev Year Values on Week day logic.