【问题标题】:MDX to sum over months with non-empty measure value for the monthMDX 对月份的非空度量值求和
【发布时间】:2012-07-03 17:59:20
【问题描述】:

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

这是我尝试过的一种 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在计算“ALL”时仅在相关月份。

【问题讨论】:

  • 你能告诉我们月份层次结构的级别是什么吗?

标签: ssas mdx


【解决方案1】:

我假设您在月份层次结构中有 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 函数计算非空值的平均值。

【讨论】:

  • 这肯定更接近,除了它给出了整个时间维度的平均值,而不仅仅是我在 WHERE 子句中指定的日期范围内。 (如果我对 MDX 术语的使用有缺陷,请原谅我,我正在尝试将我的 SQL 专业知识应用到分析范式中)。 [月]“层次结构”实际上是一个维度属性,[年]也是如此。我的时间维度上也有一个真正的“YQMD”层次结构,具有年、季度、月、日期级别。我应该改用它吗?
  • 好的,我只需要:Avg(([Time].[Year].&[2010-01-01T00:00:00],[Time].[Month].CurrentMember.Children) , [Measures].[ProductsSold]) 在成员定义中
【解决方案2】:

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

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 日期

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-04-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多