DAX VAR 在度量代码中间定义命名变量 [英] DAX VAR defining named variables in the middle of the measure code
问题描述
我已在这里 奇怪地利用了 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屋!