The ultimate Fabric, Power BI, SQL, and AI community-led learning event. Save €200 with code FABCOMM.
Get registeredEnhance your career with this limited time 50% discount on Fabric and Power BI exams. Ends September 15. Request your voucher.
I am trying to use the result of 1 variable into a 2nd variable in computing a value. I know I can do this with nested Switch Statements, but trying to see if this methos would work as well.
In the below, I am trying to replace [QTY] value in the Var MyItem and in the Return dependent upon what is selected from the Var ItemValue. Yet, it returns an incorrect value when I try it.
Can anyone maybe help troubleshoot the below? Is it even possible?
Thank you,
Paul
Occupied Rooms =
VAR ItemValue =
SWITCH (
VALUES ('Measures List'[Fact Period]),
"Day", [QTY],
"Last 7", [Last 7 QTY],
"MTD", [MTD QTY],
"QTD", [QTD QTY],
"YTD", [YTD QTY]
)
Var Myitem = CALCULATE([QTY],'Dim Item'[description]="OCCUPIED ROOMS")
RETURN
SWITCH (
VALUES ('FactType'[FactType]),
"#", Myitem,
"% of Avail. Rooms", DIVIDE( Myitem, CALCULATE([QTY],'Dim Item'[description]="AVAILABLE ROOMS") )
)
I'm definitely struggling with this today, and I think the issue may have something to do with the ability to pass a variable meaure into the Calculate function?
I tried with the field paramater, and also tried @dharmendars007 code, and get the same incorrect value.
If I place the Switch statement directly within the Calculate function, it works as expected. But if change out the Switch statement with variable name in the Calculate, it returns an incorrect value?
Can you use a variable measure in Calculate?
EDIT. Pretty sure you can't use the Variable in the Calculate, and finally found an explanation here that makes sense. The Variable gets totaled before the filtering in the Calculate, thus the wrong response is given.
https://radacad.com/caution-when-using-variables-in-dax-and-power-bi
Occupied Rooms =
VAR _Period = SWITCH (
VALUES ('Measures List'[Fact Period]),
"Day", [QTY],
"Last 7", [Last 7 QTY],
"MTD", [MTD QTY],
"QTD", [QTD QTY],
"YTD", [YTD QTY]
)
RETURN
CALCULATE(
SWITCH (
VALUES ('Measures List'[Fact Period]),
"Day", [QTY],
"Last 7", [Last 7 QTY],
"MTD", [MTD QTY],
"QTD", [QTD QTY],
"YTD", [YTD QTY]
)
,'Dim Item'[description]="OCCUPIED ROOMS"
)
Hello @ptmuldoon ,
You can try the below code..
Occupied Rooms =
VAR ItemValue =
SWITCH (
VALUES ('Measures List'[Fact Period]),
"Day", [QTY],
"Last 7", [Last 7 QTY],
"MTD", [MTD QTY],
"QTD", [QTD QTY],
"YTD", [YTD QTY])
VAR MyItem =
CALCULATE(ItemValue, 'Dim Item'[description] = "OCCUPIED ROOMS")
RETURN
SWITCH (
VALUES ('FactType'[FactType]),"#", MyItem,"% of Avail. Rooms", DIVIDE( MyItem, CALCULATE([QTY], 'Dim Item'[description] = "AVAILABLE ROOMS") ))
If you find this helpful , please mark it as solution which will be helpful for others and Your Kudos/Likes are much appreciated!
Thank You
Dharmendar S
Field parameters offer a robust way to capture the selected measure. You could create a field parameter using the five measures in variable ItemValue.
https://learn.microsoft.com/en-us/power-bi/create-reports/power-bi-field-parameters
Proud to be a Super User!
User | Count |
---|---|
15 | |
12 | |
8 | |
7 | |
7 |
User | Count |
---|---|
24 | |
21 | |
12 | |
10 | |
7 |