DAX HASONEVALUE行过滤器

时间:2015-10-12 16:13:18

标签: dax tabular

12 Total :=
SWITCH (
    TRUE (),
    HASONEVALUE ([level04] ), CALCULATE (
        [Local],
        DATESINPERIOD (
            Calendar[Date],
            MAX ( Calendar[Date] ),
            -12,
            MONTH
        )
    ),
    HASONEVALUE ([level03]),If([level 03]= "calculation:" && [level03]= "Cash:" && [level03]= "Costs:"=>0, Blank(), CALCULATE (
        [Local],
        DATESINPERIOD (
            Calendar[Date],
            MAX ( Calendar[Date] ),
            -12,
            MONTH
        )
)
        ),
        HASONEVALUE ( [level02] ), BLANK ()
    )

我想添加条件,如果杠杆03 =现金,计算和成本,则返回空白以删除子总行。我尝试了类似的东西,它不能正常工作,它给了我错误“列级别03的值无法在当前上下文中确定”。如何在level03列中添加该条件?

1 个答案:

答案 0 :(得分:2)

只是旁注,“它不能正常工作”在确定问题根源时非常无益。特别是在Power Pivot / Tabular模型中,this link provides a list of ways to help answerers help you

从语法上讲,存在一些错误。让我们解决这些问题,并在深入研究其他度量定义之前先看看行为是否合适。

这部分是问题所在:

...
,HASONEVALUE([level03])
    // Note, you are inconsistent in referring to [level03] and
    // [level 03]
,If( // Below you are testing column references against scalar
     // literals. DAX doesn't understand how to do this, so you
     // have to wrap each reference in a VALUES() function, which
     // can be implicitly converted to a scalar value
    [level 03] = 'calculation:'   // DAX needs double quotes: ""
        && [level03]= 'Cash:'     // You have multiple predicates
        && [level03]=' Costs:'=>0 // for a single field combined
                                  // with a logical and - these
                                  // can't all simultaneously be true
    ,Blank()
    ,CALCULATE(
        [Local]
        ,DATESINPERIOD(
            Calendar[Date],
            MAX ( Calendar[Date] ),
            -12,
            MONTH
        )
    )
)
,....

我只是将谓词组合更改为逻辑或重写,以便此IF()可以返回BLANK。上面写的方式,即使修复语法错误也会导致它总是评估else条件,因为[level 03]不能同时具有“计算:”,“现金:”和“成本:=> 0”的值”

...
,HASONEVALUE('<table>'[level03]) 
    // As a best practice, you should always use fully qualified
    // column references - 'table'[field]
    // Also, I've referred only to [level03] below
,IF( // Note we've wrapped [level 03] in VALUES() below - this will
     // allow our comparisons to scalar literals
    VALUES('<table>'[level03]) = "calculation:" // double quotes
        || VALUES('<table>'[level03]) = "Cash:"  
            // Note: we've changed the logical and, &&, to a logical
            // or, || - meaning if any 1 of these predicates is true,
            // we'll return BLANK
        || VALUES('<table>'[level03]) = " Costs:=>0"
    ,BLANK()
    ,CALCULATE(
        [Local]
        ,DATESINPERIOD(
            Calendar[Date]
            ,MAX( Calendar[Date] )
            ,-12
            ,MONTH
        )
    )
)
,....

这将检查数据透视表上[level03]只有一个不同值的任何单元格 - 如果这是真的,它将评估IF()函数。

IF()函数将检查[level03]的值。如果该值是以下三个值中的任何一个,“计算:”,“现金:”或“成本:=&gt; 0”,则它将返回BLANK。如果[level03]的值不是这三个中的任何一个,它将评估CALCULATE(),它返回滚动12个月期间的度量[Local]。