DAX VAR在度量代码的中间定义命名变量 [英] DAX VAR defining named variables in the middle of the measure code

查看:151
本文介绍了DAX VAR在度量代码的中间定义命名变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

已在此处向我介绍奇怪地利用了 VAR 函数。到目前为止,我在小数开始时在等号之后就遇到了VAR。请参见下面的代码:

I have been presented here with a bizarre exploitation of VAR function. So far I have encountered VAR in the beginning of the measure, right after the equal sign. See the code below:

Expected Result =
SUMX (
    VALUES ( Unique_Manager[Manager] ),
    VAR SumBrand = CALCULATE ( SUM ( Budget_Brand[BudgetBrand] ) )
    VAR SumProduct = CALCULATE ( SUM ( Budget_Product[BudgetProduct] ) )
    RETURN
        IF ( ISBLANK ( SumProduct ), SumBrand, SumProduct )
)

此处VAR嵌套在度量代码的深处。不用说,这是VAR的神奇功能。我一直在尝试在运气方面使用该功能。例如,这可行:

Here VAR is nested deeply inside the measure code. Needless to say that this is a fantastic feature of VAR. I have been trying to use that feature in my measures with no luck. For example, this works:

Measure_good = CALCULATE( 
    SUM(table[Amount])
)

虽然这不起作用:

Measure_bad = CALCULATE( 
    VAR inside_measure = SUM(table[Amount])
)

以这种不寻常的方式使用VAR的规则是什么?

What are the rules for using VAR in this unusual way?

推荐答案

VAR语句需要匹配的 RETURN 。您可以一个接一个地声明多个变量,但是必须使用 RETURN 命令将变量传递回主计算,然后可以在其中将变量用于求值。 IF语句之类的

A VAR statement needs a matching RETURN. You can declare several variables after one another, but then you must use a RETURN command to pass the variables back to the main calculation, where the variable can then be used for evaluation in an IF statement or whatever

我还没有测试过,但是您的Measure_bad应该是这样的(尽管如果剩下的全部使用VAR则没有多大意义公式的返回变量)

I have not tested this, but your Measure_bad should be like this (although it doesn't make much sense to use a VAR if all the remainder of the formula returns the variable)

Measure_bad = CALCULATE( 
    VAR inside_measure = SUM(table[Amount])
    RETURN
    inside_measure
)

这篇关于DAX VAR在度量代码的中间定义命名变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆