英文:
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.
答案1
得分: 3
你的公式是错误的,因为在你的DAX代码中,第3行会短路任何大于1
的Period
值并返回"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 "2"
.
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 <= 0, "-",
_period <= 1, "1 hour",
_period <= 2, "2 hours",
_period <= 3, "3 hours",
_period <= 4, "4 hours"
)
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论