1
votes

Let's assume i have easy table with sales data like:

  • id shop
  • id product
  • date
  • amount

Can you help me to write MDX query for calculated member to get current period sales ratio to same period of previous year?

For example, if month or quarter selected as one of dimensions.

2
I have no idea at all how to do that in cubes. On relational databases i was doing 2 separate reports to temporary table and then merging themOleksandr Voytsekhovskyy
In order to write any Query, it's needed to know the relational schema. Please, post the entire schema involved.Paolo Maresca

2 Answers

0
votes

Let's assume you've a [Time] dimension with [Year], [Month] and [Day] levels.

If

SELECT
    [Time].[Jan 2015]:[Time].[Dec 2015] on 0,
    [Measures].[Sales] on 1
FROM
    [Cube]

Returns the sales for all months of 2015. We can add a calculated measure to get ratio :

  WITH
    MEMBER [Sales Ratio] AS DivN( 
                [Sales], 
               ( ParallelPeriod( [Time].[Year], 1, [Time].current ), [Sales] )
  SELECT
    [Time].[Jan 2015]:[Time].[Dec 2015] on 0,
    {[Sales],[Sales Ratio]} on 1
  FROM
    [Cube]

DivN is icCube specific and allows for dividing being 'empty' safe.

ParallelPeriod is a standard MDX function, that returns previous years month. You could also use Lag(-12), that is 'travelling' backwards in a level 12 times.

current (aka Currentmember) is also standard MDX and allows for retrieving the current value of a hierarchy/ dimension.

In icCube I'd add a function to navigate to the previous year so you can reuse it (and fix one if needed). Like :

  WITH
    FUNCTION timePrevYear(t_) AS ParallelPeriod( [Time].[Year], 1, t_ )
    MEMBER [Sales Ratio] AS DivN( 
                [Sales], 
                ( timePrevYear( [Time].current ), [Sales] )
  SELECT
    [Time].[Jan 2015]:[Time].[Dec 2015] on 0,
    {[Sales],[Sales Ratio]} on 1
  FROM
    [Cube]

It's going to be a bit too much but eventually you could add this kind of calculations in what we call in MDX Utility or Stats dimension, so you can even let the end-user select this in a dropdown from a reporting tools. More on this here.

0
votes

In the models I create for my clients, I sometimes take another route as ic3 has suggested:

Especially when there will be lots of additional calculations on top of these (e.g. year-to-date, inception-to-date, month-to-date etc).

This is: load the same facts data again, but set as the "load date" the "date" - 1 year (e.g. mySQL: DATE_ADD(,INTERVAL -1 YEAR).

Advantages:

  • drill through on history is possible
  • lots of formulas can be added "on top" of these, you always know that the basics are ok