简体   繁体   中英

How do I perform MDX member subtraction across another hierarchy?

I have a cube with customers and products. Each customer holds a varying combination of products, each in different amounts.

I would like to calculate the difference (subtraction), between one customer and another, of amounts held in each product.

I am using SQL Server Analysis Services 2014. An example from AdventureWorksDW2014 would be:

select
{[Customer].[Country].&[Australia],[Customer].[Country].&[Canada]} on columns,
non empty ([Product].[Category].members, [Measures].[Internet Sales Amount]) on rows
from 
[Adventure Works]

This generates the following output:

                Australia       Canada
All Products    $9,061,000.58   $1,977,844.86
Accessories     $138,690.63     $103,377.85
Bikes           $8,852,050.00   $1,821,302.39
Clothing        $70,259.95      $53,164.62

However what I would like to obtain is

                Australia       Canada          (Australia - Canada)
All Products    $9,061,000.58   $1,977,844.86   $7,083,155.72
Accessories     $138,690.63     $103,377.85     $35,312.78
Bikes           $8,852,050.00   $1,821,302.39   $7,030,747.61
Clothing        $70,259.95      $53,164.62      $17,095.33

Ideally this could be performed not just in MDX, but allowing the user to select any two arbitrary customers for comparison.

This is my first MDX question, so please let me know if it should be framed differently.

Here is the MDX query for the desired output:

WITH MEMBER Australia AS
SUM({([Measures].[Internet Sales Amount], [Customer].[Country].&[Australia])}), FORMAT_STRING = "#,#.##"
MEMBER Canada AS
SUM({([Measures].[Internet Sales Amount], [Customer].[Country].&[Canada])}), FORMAT_STRING = "#,#.##"
MEMBER [Australia - Canada] AS
[Australia] - [Canada]
SELECT
{[Australia], [Canada], [Australia - Canada]} ON COLUMNS,
NON EMPTY {[Product].[Category].MEMBERS} ON ROWS
FROM [Adventure Works]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM