MDX与月份的非空度量值相加的月份

时间:2012-07-03 17:59:20

标签: ssas mdx

这一直困扰着我,我不确定为何如此困难。 我有一个度量值,在某个时间点之前有空值,然后开始有值。我想获得月平均值,但仅限于那些实际上具有非空值的月份。我还希望我的时间范围可以为查询修复,无论哪个月有值(例如,全年)

以下是我尝试过的MDX的一种变体:

WITH 
MEMBER Measures.MonthsWithSales AS
    (IIF( IsEmpty(([Time].[Month].CurrentMember,[Measures].[ProductsSold])), 0,  [Measures].[MonthCount]))

MEMBER  Measures.AvgProductsSold AS
    [Measures].[ProductsSold] /Measures.MonthsWithSales

SELECT
{
[Measures].[ProductsSold], [Measures].[MonthCount],
[Measures].[MonthsWithSales], [Measures].[AvgProductsSold]
} ON 0,

[Time].[Month].Members ON 1

FROM MyCube
WHERE [Time].[Year].&[2010-01-01T00:00:00]

返回如下内容:

    ProductsSold    MonthCount  MonthsWithSales AvgProductsSold
All                     1644    12  **12**  **137**
2010-01-01 00:00:00.000 (null)  1   0       (null)
2010-02-01 00:00:00.000 (null)  1   0       (null)
2010-03-01 00:00:00.000 (null)  1   0       (null)
2010-04-01 00:00:00.000 (null)  1   0       (null)
2010-05-01 00:00:00.000 (null)  1   0       (null)
2010-06-01 00:00:00.000 234     1   1       234
2010-07-01 00:00:00.000 237     1   1       237
2010-08-01 00:00:00.000 236     1   1       236
2010-09-01 00:00:00.000 232     1   1       232
2010-10-01 00:00:00.000 232     1   1       232
2010-11-01 00:00:00.000 233     1   1       233
2010-12-01 00:00:00.000 240     1   1       240

问题出在ALL行上。 我希望全年MonthsWithSales返回7而不是12 AvgProductsSold(每月销售额)为234.86而不是137。

我意识到它没有做我想要的,因为它在ALL级别使用MonthCount。但我不知道如何“陷入”“每月维度”,仅在计算“全部”时的相关月份总结MonthCount

2 个答案:

答案 0 :(得分:3)

我假设您在月份层次结构中有2个级别:一个包含All成员,另一个包含月份。

MEMBER Measures.AvgProductsSold AS 
            IIf([Time].[Month].CurrentMember.Level.Ordinal = 0
                    , Avg([Time].[Month].CurrentMember.Children, [Measures].[ProductsSold])
                    , [Measures].[ProductsSold])

(您可能需要将[Time].[Month].CurrentMember.Children替换为[Time].[Month].Members

Avg函数计算非空值的平均值。

答案 1 :(得分:1)

以下是我可能最终使用的查询,正确使用我的时间层次结构:

WITH 
MEMBER [Measures].[MonthsWithSales]
AS
COUNT
(
    FILTER
    (
        DESCENDANTS([Time].[YQMD].CurrentMember,[Time].[YQMD].[Month]),
        NOT ISEMPTY([Measures].[ProductsSold])
    )
)

MEMBER
[Measures].[AvgProductsSold]
AS
[Measures].[ProductsSold]/[Measures].[MonthsWithSales]

SELECT
{
[Measures].[ProductsSold],
[Measures].[MonthsWithSales],
[Measures].[AvgProductsSold]
} ON 0,

[Time].[Month].Members ON 1

FROM MyCube

[YQMD]是一个时间层次结构,级别为:1年,2季度,3个月,4个日期