更新DAX中变量的值
我需要更改DAX中开关中变量的值:
switch( true(),
condition1,
var test1 = 2
var test2 = 3
,condition 2,
var test1 = 4
var test2 = 5
,
var test1 = 7
var test2 = 6
)
我需要这样做,因为我必须根据条件更改大量变量,并且我不想为每个变量提供一个开关。
我已经尝试了使用Switch的方法,并且它没有变量。 就像DAX不允许您在第一次分配变量后更改的值。
编辑
你好,这是我要实现的示例:
Value Daily Form =
SWITCH( true(),
// if both are selected
(ISFILTERED(tblAAA[AAA_name]) && ISFILTERED(tblBBB[BBB_name])),
var Target = sum(F_Daily_AAA_BBB[target])
var TotalPayments = sum(F_Daily_AAA_BBB[vlr_total_payment])
// if AAA is selected
, (ISFILTERED(tblAAA[AAA_name]) && not(ISFILTERED(tblBBB[BBB_name]))),
var Target = sum(F_Daily_AAA[target])
var TotalPayments = sum(F_Daily_AAA[vlr_total_payment])
// if BBB is selected
, (not(ISFILTERED(tblAAA[AAA_name])) && ISFILTERED(tblBBB[BBB_name])),
var Target = sum(F_Daily_BBB[target])
var TotalPayments = sum(F_Daily_BBB[vlr_total_payment])
// none is selected
,
var Target = sum(F_Daily_OR[target])
var TotalPayments = sum(F_Daily_OR[vlr_total_payment])
)
var result =
SWITCH(FIRSTNONBLANK(tblKPIs[Group_Name], tblKPIs[Group_Name]),
"Target (€)", IF(Target > 0, FORMAT(Target, "€ #,##"), if(SELECTEDVALUE('Calendar'[isWorkingDay]) = 1, "€ 0", BLANK())),
"Total Payments (€)", IF(TotalPayments > 0, FORMAT(TotalPayments, "€ #,##"), IF(SELECTEDVALUE('Calendar'[isWorkingDay]) = 1, "€ 0", BLANK())),
)
return result
一开始我要修改的代码是:
Value Daily Form =
var Target = sum(F_Daily_OR[target])
var TotalPayments = sum(F_Daily_OR[vlr_total_payment])
var result =
SWITCH(FIRSTNONBLANK(tblKPIs[Group_Name], tblKPIs[Group_Name]),
"Target (€)", IF(Target > 0, FORMAT(Target, "€ #,##"), if(SELECTEDVALUE('Calendar'[isWorkingDay]) = 1, "€ 0", BLANK())),
"Total Payments (€)", IF(TotalPayments > 0, FORMAT(TotalPayments, "€ #,##"), IF(SELECTEDVALUE('Calendar'[isWorkingDay]) = 1, "€ 0", BLANK())),
)
return result
唯一的区别是,而不是2个变量,大约有75个变量, 而且我不想为它们做一个切换,因为这会占用代码。
另外,我必须像50个充满变量的指标一样修改,因此我需要一个快速复制粘贴解决方案,例如该switch,在其他情况下,代码没有变量。
感谢您抽出宝贵的时间来帮助我。
I need to change the value of variables in a switch in dax:
switch( true(),
condition1,
var test1 = 2
var test2 = 3
,condition 2,
var test1 = 4
var test2 = 5
,
var test1 = 7
var test2 = 6
)
I need to do this because I have to change massive number of variables depending on the condition, and I don't want to have a switch for every single variable.
I already tried this approach with switch and it works without variables.
It's like dax does not allow you to change the value of a variable after the first time it was assigned.
edit
hello, here is the example of what I want to achieve:
Value Daily Form =
SWITCH( true(),
// if both are selected
(ISFILTERED(tblAAA[AAA_name]) && ISFILTERED(tblBBB[BBB_name])),
var Target = sum(F_Daily_AAA_BBB[target])
var TotalPayments = sum(F_Daily_AAA_BBB[vlr_total_payment])
// if AAA is selected
, (ISFILTERED(tblAAA[AAA_name]) && not(ISFILTERED(tblBBB[BBB_name]))),
var Target = sum(F_Daily_AAA[target])
var TotalPayments = sum(F_Daily_AAA[vlr_total_payment])
// if BBB is selected
, (not(ISFILTERED(tblAAA[AAA_name])) && ISFILTERED(tblBBB[BBB_name])),
var Target = sum(F_Daily_BBB[target])
var TotalPayments = sum(F_Daily_BBB[vlr_total_payment])
// none is selected
,
var Target = sum(F_Daily_OR[target])
var TotalPayments = sum(F_Daily_OR[vlr_total_payment])
)
var result =
SWITCH(FIRSTNONBLANK(tblKPIs[Group_Name], tblKPIs[Group_Name]),
"Target (€)", IF(Target > 0, FORMAT(Target, "€ #,##"), if(SELECTEDVALUE('Calendar'[isWorkingDay]) = 1, "€ 0", BLANK())),
"Total Payments (€)", IF(TotalPayments > 0, FORMAT(TotalPayments, "€ #,##"), IF(SELECTEDVALUE('Calendar'[isWorkingDay]) = 1, "€ 0", BLANK())),
)
return result
in the beginning the code that I have to modify is:
Value Daily Form =
var Target = sum(F_Daily_OR[target])
var TotalPayments = sum(F_Daily_OR[vlr_total_payment])
var result =
SWITCH(FIRSTNONBLANK(tblKPIs[Group_Name], tblKPIs[Group_Name]),
"Target (€)", IF(Target > 0, FORMAT(Target, "€ #,##"), if(SELECTEDVALUE('Calendar'[isWorkingDay]) = 1, "€ 0", BLANK())),
"Total Payments (€)", IF(TotalPayments > 0, FORMAT(TotalPayments, "€ #,##"), IF(SELECTEDVALUE('Calendar'[isWorkingDay]) = 1, "€ 0", BLANK())),
)
return result
The only difference is the instead of 2 variables there are like 75,
and I don't want to make a switch for each of them because this will spaghettify the code.
Also I have to modify like 50 of this Metrics full of variables, so I need a quick copy paste solution like that switch, which worked in other cases in which the code didn't have variables.
Thank you for taking the time to help me.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
确实是,因为DAX中的变量实际上是常数。变量是不可变的。您可以将值存储在变量中,但以后不能更改。
这是文档中DAX变量的定义:
在
您到底想实现什么?
为每个条件创建单独的度量,然后另一个带有开关条件的措施对您不起作用?
It is indeed, because variables in DAX are actually constants. Variables are immutable. You can store the value in variable but you cannot change it later.
Here is a definition of the DAX variable from the documentation:
Find more in the documentation.
What exactly do you want to achieve by this?
Creating separate measure for each condition and then another measure with switch condition won't work for you?