How to build a dynamic MDX formula for a calculated member?

試著忘記壹切 提交于 2020-02-05 08:37:07

问题


I am trying to create a Calculated Member to get a sum up to last week. No problem to get the week value (I need it to be two digits i.e. '05', so adding 100-1 )

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

-- That works as expected

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

I get the literal value

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

What I need is the value of this MDX calculation.

All other attempts end up with a syntax error. Just as an example

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

Error

Lexical error at line 2, column 0. Encountered: after : "[\n"

Any idea?


回答1:


[Measures].Week is already a string entity. You don't need to put StrToMember around it. Instead you should have it outside the string you dynamically defined.

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:


This is your error:

strtomember([Measures].Week)

Let us say that [Measures].Week is equal to 15 then you are trying to do this:

strtomember(15)

So there are two errors in the above:

  1. You're feeding a numeric into a functions that converts Strings to Memebers
  2. You need to feed in the full string representation of the member i.e."[Dim].[2015].[15]"

Maybe try putting the strToMember function around the string that is the representation of the member:

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

Here is the MSDN reference for the function strtomember:
https://msdn.microsoft.com/en-us/library/ms146022.aspx?f=255&MSPPError=-2147217396


Edit

Looking at this previous post: (StrToMember does not accept calculated measure (mdx))
...you need to create the member before feeding it into the new measure, so:

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

Ok if you wish to use PeriodsToDate then we need to use StrToSet and then use the member in this set inside the function. This is because custom members lose their family ties and are therefore useless inside some mdx functions. Here is a working script in AdvWrks illustrating the approach I'm suggesting:

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];



回答3:


I got an interesting suggestion on Pentaho forums that pointed me to a nice blog post which allowed to write an elegant solution:

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



来源:https://stackoverflow.com/questions/31000705/how-to-build-a-dynamic-mdx-formula-for-a-calculated-member

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!