Note that there are some explanatory texts on larger screens.

plurals
  1. POSelect average sales count over 3 month period in MDX
    primarykey
    data
    text
    <p>I have a problem with the following query:</p> <p>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.</p> <p>Here is the structure of the database/cube: <img src="https://i.stack.imgur.com/5tTXh.jpg" alt="enter image description here"></p> <pre><code>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].&amp;[12] } ON ROWS FROM [Pizza Place Cube]; </code></pre> <p>The result from this query is as follows: <img src="https://i.stack.imgur.com/nyuwq.jpg" alt="enter image description here"></p> <p>The following SQL Script returns the corrects results:</p> <pre><code>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'; </code></pre> <p>The desired results as returned by the SQL query are as follows: <img src="https://i.stack.imgur.com/rP6W3.jpg" alt="enter image description here"></p> <p>Pleas help, I have tried many different sollutions on the internet, however they do not return the correct results.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload