Skip to main content
cancel
Showing results for 
Search instead for 
Did you mean: 

Enhance your career with this limited time 50% discount on Fabric and Power BI exams. Ends September 15. Request your voucher.

Reply
ptmuldoon
Resolver I
Resolver I

Using variable result in another Variable?

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") )
	)

 

3 REPLIES 3
ptmuldoon
Resolver I
Resolver I

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"
    )

 

 

dharmendars007
Super User
Super User

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

LinkedIN 

DataInsights
Super User
Super User

@ptmuldoon,

 

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 





Did I answer your question? Mark my post as a solution!

Proud to be a Super User!




Helpful resources

Announcements
August Power BI Update Carousel

Power BI Monthly Update - August 2025

Check out the August 2025 Power BI update to learn about new features.

August 2025 community update carousel

Fabric Community Update - August 2025

Find out what's new and trending in the Fabric community.