Power BI,Excel嵌套IF语句

huangapple go评论138阅读模式
英文:

Power bi , excel nested if statement

问题

I'm having 1 column containing the number of hours, and I'm trying to write a nested if statement in Excel (or Power BI since both have the same syntax), but it seems the code never reaches the other condition.

根据图片显示,嵌套的if语句和switch函数都不起作用。

英文:

im having 1 column contains number of hours, and i'm trying write down nested if statement in excel (or power bi since both have same syntax) but seems the code never reach other condition.

as per the image shows niether nested if nor the switch function works.

Power BI,Excel嵌套IF语句

答案1

得分: 3

你的公式是错误的,因为在你的DAX代码中,第3行会短路任何大于1Period值并返回"2"

你需要的是每个连续的测试都应用一个逐渐不那么严格的约束条件,朝着一个方向:

ppp = 
VAR _period = Sheet1[Period]
RETURN
SWITCH ( 
   TRUE (),
   _period <= 0, "-",
   _period <= 1, "1 小时",
   _period <= 2, "2 小时",
   _period <= 3, "3 小时",
   _period <= 4, "4 小时"
)
英文:

Your formula is wrong, since row 3 in your DAX code short circuits any value of Period larger than 1 to return &quot;2&quot;.

What you need is something where each successive test applies a gradually less strict constraint, in one direction:

ppp = 
VAR _period = Sheet1[Period]
RETURN
SWITCH ( 
   TRUE (),
   _period &lt;= 0, &quot;-&quot;,
   _period &lt;= 1, &quot;1 hour&quot;,
   _period &lt;= 2, &quot;2 hours&quot;,
   _period &lt;= 3, &quot;3 hours&quot;,
   _period &lt;= 4, &quot;4 hours&quot;
)

huangapple
  • 本文由 发表于 2023年6月13日 03:20:56
  • 转载请务必保留本文链接:https://go.coder-hub.com/76459721.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定