MDX MEDIAN()函数不正确的结果

时间:2016-03-24 19:49:58

标签: ssas mdx

我正在尝试实现一个数据立方体,我可以显示给定维度集的度量的平均值和中值。使用AdventureWorksDW的基本示例:

WITH
    MEMBER [Measures].[Median Qty] AS
     MEDIAN ( [Product].[Product Categories].siblings, [Measures].[Reseller Sales-Order Quantity])
    MEMBER [Measures].[avgOrderSize] AS
    CASE
        WHEN [Measures].[Reseller Sales Count] = 0 THEN NULL
        ELSE ([Measures].[Reseller Sales-Order Quantity]  / [Measures].[Reseller Sales Count])
    END

SELECT {[Measures].[Reseller Sales-Order Quantity], [Measures].[Median Qty], [Measures].[avgOrderSize]} ON 0,
NON EMPTY([Product].[Product Categories].[Category]) ON 1
FROM [Analysis Services Tutorial]
WHERE ([Product].[Product Name].&[476]);

输出结果为:

Median output

我的[Measures].[avgOrderSize]是正确的,因为它似乎正在使用查询中返回的实际度量。但是,[Measures].[Median Qty]根本不符合我的要求。我的价值大约为6(鉴于我的AdventureWorksDW2012有344个经销商订单,其中包括此产品)。看起来MEDIAN()正在整个产品系列中工作?

我在[Product].[Product Categories].siblings上尝试了[Product].[Product Name].CurrentMember的变体,其中{{1}}返回了与转销商销售订单数量相同的值 我做错了什么,如何让MEDIAN()函数像我期待的那样行动?

2 个答案:

答案 0 :(得分:1)

警告。这将是缓慢的。但它应该是正确的。我担心中位数只是一个昂贵的计算。第一个参数应该是简并维度,以及在事实表中每行有一个成员的属性。

WITH
    MEMBER [Measures].[Median Qty] AS
     MEDIAN ( EXISTING [Reseller Sales].[Reseller Sales Order Number].[Reseller Sales Order Number].Members, [Measures].[Reseller Sales-Order Quantity])
    ...

答案 1 :(得分:0)

我还没有使用过该功能,但您可以尝试的几件事情是:

EXISTING关键字添加到计算中:

WITH
    MEMBER [Measures].[Median Qty] AS
     MEDIAN ( 
        EXISTING [Product].[Product Categories].siblings
      , [Measures].[Reseller Sales-Order Quantity]
     )
...

添加Exists功能虽然这可能与上面的内容非常相似:

WITH
    MEMBER [Measures].[Median Qty] AS
     MEDIAN ( 
        EXISTS([Product].[Product Categories].siblings, [Product].[Product Name].&[476])
      , [Measures].[Reseller Sales-Order Quantity]
     )
...

试试这个:

WITH
    MEMBER [Measures].[Median Qty] AS
     MEDIAN ( 
        [Product].[Product Categories].currentmember.siblings
      , [Measures].[Reseller Sales-Order Quantity]
     )
...