Search code examples
ssasmdxolap

Intersect Select Query in MDX


I want to have customers intersection in two mdx querys.

1.

SELECT 
  [Measures].[Cs] ON 0
 ,NonEmpty([Customers].[Customer].MEMBERS) ON 1
FROM [sfe cube]
WHERE 
  (
    [Calend].[Period].&[201506]
   ,[Customers].[BP Territory].&[38UZ1]
   ,[Materials].[Brand].&[Coca-Cola]
  );

2.

SELECT 
  [Measures].[Cs] ON 0
 ,NonEmpty([Customers].[Customer].MEMBERS) ON 1
FROM [sfe cube]
WHERE 
  (
    [Calend].[Period].&[201506]
   ,[Customers].[BP Territory].&[38UZ1]
   ,[Materials].[Brand].&[Fanta Orange CSD]
  );

My attempted mdx script:

SELECT
 {} ON COLUMNS,
INTERSECT(
 (  Select [Measures].[Cs] on 0,
    nonempty([Customers].[Customer].members) on 1
    from [sfe cube]
    WHERE ([Calend].[Period].&[201506]
    ,[Customers].[BP Territory].&[38UZ1]
    ,[Materials].[Brand].&[Coca-Cola])
 )
,
(   Select [Measures].[Cs] on 0,
    nonempty([Customers].[Customer].members) on 1
    from [sfe cube]
    WHERE ([Calend].[Period].&[201506]
    ,[Customers].[BP Territory].&[38UZ1]
    ,[Materials].[Brand].&[Fanta Orange CSD]))

) ON ROWS      
FROM [sfe cube] 

When I run query there is a error :

Subselect only support axis COLUMNS.

What am I doing wrong?


Solution

  • You can implement something like this for the AND logic:

    WITH 
      SET [YourSet] AS 
        NonEmpty
        (
          NonEmpty
          (
            {[Customers].[Customer].MEMBERS}
           ,{[Materials].[Brand].&[Fanta Orange CSD]}
          )
         ,{[Materials].[Brand].&[Coca-Cola]}
        ) 
    SELECT 
      [Measures].[Cs] ON 0
     ,NON EMPTY [YourSet] ON 1
    FROM [sfe cube]
    WHERE 
      (
        [Calend].[Period].&[201506]
       ,[Customers].[BP Territory].&[38UZ1]
      );
    

    A different variant (that is I like better) using Exists is detailed in this post:

    MDX query to calculate measure with AND condition by members from same dimension

    I like sourav's idea to us intersect. I think his script can be simplified slightly to this:

    WITH 
      SET CommonCustomers AS 
        Intersect
        (
          NonEmpty
          (
            [Customers].[Customer].MEMBERS
           ,[Materials].[Brand].&[Coca-Cola]
          )
         ,NonEmpty
          (
            [Customers].[Customer].MEMBERS
           ,[Materials].[Brand].&[Fanta Orange CSD]
          )
        ) 
    SELECT 
      {[Measures].[Cs]} ON COLUMNS
     ,NON EMPTY 
        CommonCustomers ON ROWS
    FROM [sfe cube]
    WHERE 
      (
        [Calend].[Period].&[201506]
       ,[Customers].[BP Territory].&[38UZ1]
      );