[英]How to build a dynamic MDX formula for a calculated member?
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 ie '05', so adding 100-1 )
取得星期值没有问题(我需要为两位数,即'05',所以加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. 我需要的是此
MDX
计算的值。
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"
第2行第0列出现词法错误。遇到:之后:“ [\\ n”
Any idea? 任何想法?
This is your error: 这是您的错误:
strtomember([Measures].Week)
Let us say that [Measures].Week
is equal to 15 then you are trying to do this: 假设
[Measures].Week
等于15,则您尝试这样做:
strtomember(15)
So there are two errors in the above: 因此,上面有两个错误:
Maybe try putting the strToMember
function around the string that is the representation of the member: 也许尝试将
strToMember
函数放在表示成员的字符串周围:
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
: 这是功能
strtomember
的MSDN参考:
https://msdn.microsoft.com/en-us/library/ms146022.aspx?f=255&MSPPError=-2147217396 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) ) 看上一篇文章:( StrToMember不接受计算量度(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 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. 好吧,如果您希望使用
PeriodsToDate
那么我们需要使用StrToSet
,然后在函数内使用此set中的成员。 This is because custom members lose their family ties and are therefore useless inside some mdx
functions. 这是因为自定义成员失去了家庭联系,因此在某些
mdx
函数中无用。 Here is a working script in AdvWrks
illustrating the approach I'm suggesting: 这是
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];
[Measures].Week
is already a string entity. [Measures].Week
已经是一个字符串实体。 You don't need to put StrToMember
around it. 您无需在其周围放置
StrToMember
。 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]
)
I got an interesting suggestion on Pentaho forums that pointed me to a nice blog post which allowed to write an elegant solution: 在Pentaho论坛上,我得到了一个有趣的建议,使我指向了一个不错的博客文章,该博客文章允许编写一个优雅的解决方案:
http://diethardsteiner.blogspot.com.es/2009/10/current-date-function-on-mondrian.html 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 THKS
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.