0

I have a problem with the following query:

Month to month comparison. Compare the sales of a current month with three other numbers: a. The previous month. b. The average of the previous three months. c. The average of the three month period before the previous 3 months.

Here is the structure of the database/cube: enter image description here

WITH MEMBER [Measures].[Current Month] AS 
    [Measures].[PIZZA SALES Count]
MEMBER [Measures].[Previous Month] AS
        AVG({[PIZZA SALES].[Time ID].Children * [TIME].    [Month].CurrentMember.lag(1)},[Measures].[PIZZA SALES Count]])

MEMBER [Measures].[3 Months] AS
        ((AVG({[PIZZA SALES].[Time ID].Children * [TIME].    [Month].CurrentMember.lag(1)},[Measures].[PIZZA SALES Count]]) +
         AVG({[PIZZA SALES].[Time ID].Children * [TIME].    [Month].CurrentMember.lag(2)},[Measures].[PIZZA SALES Count]]) +
         AVG({[PIZZA SALES].[Time ID].Children * [TIME].    [Month].CurrentMember.lag(3)},[Measures].[PIZZA SALES Count]]))/3)   

MEMBER [Measures].[6 Months] AS
        ((AVG({[PIZZA SALES].[Time ID].Children * [TIME].    [Month].CurrentMember.lag(4)},[Measures].[PIZZA SALES Count]]) +
         AVG({[PIZZA SALES].[Time ID].Children * [TIME].    [Month].CurrentMember.lag(5)},[Measures].[PIZZA SALES Count]]) +
         AVG({[PIZZA SALES].[Time ID].Children * [TIME].    [Month].CurrentMember.lag(6)},[Measures].[PIZZA SALES Count]]))/3)

    SELECT
    {
        [Measures].[Current Month],
        [Measures].[Previous Month],
        [Measures].[3 Months],
        [Measures].[6 Months]
    }
    ON COLUMNS,
    {
        [TIME].[Month].&[12]
    } ON ROWS
FROM [Pizza Place Cube];

The result from this query is as follows: enter image description here

The following SQL Script returns the corrects results:

SELECT
(SELECT COUNT(*) FROM PIZZA_SALES WHERE TimeID IN (
SELECT TimeID FROM TIME WHERE Month = (SELECT Month FROM TIME WHERE TIMEID = ((SELECT     MAX(TimeID) FROM PIZZA_SALES))) AND
        Year = (SELECT Year FROM TIME WHERE TIMEID = (SELECT MAX(TimeID) FROM     PIZZA_SALES)))) AS 'CURRENT MONTH',
(SELECT COUNT(*) FROM PIZZA_SALES WHERE TimeID IN (
SELECT TimeID FROM TIME WHERE Month = (SELECT Month FROM TIME WHERE TIMEID = ((SELECT     MAX(TimeID) FROM PIZZA_SALES)))-1 AND
        Year = (SELECT Year FROM TIME WHERE TIMEID = (SELECT MAX(TimeID) FROM     PIZZA_SALES)))) AS 'PREVIOUS MONTH',
(SELECT COUNT(*)/3 FROM PIZZA_SALES WHERE TimeID IN (
SELECT TimeID FROM TIME WHERE
        Year = (SELECT Year FROM TIME WHERE TIMEID = (SELECT MAX(TimeID) FROM     PIZZA_SALES)) AND
        (Month = (SELECT Month FROM TIME WHERE TIMEID = ((SELECT MAX(TimeID) FROM     PIZZA_SALES)))-1 OR
        Month = (SELECT Month FROM TIME WHERE TIMEID = ((SELECT MAX(TimeID) FROM     PIZZA_SALES)))-2 OR
        Month = (SELECT Month FROM TIME WHERE TIMEID = ((SELECT MAX(TimeID) FROM     PIZZA_SALES)))-3))) AS '3 MONTHS',
(SELECT COUNT(*)/3 FROM PIZZA_SALES WHERE TimeID IN (
SELECT TimeID FROM TIME WHERE
        Year = (SELECT Year FROM TIME WHERE TIMEID = (SELECT MAX(TimeID) FROM     PIZZA_SALES)) AND
        (Month = (SELECT Month FROM TIME WHERE TIMEID = ((SELECT MAX(TimeID) FROM     PIZZA_SALES)))-4 OR
        Month = (SELECT Month FROM TIME WHERE TIMEID = ((SELECT MAX(TimeID) FROM     PIZZA_SALES)))-5 OR
        Month = (SELECT Month FROM TIME WHERE TIMEID = ((SELECT MAX(TimeID) FROM     PIZZA_SALES)))-6))) AS '6 MONTHS';

The desired results as returned by the SQL query are as follows: enter image description here

Pleas help, I have tried many different sollutions on the internet, however they do not return the correct results.

thewikus
  • 445
  • 1
  • 5
  • 16
  • It seems that your results aren't shown in the question. Can you simplify the question to just start by comparing between current and *one* prior month, and then expand it to the averages? – N West Oct 01 '13 at 14:33
  • The MDX query should return a:the sales of the current month, b: the sales of the previous month and c: the average of the last the months. The last part (d) should return results similar to (c) but for the 3 month period before the last 3 months. – thewikus Oct 02 '13 at 08:44

1 Answers1

1

The following query should return what you want:

WITH MEMBER [Measures].[Current Month] AS 
    [Measures].[PIZZA SALES Count]
MEMBER [Measures].[Previous Month] AS
        ([TIME].[Month].CurrentMember.Lag(1),
         [Measures].[PIZZA SALES Count])

MEMBER [Measures].[3 Months] AS
        AVG([TIME].[Month].CurrentMember.Lag(3) : [TIME].[Month].CurrentMember.Lag(1),
            [Measures].[PIZZA SALES Count])   

MEMBER [Measures].[6 Months] AS
        AVG([TIME].[Month].CurrentMember.Lag(6) : [TIME].[Month].CurrentMember.Lag(4),
            [Measures].[PIZZA SALES Count])   

    SELECT
    {
        [Measures].[Current Month],
        [Measures].[Previous Month],
        [Measures].[3 Months],
        [Measures].[6 Months]
    }
    ON COLUMNS,
    {
        [TIME].[Month].&[12]
    } ON ROWS
FROM [Pizza Place Cube]

Note that Avg needs a set as the first argument, and the colon construct build a set starting with the member before the colon and ending with the member after it.

FrankPl
  • 13,205
  • 2
  • 14
  • 40