假设这个层次结构是ON ROWS
:
[All Products].[All Products]
然后类似于以下内容:
WITH MEMBER [Measures].test AS
CASE
WHEN
[All Products].[All Products].CURRENTMEMBER
IS [All Products].[All Products].[Group 2].&[1]
AND [Measures].[minus-prod-trx] > 0
THEN
(ancestor([All Products].[All Products].[Group 2].&[1],2),[Measures].[minus-prod-trx])
WHEN
....
以下是针对 MS 的 AdvWrks 多维数据集使用的上述示例:
WITH
MEMBER [Measures].test AS
CASE
WHEN
[Product].[Product Categories].CurrentMember
IS
[Product].[Product Categories].[Product].[Hitch Rack - 4-Bike]
AND
[Measures].[Internet Sales Amount] > 0
THEN
(
Ancestor
(
[Product].[Product Categories].[Product].[Hitch Rack - 4-Bike]
,2
)
,[Measures].[Internet Sales Amount]
)
WHEN
[Product].[Product Categories].CurrentMember
IS
[Product].[Product Categories].[Product].[Road Bottle Cage]
AND
[Measures].[Internet Sales Amount] > 0
THEN
(
Ancestor
(
[Product].[Product Categories].[Product].[Road Bottle Cage]
,3
)
,[Measures].[Internet Sales Amount]
)
END
SELECT
NON EMPTY
{
[Measures].[Internet Sales Amount]
,[Measures].test
} ON 0
,[Product].[Product Categories].[Product] ON 1
FROM [Adventure Works];
结果如下:
在评论中,您输入了以下内容
with member [Measures].Test as
case
when [Dim Date].[Hierarchy].currentmember
IS [Dim Date].[Hierarchy].[Calendar Year].&[2007].&[1].&[2]
then (ancestor([Dim Date].[Hierarchy].[Calendar Year].&[2007].&[1].&[2],2),[Measures].[Amount]) end
select {[Measures].Test} on 0
from [My Adventure Works];
这段代码对我来说看起来不错。[Dim Date].[Hierarchy].currentmember
不等于您在上面指定的成员 -currentmember
查看输出的每一行并返回当前成员,但在上面您的行上没有此层次结构......所以 currentmember 是 All 成员,所以您的WHEN
条件为假。
Sourav 通过创建上下文为您修复了上述问题,以便 currentmember 返回层次结构成员以外的All
成员:
with member [Measures].Test as
case
when [Dim Date].[Hierarchy].currentmember
IS [Dim Date].[Hierarchy].[Calendar Year].&[2007].&[1].&[2]
then (ancestor([Dim Date].[Hierarchy].[Calendar Year].&[2007].&[1].&[2],2),[Measures].[Amount]) end
select
{[Measures].Test} on 0,
[Dim Date].[Hierarchy].[Calendar Year].&[2007].&[1].&[2] on 1 //<<including this means that the currentmember has something to work with!!
from [My Adventure Works];
请阅读 currentmember 函数的定义:https ://msdn.microsoft.com/en-us/library/ms144948.aspx