【问题标题】:MDX wrong sum on month level月份级别的 MDX 错误总和
【发布时间】:2025-11-24 16:55:02
【问题描述】:

我需要用立方体中的值计算过去 7 天的计数。

我写下一个 MDX 表达式:

Count
(
  Filter
  (
    Descendants
    (
        [Date].[Date Hierarchy].CurrentMember.Lag(6)
      : 
        [Date].[Date Hierarchy].CurrentMember
    )
   ,[Measures].[Amount]
  )
)

数据在日级别的层次结构中是正确的,但在月级别和其他级别衡量的是过去 7 个月的总和值。

更新 我想显示每月最后一天的值

示例:
日期 |值1 |价值2
2017-01-01 | 5 | 4
2017-01-02 | 4 | 3
2017-01-03 | 3 | 2
.....
2017-01-31 | 7 |1

【问题讨论】:

  • 如果您选择了月份级别,您希望此度量显示什么? ....所以说在第 1 行显示 11 月 16 日,在第 2 行显示 12 月 16 日,在第 3 行显示 1 月 17 日:然后在第 2 列中您有上述措施 - 它应该显示什么?
  • 只是为了说清楚:你想得到当月的最后 7 天吗?

标签: ssas mdx


【解决方案1】:

您可以在lastchild 的帮助下获取该月的最后 7 天。它将帮助您获得本月的最后一天。因此,对于月份级别,您可以使用:

Count (
    Filter ( 

            {[Date].[Date Hierarchy].CurrentMember.lastchild.Lag(6) :
            [Date].[Date Hierarchy].CurrentMember.lastchild}
         ,[Measures].[Amount]
    )
)

在这里使用 DESCENDANTS 是不合适的,因为它返回包含该层次结构的所有子成员的集合。

【讨论】:

    【解决方案2】:

    未经测试,但可能类似于以下内容

    COUNT(
      //to find the members with values use nonempty
      NONEMPTY( 
        //find the last date in a set
        TAIL(  
          EXISTS(  //find the dates associated to the currentmember 
            [Date].[Date Hierarchy].CurrentMember
           ,[Date].[Date Hierarchy].[Date Hierarchy].MEMBERS
          )
        ).ITEM(0).LAG(6)
        :
        TAIL(  //find the last date
          EXISTS(  //find the dates associated to the currentmember 
            [Date].[Date Hierarchy].CurrentMember
           ,[Date].[Date Hierarchy].[Date Hierarchy].MEMBERS
          )
        ).ITEM(0)
        , [Measures].[Amount]  
      )
    )
    

    我针对 Adventure Works 立方体进行了测试。

    这是如果选择了月份:

    WITH 
      MEMBER [Measures].[X] AS 
        Count
        (
          NonEmpty
          (
              Tail
              (
                Exists
                (
                  [Date].[Calendar].[Date].MEMBERS
                 ,[Date].[Calendar].CurrentMember
                )
              ).Item(0).Lag(6)
            : 
              Tail
              (
                Exists
                (
                  [Date].[Calendar].[Date].MEMBERS
                 ,[Date].[Calendar].CurrentMember
                )
              ).Item(0)
           ,[Measures].[Internet Sales Amount]
          )
        ) 
    SELECT 
      NON EMPTY 
        {
          [Measures].[Internet Sales Amount]
         ,[Measures].[X]
        } ON 0
     ,NON EMPTY 
          [Date].[Calendar].[Month].&[2007]&[7]
        : 
          [Date].[Calendar].[Month].&[2008]&[6] ON 1
    FROM [Adventure Works];
    

    按预期返回 7:

    如果我在轴 1 上改为每天:

    WITH 
      MEMBER [Measures].[X] AS 
        Count
        (
          NonEmpty
          (
              Tail
              (
                Exists
                (
                  [Date].[Calendar].[Date].MEMBERS
                 ,[Date].[Calendar].CurrentMember
                )
              ).Item(0).Lag(6)
            : 
              Tail
              (
                Exists
                (
                  [Date].[Calendar].[Date].MEMBERS
                 ,[Date].[Calendar].CurrentMember
                )
              ).Item(0)
           ,[Measures].[Internet Sales Amount]
          )
        ) 
    SELECT 
      NON EMPTY 
        {
          [Measures].[Internet Sales Amount]
         ,[Measures].[X]
        } ON 0
     ,NON EMPTY 
          [Date].[Calendar].[Date].&[20070701]
        : 
          [Date].[Calendar].[Date].&[20080630] ON 1
    FROM [Adventure Works];
    

    【讨论】:

    • 一个月这个表达式工作正常,但在一天级别我看到 0
    • @Elrengil 忘记了这个答案 - 意思是尝试在 AdvWrks 上测试它
    • @Elrengil 在 AdvWrks 中似乎可以正常工作 - 您可能需要在 EXISTS 函数内切换参数,因为我在第一次尝试时弄错了它们
    最近更新 更多