如何为计算成员构建动态MDX公式?

时间:2015-06-23 10:47:24

标签: dynamic mdx

我正在尝试创建一个计算成员,以获得上周的总结。 获得周值没问题(我需要它是两位数,即' 05',所以加100-1)

with
Member [Measures].Week as 
  'right(str(int(99+datepart ( ''ww'', Now()))),2)' 

- 按预期工作

member [Measures].SalesUpToWeek as
  'strtomember(
     "aggregate(periodstodate([Dim].[2015],[Dim].[2015].[" + ([Measures].Week) + "]),[Measures].[Sales])")'

我得到字面值

aggregate(
    periodstodate([Dim].[2015],[Dim].[2015].[25])
   ,[Measures].[Sales]
)

我需要的是此MDX计算的值。

所有其他尝试最终都会出现语法错误。就像一个例子

member [Measures].SumToWeek as 
 'aggregate(
     periodstodate(
       [Dim].[2015],[Dim].[2015].[' + strtomember([Measures].Week) + '])
   ,[Measures].[Sales])'   

错误

  

第2行第0列的词汇错误。遇到:之后:" [\ n"

有什么想法吗?

3 个答案:

答案 0 :(得分:0)

这是你的错误:

strtomember([Measures].Week)

我们假设[Measures].Week等于15,那么你正试图这样做:

strtomember(15)

所以上面有两个错误:

  1. 您将数字输入到将字符串转换为Memebers的函数
  2. 您需要提供该成员的完整字符串表示,即。" [Dim]。[2015]。[15]"
  3. 也许尝试将strToMember函数放在作为成员表示的字符串周围:

    MEMBER[Measures].SumToWeek AS
     'aggregate(
         periodstodate(
           [Dim].[2015],
           strtomember('[Dim].[2015].[' + [Measures].Week + ']', constrained)
         )
       ,[Measures].[Sales])'
    

    以下是函数strtomember的MSDN参考:
    https://msdn.microsoft.com/en-us/library/ms146022.aspx?f=255&MSPPError=-2147217396

    修改

    查看上一篇文章:(StrToMember does not accept calculated measure (mdx)
    ...您需要先创建成员,然后再将其添加到新度量中,所以:

    WITH 
      MEMBER [Measures].[Week] AS 
        Right
        (
          Str(Int(99 + Datepart('ww',Now())))
         ,2
        ) 
      MEMBER [Dim].[2015].[TargetWeek] AS 
        StrToMember
        (
          '[Dim].[2015].[' + [Measures].Week + ']'
         ,constrained
        ) 
      MEMBER [Measures].SumToWeek AS 
        Aggregate
        (
          PeriodsToDate
          (
            [Dim].[2015]
           ,[Dim].[2015].[TargetWeek]
          )
         ,[Measures].[Sales]
        ) 
    

    <强> EDIT2

    好的,如果你想使用PeriodsToDate,那么我们需要使用StrToSet,然后在函数中使用此集合中的成员。这是因为自定义成员失去了他们的家庭关系,因此在某些mdx函数中无用。 以下是AdvWrks中的工作脚本,说明了我建议的方法:

    WITH 
      MEMBER [Measures].[Wk] AS 
        Right
        (
          Str(Int(99 + Datepart('ww',Now())))
         ,2
        ) 
      SET [TargetWeek] AS 
        StrToSet
        (
         '[Date].[Calendar Weeks].[Calendar Week].[Week ' + cstr([Measures].[Wk]) + ' CY 2007]'
    
        ) 
      MEMBER [Measures].[SumToWeek] AS 
        Aggregate
        (
          PeriodsToDate
          (
            [Date].[Calendar Weeks].[Calendar Year]
           ,[TargetWeek].item(0).item(0)
          )
         ,([Measures].[Internet Sales Amount])
        ) 
    SELECT 
      {[Measures].[SumToWeek]} ON 0,
      [Product].[Product Categories].[All] ON 1  
    FROM [Adventure Works];
    

答案 1 :(得分:0)

[Measures].Week已经是一个字符串实体。您不需要在其周围放置StrToMember。相反,你应该在动态定义的字符串之外。

with
Member [Measures].Week as 
  "right(str(int(99+datepart ( ''ww'', Now()))),2)"

member [Measures].SumToWeek as 
aggregate(
            periodstodate(
                        [Dim].[2015],
                        StrToMember("[Dim].[2015].[" + [Measures].Week + "]")
                         )
            ,
            [Measures].[Sales]
         )

答案 2 :(得分:0)

我在Pentaho论坛上得到了一个有趣的建议,它指出了一个很好的博客文章,它允许写一个优雅的解决方案:

http://diethardsteiner.blogspot.com.es/2009/10/current-date-function-on-mondrian.html

with
member [Measures].sm as aggregate(
    periodstodate(
        currentdatemember([Dim],'["Dim"]\.[yyyy]'),                     -- current year
        currentdatemember([Dim],'["Dim"]\.[yyyy]\.[ww]').lag(1)   -- previous week
    ),
    [Measures].[Sales])

THKS