计算成员的麻烦始终等于零

时间:2018-11-28 16:07:52

标签: ssas mdx

我有一个简单的事实表,其中包含AverageBalance列,TimeKey(始终为月末日期),AccountKey和AccountClassKey。

我添加了计算所得的成员,该成员将计算不同时间层次上的平均余额。

CREATE MEMBER CURRENTCUBE.[Measures].[Average Calc] AS 
    Avg(
      EXISTING([Time].[Processing Date].[Processing Date].MEMBERS) ,
      [Measures].[Avg Bal]
    ) ;

它工作正常。

但是我有一个在计算中添加以下逻辑的问题: 根据AccountClassKey,应该从平均余额的其余部分中添加或减去平均余额。

因此,我尝试了类似的操作(首先我添加了“帐户类别密钥”作为衡量标准):

CREATE MEMBER CURRENTCUBE.[Measures].[AvgBalMult] AS
[Measures].[Avg Bal] * (
       CASE 
       WHEN [Measures].[Account Class Key] = 1 THEN 1  
       WHEN [Measures].[Account Class Key] = 2 THEN -1
       else 0
       END);

但是,随着时间的推移,我在报表中总是得到零,我意识到这是因为“帐户类别”度量值首先被汇总,因此除非我查看帐户,否则它永远不会具有1或2的值。级别。

最后,我想得到这样的东西:

CREATE MEMBER CURRENTCUBE.[Measures].[Average Calc] AS 
    Avg(
      EXISTING([Time].[Processing Date].[Processing Date].MEMBERS) ,
      [Measures].[Avg Bal] * (
       CASE 
       WHEN [Measures].[Account Class Key] = 1 THEN 1  
       WHEN [Measures].[Account Class Key] = 2 THEN -1
       else 0
       END)
    ) ;

在帐户级别,它将给我“平均余额”而没有符号,但是在进行汇总时,应该与上述逻辑进行汇总。

2 个答案:

答案 0 :(得分:0)

答案 1 :(得分:0)

尝试用下面的代码替换前两个代码段。我以为“ AccountClassKey”实际上是一个维度属性,您已经为该计算进行了度量。基于此,我将其命名为“ [DimAccount]。[AccountClass]。[Account Class Key]”

CREATE MEMBER CURRENTCUBE.[Measures].[AccountClassKeyMultipiler] AS
(
CASE 
WHEN [DimAccount].[AccountClass].[Account Class Key].currentmember.Properties ("Member_Value",TYPED) = "1" THEN 1  
WHEN [DimAccount].[AccountClass].[Account Class Key].currentmember.Properties ("Member_Value",TYPED) = "2" THEN -1  
else 0
END);

CREATE MEMBER CURRENTCUBE.[Measures].[AvgBalMult] AS
[Measures].[Avg Bal] * [Measures].[AccountClassKeyMultipiler]

编辑 在这里,我试图解决冒险作品中的类似情况

我们列出产品和销售月份

select 
{[Measures].[Internet Sales Amount]
}
on columns,
(
[Product].[Category].[Category],
[Date].[Month of Year].[Month of Year]
)
on rows 
from 
[Adventure Works]
where [Date].[Calendar Year].&[2013]

Result1

接下来,我们将上面的查询转换为基于月数的每种产品的平均销售额

with member 
[Measures].[Internet Sales AmountAvg]
as
Avg(EXISTING([Date].[Month of Year].[Month of Year]) ,[Measures].[Internet Sales Amount]) 
select 
{
[Measures].[Internet Sales Amount]
,
[Measures].[Internet Sales AmountAvg]
}
on columns,
(
[Product].[Category].[Category]
)
on rows 
from 
[Adventure Works]
where [Date].[Calendar Year].&[2013]

Result2

现在我们添加一个基于案例的乘数。看看最后一列

with
member [Measures].[Internet Sales AmountAvg]
as
Avg(EXISTING([Date].[Month of Year].[Month of Year]) ,[Measures].[Internet Sales Amount]) 

member [Measures].[Multipiler]
as
CASE 
WHEN [Product].[Product Line].currentmember.Properties ("Member_Value",TYPED) = 'Accessory' THEN 1  
WHEN [Product].[Product Line].currentmember.Properties ("Member_Value",TYPED) = 'Mountain' THEN 2  
WHEN [Product].[Product Line].currentmember.Properties ("Member_Value",TYPED) = 'Road' THEN 3  
WHEN [Product].[Product Line].currentmember.Properties ("Member_Value",TYPED) = 'Touring' THEN 4 
WHEN [Product].[Product Line].currentmember.Properties ("Member_Value",TYPED) = 'Components' THEN 5  
else 0
END

member [Measures].[Internet Sales AmountAvg2]
as
[Measures].[Internet Sales AmountAvg]*[Measures].[Multipiler]

select 
{
[Measures].[Internet Sales Amount]
,
[Measures].[Internet Sales AmountAvg]
,
[Measures].[Multipiler]
,
[Measures].[Internet Sales AmountAvg2]
}
on columns,
(
[Product].[Category].[Category],[Product].[Product Line].[Product Line]
)
on rows 
from 
[Adventure Works]
where [Date].[Calendar Year].&[2013]

Result 3

现在让我们看看如果从行中删除[Product]。[Product Line]。[Product Line]会发生什么情况

with
member [Measures].[Internet Sales AmountAvg]
as
Avg(EXISTING([Date].[Month of Year].[Month of Year]) ,[Measures].[Internet Sales Amount]) 

member [Measures].[Multipiler]
as
CASE 
WHEN [Product].[Product Line].currentmember.Properties ("Member_Value",TYPED) = 'Accessory' THEN 1  
WHEN [Product].[Product Line].currentmember.Properties ("Member_Value",TYPED) = 'Mountain' THEN 2  
WHEN [Product].[Product Line].currentmember.Properties ("Member_Value",TYPED) = 'Road' THEN 3  
WHEN [Product].[Product Line].currentmember.Properties ("Member_Value",TYPED) = 'Touring' THEN 4 
WHEN [Product].[Product Line].currentmember.Properties ("Member_Value",TYPED) = 'Components' THEN 5  
else 0
END

member [Measures].[Internet Sales AmountAvg2]
as
[Measures].[Internet Sales AmountAvg]*[Measures].[Multipiler]

select 
{
[Measures].[Internet Sales Amount]
,
[Measures].[Internet Sales AmountAvg]
,
[Measures].[Multipiler]
,
[Measures].[Internet Sales AmountAvg2]
}
on columns,
(
[Product].[Category].[Category]
)
on rows 
from 
[Adventure Works]
where [Date].[Calendar Year].&[2013]

Result 4

最重要的是,在这种情况下,度量的行为取决于维度属性值,您需要在一个轴上具有该维度属性。如果不存在,则使用默认值。如果逻辑中未处理默认值,则会导致错误结果