1

I recently used excel to connect to analysis services and generate a mdx code that was used to query the cube. The code is shown below. The code was also verified using sql profiler. However when I go to analysis services and browse the cube and put in the cube in management studio, I get errors. Please let me know if the code can be fixed. This code is exactly what i need for my requirements so if there are any modifications please replace it within the cube itself.

Code

SELECT NON EMPTY Hierarchize(DrilldownMember(CrossJoin({ 
[ColorsDim].[PrimeColor].[All], [ColorsDim].[PrimeColor].[PrimeColor].AllMember}, 
{([ColorsDim].[SecondColor].[All]) }), [ColorsDim].[PrimeColor].[PrimeColor].AllMember, 
[ColorsDim].[SecondColor])) DIMENSION PROPERTIES PARENT_UNIQUE_NAME
            ,HIERARCHY_UNIQUE_NAME ON COLUMNS
            ,NON EMPTY Hierarchize({DrilldownMember({ 
[ColorsDim].[Color_id].[All] },,, INCLUDE_CALC_MEMBERS) }) DIMENSION PROPERTIES 
PARENT_UNIQUE_NAME
                    ,HIERARCHY_UNIQUE_NAME ON ROWS FROM [Model]
                WHERE ([Measures].[Number of records in ColorDim]) CELL PROPERTIES VALUE
                    ,FORMAT_STRING
                    ,BACK_COLOR
                    ,FORE_COLOR
                    ,FONT_FLAGS

Error

Query Preparation failed. 

Additional information
The query cannot be prepared: The query must have at least one axis. The first axis of the query should not have mulitple hierarchies, nor should it reference any dimension other than the measures dimension..
Parameter name: mdx (MDXQueryGenerator)

Desired Output enter image description here

The following query shows the desired output:

Select non empty
[ColorsDim].[PrimeColor].children *
[ColorsDim].[SecondColor].children
on 0
, non empty
[ColorsDim].[Color_id].children
on 1
from [model]
where [Measures].[Number of records in ColorDim];

The measure in the query is a count of rows for that particular dimension

Mutai
  • 125
  • 1
  • 1
  • 9
  • "However when I go to analysis services and browse the cube and put in the cube in management studio, I get errors." Could you clarify? Are you using an MDX window in SSMS? Are you using Reporting Services? Are you opening a query window against the cube in SSMS and then editing the MDX? Try an MDX window. – GregGalloway Apr 06 '17 at 20:14
  • Your right the mdx worked. But the same mdx doesn't work when u browse the cube and input the same code in there. – Mutai Apr 07 '17 at 20:42
  • ah. The cube browser is essentially the Reporting Services MDX query designer. It only supports measures on columns. So I would stick with an MDX query window not the cube browser. @whytheq is absolutely right. – GregGalloway Apr 07 '17 at 20:51

1 Answers1

1

Well the error message you've provided looks to me like an SSRS error message.

If you open SSMS with a connection to the cube and then open an mdx query the query should run ok.

Reformatting the query so we can see more clearly what is happening gives this:

SELECT 
  NON EMPTY 
    Hierarchize(
      DrilldownMember(
        CrossJoin(
          {[ColorsDim].[PrimeColor].[All]
         , [ColorsDim].[PrimeColor].[PrimeColor].AllMember}
         ,{([ColorsDim].[SecondColor].[All]) }
        )
      , [ColorsDim].[PrimeColor].[PrimeColor].AllMember
      , [ColorsDim].[SecondColor]
      )
    ) DIMENSION PROPERTIES PARENT_UNIQUE_NAME
      ,HIERARCHY_UNIQUE_NAME ON COLUMNS
 ,NON EMPTY 
    Hierarchize(
       {
         DrilldownMember(
           {[ColorsDim].[Color_id].[All] }
            ,,, INCLUDE_CALC_MEMBERS
         ) 
        }
     ) 
    DIMENSION PROPERTIES PARENT_UNIQUE_NAME
   ,HIERARCHY_UNIQUE_NAME ON ROWS 
FROM [Model]
WHERE ([Measures].[Number of records in ColorDim]) 
CELL PROPERTIES VALUE
                    ,FORMAT_STRING
                    ,BACK_COLOR
                    ,FORE_COLOR
                    ,FONT_FLAGS

Excel adds a lot of boilerplate code so we can get rid of the cell and dimension properties, I suspect hierarchize is not needed, and the crossjoin can be replaced with the simple * operator, to give this:

SELECT 
  NON EMPTY 
      DrilldownMember(
           [ColorsDim].[PrimeColor].[All]
         * [ColorsDim].[PrimeColor].[PrimeColor].AllMember
         ,{([ColorsDim].[SecondColor].[All])}
        )
      , [ColorsDim].[PrimeColor].[PrimeColor].AllMember
      , [ColorsDim].[SecondColor]
      )
     ON 0
 ,NON EMPTY 
     DrilldownMember(
       {[ColorsDim].[Color_id].[All] }
        ,,, INCLUDE_CALC_MEMBERS
     ) 
    ON 1
FROM [Model]
WHERE [Measures].[Number of records in ColorDim]; 

If you want to use this in SSRS then you will need to re-write it so that COLUMNS (or 0) only uses the dimension Measures!

To make it so that ssrs is happy enough you could do this:

WITH 
MEMBER [Measures].[Green_YellowGreen] AS
  (
   [ColorsDim].[PrimeColor].[PrimeColor].[Green],
   [ColorsDim].[PrimeColor].[SecondColor].[Yellow-Green],
   [Measures].[Number of records in ColorDim]
  )
MEMBER [Measures].[Green_BlueGreen] AS
  (
   [ColorsDim].[PrimeColor].[PrimeColor].[Green],
   [ColorsDim].[PrimeColor].[SecondColor].[Blue-Green],
   [Measures].[Number of records in ColorDim]
  )
SELECT 
 NON EMPTY
 {[Measures].[Green_YellowGreen], [Measures].[Green_BlueGreen] }
ON 0
, NON EMPTY
  [ColorsDim].[Color_id].children
ON 1
FROM [model];
whytheq
  • 34,466
  • 65
  • 172
  • 267
  • Can you give an example of how to re-write it so i can use it in ssrs – Mutai Apr 07 '17 at 20:41
  • @Mutai so you want to use it in SSRS? It is as I've said you can only use `[Measures].[...]` on the `ON COLUMNS` clause. You'll need to supply an idea of the output (screen-print) for us to re-write it. – whytheq Apr 07 '17 at 21:02
  • I have provided the desired output as well as the query that I have used to get that output in mdx. Just not certain on how to apply that to ssrs. Appreciate an example to clear up the confusion. – Mutai Apr 07 '17 at 21:43
  • @Mutai ok - I added a script - you can expand the WITH clause as much as you want for the other colour combination - the drawback is that this is not very dynamic like using `.children` but ssrs is not perfect when it comes to consuming mdx – whytheq Apr 08 '17 at 08:52
  • Thank you! Appreciate the help – Mutai Apr 08 '17 at 15:13
  • @Mutai no problem - as I mentioned SSRS is a little limited when it comes to connecting to olap: still very useful though. – whytheq Apr 08 '17 at 15:15