为特定值MDX查询设置0

时间:2015-01-28 10:35:04

标签: sql sql-server-2008 ssas mdx cube

我一直在寻找答案,但我没有找到任何答案。对不起,如果答案已在其他地方给出。

这是我的问题:

我有一个计算成员,即项目数(当前成员)除以项目总数(sumitem)。

with 
member 
    sumitem
as 
    SUM ([FailureReason].[FailureReason].[All],[Measures].[Items])
member
    Impact
as
    [Measures].[Items]/[Measures].[SumItem]

但对于我的维度 FailureReason 的特定成员,Impact的结果必须为0.所以我尝试添加此内容:

member
    ImpactFinal
as
    iif ([FailureReason].CurrentMember = [FailureReason].[FailureReason].&[127], 
        0, 
        Impact
    )   

我选择了这样的数据:

select 
    {[Measures].[Items],
     ImpactFinal
     } on columns, 
    [FailureReason].members on rows 
from 
    NoOTAR

但是这个特定成员的每个成员都没有为这个特定成员获得0,而是将它们的ImpactFinal等于0.奇怪的是,如果我将0替换为任何其他值,结果是好的。

3 个答案:

答案 0 :(得分:5)

只需使用

[FailureReason].CurrentMember IS [FailureReason].[FailureReason].&[127]

而不是

[FailureReason].CurrentMember = [FailureReason].[FailureReason].&[127]

它会起作用。

更新:一些提示:

  1. 也没有必要使用SUM函数,因为您只能定义元组,这对于服务器来说已经足够了:([FailureReason].[FailureReason].[All],[Measures].[Count])

  2. sumitem计算中检查ImpactFinal度量除以零是非常合理的。因为一旦应用了某些过滤器,这可能会导致将此度量和报告中的错误归零。

  3. 如果您不仅有机会查询,而且更新多维数据集,SCOPE ([FailureReason].[FailureReason].&[127],[Measures].[Impact]) THIS = 0因为效果而优于其他成员。

  4. 祝你好运!

    更新以修复总计:

    如果总数不应该是FailureReason 127,您可以用以下代码替换您的措施:

    member Impact
    as
    iif ([FailureReason].[FailureReason].CurrentMember is [FailureReason].[FailureReason].&[127],
            0,
            [Measures].[Items]
        )
    
    member ImpactFinal
    as 
    iif ([FailureReason].[FailureReason].CurrentMember is [FailureReason].[FailureReason].[All]
        ,[Measures].[Items]-([FailureReason].[FailureReason].&[127],[Measures].[Items])
        ,[Measures].[Impact])/[Measures].[SumItem]
    

    但是我有另一种解决方案,它更具可读性:

    member v2_ImpactUncountableFailure
    as
    iif ([FailureReason].[FailureReason].CurrentMember.Level.Ordinal=0
    or 
    [FailureReason].[FailureReason].CurrentMember is [FailureReason].[FailureReason].&[127]
    
    ,([FailureReason].[FailureReason].&[127],[Measures].[Items])
    ,null)
    
    member v2_ImpactFinal
    as
    ([Measures].[Items]-[Measures].[v2_ImpactUncountableFailure])
    /
    ([FailureReason].[FailureReason].[All],[Measures].[Items])
    

    仅使用这两项指标而不是一组指标sumitemImpactImpactFinal。第一个将显示失败的结果-127和总数。第二个从清洁的未过滤措施中减去它,所以最后我们有干净的成员,归零失败-127和纠正的总数。

    请告诉我,如果它不起作用,我已在我的数据库上测试过,一切正常。

答案 1 :(得分:0)

尝试

with 
member sumitem
as 
    SUM ([FailureReason].[FailureReason].[All],[Measures].[Items])
member LeaveOut
as 
    [FailureReason].[FailureReason].CurrentMember.Properties("Key")   
member Impact
as
    IIf([Measures].[LeaveOut]= "127", 0, [Measures].[Items]/[Measures].[SumItem])

答案 2 :(得分:0)

一个简单的 CASE 语句可以解决您的问题:试试这个

With 
Member ImpactFinal As 
CASE 
WHEN [FailureReason].[FailureReason].CurrentMember IS [FailureReason].[FailureReason].&[127] THEN 0
ELSE 
[Measures].[Items]
/
([FailureReason].[FailureReason].[All], [Measures].[Items]) 
END
SELECT
{ [Measures].[Items], ImpactFinal } On Columns
[FailureReason].[FailureReason].Members On Rows
From NoOTAR