2

这一直困扰着我,我不知道为什么这么难。我有一个度量值,直到某个时间点才有空值,然后开始有值。我想获得每月平均值,但仅限于那些实际上具有非空值的月份。我还希望我的查询时间范围是固定的,无论哪个月份有值(例如,全年)

这是我尝试过的一种 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。

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

4

2 回答 2

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函数计算非空值的平均值。

于 2012-07-03T21:59:40.493 回答
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 日期

于 2012-07-04T16:14:50.560 回答