Quantcast
Channel: SQL Server Analysis Services Forum
Viewing all articles
Browse latest Browse all 2472

Add a total row to an MDX query that pulls in information from 2 cubes

$
0
0

Using: SQL 2008r2

Hi,

Im quite new to MDX and have been given the challenge of writing an MDX statement that returns results from 2 cubes.  I realise it is not recommended to use LOOKUPCUBE, but performance testing on our cube(s) showed this to be quicker.

The code below shows my working to date.  As I couldnt get it to do what I wanted to, Ive tried to output the counts from each cube to separate rows (Computer Total\User Total) - With the hope of recombining them into 1 row later..  Unfortunately, the User counts are appearing below the  Computer Count (because of the way I was setting the dimension (to avoid dimensionality issues) in the User total row.

WITH 
   // Computer Members
   //===============================================
    MEMBER [Measures].[Department Key]
        AS [Computer Department].[Parent Department Key].CurrentMember.Properties("Key")
    MEMBER [Measures].[Department Name]
        AS [Computer Department].[Parent Department Key].CurrentMember.Properties("Caption")

    MEMBER [Computer Department].[Parent Department Key].[This Department]
        AS ([Computer Department].[Parent Department Key].[Level 01].DataMember, [Measures].[Fact Computer Specification Count])
        
    MEMBER [Measures].[Migration Rag Key]
        AS [Computer].[Migration RAG].CurrentMember.Properties("Key") 
    MEMBER [Measures].[Computers]
        AS ([Measures].[Fact Computer Specification Count], [Computer Directory Object].[Object Type].&[Computer])

    MEMBER [Computer Department].[Parent Department Key].[Computer Total]
        AS ([Computer Department].[Parent Department Key].[All], [Measures].[Fact Computer Specification Count])


   // User Members
   //===============================================

  MEMBER [Computer Department].[Parent Department Key].[User Total]
    AS 
      LOOKUPCUBE
        (
          "Directory"
          , "
            (
               [Measures].[Directory Object Count]
              , [Directory Object].[Object Type].[User]
              , [Directory Object].[Migration RAG].&[" + [Measures].[Migration Rag Key] + "]
              , [Department].[Source].&[2]
              , [Directory Name].[All]
              , [Directory Object].[Parent Object Key].[All]
              , [Location].[Location].[All]
              , [Department].[Parent Department Key].[All]
             )"
        )

   MEMBER [Measures].[Users]  
    AS LOOKUPCUBE
        (
          "Directory"
          , "
            (
               [Measures].[Directory Object Count]  
              , [Directory Object].[Object Type].&[User]
              , [Department].[Source].&[2]  
              , [Directory Object].[Directory Name].[All]
              , [Directory Object].[Parent Object Key].[All]
              , [Location].[Location].[All]
              , [Department].[Parent Department Key].&[" + [Measures].[Department Key] + "] 
             )"
        )

  MEMBER [Measures].[User Department Name]
    AS [Computer Department].[Parent Department Key].CurrentMember.Properties("Caption")
  MEMBER [Measures].[Red Users]
    AS 
      LOOKUPCUBE
        ("Directory"
          , "
            (
              [Measures].[Directory Object Count]
              , [Directory Object].[Object Type].&[User]
              , [Directory Object].[Migration RAG].&[Red]
              , [Department].[Source].&[2]
              , [Directory Object].[Directory Name].[All]
              , [Directory Object].[Parent Object Key].[All]
              , [Location].[Location].[All]
              , [Department].[Parent Department Key].&[" + [Measures].[Department Key] + "]
             )"
        )
   MEMBER [Measures].[Amber Users]
    AS 
      LOOKUPCUBE
        ("Directory"
          , "
            (
              [Measures].[Directory Object Count]
              , [Directory Object].[Object Type].&[User]
              , [Directory Object].[Migration RAG].&[Amber]
              , [Department].[Source].&[2]
              , [Directory Object].[Directory Name].[All]
              , [Directory Object].[Parent Object Key].[All]
              , [Location].[Location].[All]
              , [Department].[Parent Department Key].&[" + [Measures].[Department Key] + "]
             )"
        )
  MEMBER [Measures].[Green Users]
    AS 
      LOOKUPCUBE
        ("Directory"
          , "
            (
              [Measures].[Directory Object Count]
              , [Directory Object].[Object Type].&[User]
              , [Directory Object].[Migration RAG].&[Green]
              , [Department].[Source].&[2]
              , [Directory Object].[Directory Name].[All]
              , [Directory Object].[Parent Object Key].[All]
              , [Location].[Location].[All]
              , [Department].[Parent Department Key].&[" + [Measures].[Department Key] + "]
             )"
        )
  MEMBER [Measures].[Unknown Users]
    AS 
      LOOKUPCUBE
        ("Directory"
          , "
            (
              [Measures].[Directory Object Count]
              , [Directory Object].[Object Type].&[User]
              , [Directory Object].[Migration RAG].&[Unknown]
              , [Department].[Source].&[2]
              , [Directory Object].[Directory Name].[All]
              , [Directory Object].[Parent Object Key].[All]
              , [Location].[Location].[All]
              , [Department].[Parent Department Key].&[" + [Measures].[Department Key] + "]
             )"
        )
   MEMBER [Measures].[Total RAGU Users]
    AS
      LOOKUPCUBE
        ("Directory"
          , "
            AGGREGATE
            (
              ([Measures].[Directory Object Count]
              , [Directory Object].[Object Type].&[User]
              , [Department].[Source].&[2]
              , [Directory Object].[Directory Name].[All]
              , [Directory Object].[Parent Object Key].[All]
              , [Location].[Location].[All]
              , [Department].[Parent Department Key].&[" + [Measures].[Department Key] + "]
              , EXCEPT([Directory Object].[Migration RAG].Children, [Directory Object].[Migration RAG].&[Unknown])
             )
           )"
        )    
  MEMBER [Measures].[Red Computers]
    AS ([Measures].[Fact Computer Specification Count], [Computer].[Migration RAG].&[Red])
  MEMBER [Measures].[Amber Computers]
    AS ([Measures].[Fact Computer Specification Count], [Computer].[Migration RAG].&[Amber])
  MEMBER [Measures].[Green Computers]
    AS ([Measures].[Fact Computer Specification Count], [Computer].[Migration RAG].&[Green])
  MEMBER [Measures].[Unknown Computers]
    AS ([Measures].[Fact Computer Specification Count], [Computer].[Migration RAG].&[Unknown])
  MEMBER [Measures].[Total RAGU Computers]
    AS 
      AGGREGATE
        (  
          (
            EXCEPT([Computer].[Migration RAG].Children, [Computer].[Migration RAG].&[Unknown])
          )
          , [Measures].[Fact Computer Specification Count]       
        ) 
SELECT
  {
    [Measures].[Department Name]
    , [Measures].[Users]
    , [Measures].[Red Users]
    , [Measures].[Computers]
    , [Measures].[Red Computers]   
  } ON COLUMNS,
  {
      ORDER
        (             
          EXCEPT
            (
              DESCENDANTS([Computer Department].[Parent Department Key].[All], 1, SELF)
              , [Computer Department].[Parent Department Key].[All].DataMember
            )
          , [Measures].[Department Name]
          , BASC
        )
        , [Computer Department].[Parent Department Key].[Computer Total]
        , [Computer Department].[Parent Department Key].[User Total] 
       --, [Measures].[UserTotal]    
  } ON ROWS
FROM [Hardware Inventory]
//WHERE 
//  (
//    [Computer Department].[Source].&[2]
//    , [Computer Directory Object].[Directory Name].[All]
//    , DESCENDANTS([Computer Directory Object].[Parent Object Key].[All], 0, SELF)

The screen shot is taken from a scaled down version of the entire report which I will include after the code below.

If anyone can point me in the right direction that would be great!  Changing the cubes are unfortunately not an option.

             WITH 
   MEMBER [Computer Department].[Parent Department Key].[This Department]
    AS ([Computer Department].[Parent Department Key].[Level 01].DataMember, [Measures].[Fact Computer Specification Count])
//  MEMBER [Department].[Parent Department Key].[Total]
//    AS (%Parent Department Key%, [Measures].[Directory Object Count])
  MEMBER [Measures].[Computers]
    AS ([Measures].[Fact Computer Specification Count], [Computer Directory Object].[Object Type].&[Computer])
  MEMBER [Measures].[Users]  
    AS LOOKUPCUBE
        ("Directory"
          , "
            (
               [Measures].[Directory Object Count]  
              , [Directory Object].[Object Type].&[User]
              , [Department].[Source].&[2]  
              , [Directory Object].[Directory Name].[All]
              , [Directory Object].[Parent Object Key].[All]
              , [Location].[Location].[All]
              , [Department].[Parent Department Key].&[" + [Measures].[Department Key] + "] 
             )"
        )

  MEMBER [Computer Department].[Parent Department Key].[Computer Total]
    AS ([Computer Department].[Parent Department Key].[All], [Measures].[Fact Computer Specification Count])

  MEMBER [Measures].[Department Key]
    AS [Computer Department].[Parent Department Key].CurrentMember.Properties("Key")

  MEMBER [Measures].[Migration Rag Key]
    AS [Computer].[Migration RAG].CurrentMember.Properties("Key")


  MEMBER [Computer Department].[Parent Department Key].[User Total]
    AS 
      LOOKUPCUBE
        (
          "Directory"
          , "
            (
               [Measures].[Directory Object Count]
              , [Directory Object].[Object Type].[User]
              , [Directory Object].[Migration RAG].&[" + [Measures].[Migration Rag Key] + "]
              , [Department].[Source].&[2]
              , [Directory Name].[All]
              , [Directory Object].[Parent Object Key].[All]
              , [Location].[Location].[All]
              , [Department].[Parent Department Key].[All]
             )"
        )

  MEMBER [Measures].[Department Name]
    AS [Computer Department].[Parent Department Key].CurrentMember.Properties("Caption")
  MEMBER [Measures].[Red Users]
    AS 
      LOOKUPCUBE
        ("Directory"
          , "
            (
              [Measures].[Directory Object Count]
              , [Directory Object].[Object Type].&[User]
              , [Directory Object].[Migration RAG].&[Red]
              , [Department].[Source].&[2]
              , [Directory Object].[Directory Name].[All]
              , [Directory Object].[Parent Object Key].[All]
              , [Location].[Location].[All]
              , [Department].[Parent Department Key].&[" + [Measures].[Department Key] + "]
             )"
        )
   MEMBER [Measures].[Amber Users]
    AS 
      LOOKUPCUBE
        ("Directory"
          , "
            (
              [Measures].[Directory Object Count]
              , [Directory Object].[Object Type].&[User]
              , [Directory Object].[Migration RAG].&[Amber]
              , [Department].[Source].&[2]
              , [Directory Object].[Directory Name].[All]
              , [Directory Object].[Parent Object Key].[All]
              , [Location].[Location].[All]
              , [Department].[Parent Department Key].&[" + [Measures].[Department Key] + "]
             )"
        )
  MEMBER [Measures].[Green Users]
    AS 
      LOOKUPCUBE
        ("Directory"
          , "
            (
              [Measures].[Directory Object Count]
              , [Directory Object].[Object Type].&[User]
              , [Directory Object].[Migration RAG].&[Green]
              , [Department].[Source].&[2]
              , [Directory Object].[Directory Name].[All]
              , [Directory Object].[Parent Object Key].[All]
              , [Location].[Location].[All]
              , [Department].[Parent Department Key].&[" + [Measures].[Department Key] + "]
             )"
        )
  MEMBER [Measures].[Unknown Users]
    AS 
      LOOKUPCUBE
        ("Directory"
          , "
            (
              [Measures].[Directory Object Count]
              , [Directory Object].[Object Type].&[User]
              , [Directory Object].[Migration RAG].&[Unknown]
              , [Department].[Source].&[2]
              , [Directory Object].[Directory Name].[All]
              , [Directory Object].[Parent Object Key].[All]
              , [Location].[Location].[All]
              , [Department].[Parent Department Key].&[" + [Measures].[Department Key] + "]
             )"
        )
   MEMBER [Measures].[Total RAGU Users]
    AS
      LOOKUPCUBE
        ("Directory"
          , "
            AGGREGATE
            (
              ([Measures].[Directory Object Count]
              , [Directory Object].[Object Type].&[User]
              , [Department].[Source].&[2]
              , [Directory Object].[Directory Name].[All]
              , [Directory Object].[Parent Object Key].[All]
              , [Location].[Location].[All]
              , [Department].[Parent Department Key].&[" + [Measures].[Department Key] + "]
              , EXCEPT([Directory Object].[Migration RAG].Children, [Directory Object].[Migration RAG].&[Unknown])
             )
           )"
        )    
  MEMBER [Measures].[Red Computers]
    AS ([Measures].[Fact Computer Specification Count], [Computer].[Migration RAG].&[Red])
  MEMBER [Measures].[Amber Computers]
    AS ([Measures].[Fact Computer Specification Count], [Computer].[Migration RAG].&[Amber])
  MEMBER [Measures].[Green Computers]
    AS ([Measures].[Fact Computer Specification Count], [Computer].[Migration RAG].&[Green])
  MEMBER [Measures].[Unknown Computers]
    AS ([Measures].[Fact Computer Specification Count], [Computer].[Migration RAG].&[Unknown])
  MEMBER [Measures].[Total RAGU Computers]
    AS 
      AGGREGATE
        (  
          (
            EXCEPT([Computer].[Migration RAG].Children, [Computer].[Migration RAG].&[Unknown])
          )
          , [Measures].[Fact Computer Specification Count]       
        ) 
SELECT
  {
    [Measures].[Department Name]
    , [Measures].[Users]
    , [Measures].[Red Users]
    , [Measures].[Amber Users]
    , [Measures].[Green Users]
    , [Measures].[Unknown Users]
    , [Measures].[Total RAGU Users]
    , [Measures].[Computers]
    , [Measures].[Red Computers]
    , [Measures].[Amber Computers]
    , [Measures].[Green Computers]
    , [Measures].[Unknown Computers]
    , [Measures].[Total RAGU Computers]
  } ON COLUMNS,
  {
      [Computer Department].[Parent Department Key].[This Department], 
      ORDER
        (             
          EXCEPT
            (
              DESCENDANTS([Computer Department].[Parent Department Key].[All], 1, SELF)
              , [Computer Department].[Parent Department Key].[All].DataMember
            )
          , [Measures].[Department Name]
          , BASC
        )
        , [Computer Department].[Parent Department Key].[Computer Total]
        , [Computer Department].[Parent Department Key].[User Total] 
       --, [Measures].[UserTotal]    
  } ON ROWS
FROM [Hardware Inventory]
//WHERE 
//  (
//    [Computer Department].[Source].&[2]
//    , [Computer Directory Object].[Directory Name].[All]
//    , DESCENDANTS([Computer Directory Object].[Parent Object Key].[All], 0, SELF)


Viewing all articles
Browse latest Browse all 2472

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>