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

The ultimate Microsoft Fabric, Power BI, Azure AI & SQL learning event! Join us in Las Vegas from March 26-28, 2024. Use code MSCUST for a $100 discount. Register Now

Reply
PaulPannu
Frequent Visitor

Filtering Variable to Return Single Row

Hello All, 

I've come across an issue when attempting to set up a measure to be used in a switch function. Below is my current dax expression: 


Value =

VAR RatexFX=IF(HASONEFILTER(Account[Sub-Account3]),(Sales/Volume),SUMX(VALUES(Account[Sub-Account3]),(Sales/Volume))-[FX]
RETURN
Calculate(RatexFX,FILTER('Combined P&L','Combined P&L'[Sub-Account3]="Customer Sales"))

In the above measure, I am looking to utilize the RatexFX variable to only show the Rate xFX value for Customer Sales. When done in an individual measure, the desired result is given, but when using Variable, the filter in the Return function does nothing, and the Rate xFX function is shown for all accounts. 
9 REPLIES 9
lbendlin
Super User
Super User

the term "VAR"  is misleading, unfortunately.  It should have been named "CONST"  because that's what it is, a constant.

 

You need to move it inside the CALCULATE statement to be effective

 

 

Value =
Calculate(IF(HASONEFILTER(Account[Sub-Account3]),(Sales/Volume),SUMX(VALUES(Account[Sub-Account3]),(Sales/Volume))-[FX],FILTER('Combined P&L','Combined P&L'[Sub-Account3]="Customer Sales"))

 

Might even need to bring the switch out further.

 

Hi Ibendlin, 

Appreciate the insight. I provided the above details as a skimmed down version of what I am trying to calculate. In the end DAX expression, I am looking to use a SWITCH formula in a waterfall that then looks at multiple accounts. So in the end it would be: 

Value =

VAR RatexFX=IF(HASONEFILTER(Account[Sub-Account3]),(Sales/Volume),SUMX(VALUES(Account[Sub-Account3]),(Sales/Volume))-[FX]
RETURN
Switch(
1, Calculate(RatexFX,FILTER('Combined P&L','Combined P&L'[Sub-Account3]="Customer Sales")),
2, Calculate(RatexFX,FILTER('Combined P&L','Combined P&L'[Sub-Account3]="Marketing Expense")),
3, Calculate(RatexFX,FILTER('Combined P&L','Combined P&L'[Sub-Account3]="Employee Wages")), 
4, Calculate(RatexFX,FILTER('Combined P&L','Combined P&L'[Sub-Account3]="SG&A")),
BLANK()).

Thus, the goal of using the variable is to then not have to calculate RatexFX on every step of the SWITCH, but instead perform it once at the beginning. When using the variable, each step of the switch ignores the filter, and instead just displays total Rate xFX. 

Thanks




I already explained why that is the case. You cannot do it this way. Move the SWITCH inside the CALCULATE.

Can you show an example of what you mean by inside? Sorry, just a little confused on that. 

instead of SWITCH(...,CALCULATE())  you need to do CALCULATE(SWITCH(...))

 

Please provide sanitized sample data that fully covers your issue. If you paste the data into a table in your post or use one of the file services it will be easier to assist you. I cannot use screenshots of your source data.

Please show the expected outcome based on the sample data you provided. Screenshots of the expected outcome are ok.

https://community.powerbi.com/t5/Desktop/How-to-Get-Your-Question-Answered-Quickly/m-p/1447523

As a test I have written the below and got the following error:

Test Value =
CALCULATE([Rate xFX],SWITCH(VALUES('NS Account Table'[Index]),
1, FILTER('Combined P&L','Combined P&L'[Sub Account 3]="Customer Sales"),
2, FILTER('Combined P&L','Combined P&L'[Sub Account 3]="CPU Allowance"),
3, FILTER('Combined P&L','Combined P&L'[Sub Account 3]="Slotting"),
4, FILTER('Combined P&L','Combined P&L'[Sub Account 3]="Coupons"),
BLANK()))




A function 'FILTER' has been used in a True/False expression that is used as a table filter expression. This is not allowed.

Hi @PaulPannu ,

 

There a syntax error in your formula.

syntax of SWITCH()

SWITCH(<expression>, <value>, <result>[, <value>, <result>]…[, <else>])

 

You missed <expression> in your code.

 

Value =

VAR RatexFX=IF(HASONEFILTER(Account[Sub-Account3]),(Sales/Volume),SUMX(VALUES(Account[Sub-Account3]),(Sales/Volume))-[FX]
RETURN
Switch( selectedvalue( waterfall's X-axis ),
1, Calculate(RatexFX,FILTER('Combined P&L','Combined P&L'[Sub-Account3]="Customer Sales")),
2, Calculate(RatexFX,FILTER('Combined P&L','Combined P&L'[Sub-Account3]="Marketing Expense")),
3, Calculate(RatexFX,FILTER('Combined P&L','Combined P&L'[Sub-Account3]="Employee Wages")), 
4, Calculate(RatexFX,FILTER('Combined P&L','Combined P&L'[Sub-Account3]="SG&A")),
BLANK()).
 
If you need more help, please share your example data and expect result.
 

Best Regards

Community Support Team _ chenwu zhu

 

If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

 

 

 

Hi, thanks for the tip, but I believe the issue is continuing to persist even with the above solution. 

Below you can see the comparison of two DAX expressions: 

Test Value = 

VAR RatexFX = [Rate xFX]
RETURN
Calculate(RatexFX,FILTER(Account,Account[Sub-Account1]="Customer Sales"))

Rate xFX LP =
CALCULATE (
'Combined P&L'[Rate xFX],
Account[Sub-Account1] = "Customer Sales")
As you can see in the below table, test value continues to do the calculation for all accounts.

PaulPannu_0-1654608120880.png

 

Hi @PaulPannu ,

 

 

Measures cannot refer to variables defined outside the measure expression, but can refer to functional scope variables defined within the expression.

 

VAR keyword (DAX) - DAX | Microsoft Docs

 

Can you share some example data or pbix file without sensitive data and expect result.

 

Best Regards

Community Support Team _ chenwu zhu

 

If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

Helpful resources

Announcements
Fabric Community Conference

Microsoft Fabric Community Conference

Join us at our first-ever Microsoft Fabric Community Conference, March 26-28, 2024 in Las Vegas with 100+ sessions by community experts and Microsoft engineering.

February 2024 Update Carousel

Power BI Monthly Update - February 2024

Check out the February 2024 Power BI update to learn about new features.

Fabric Career Hub

Microsoft Fabric Career Hub

Explore career paths and learn resources in Fabric.

Fabric Partner Community

Microsoft Fabric Partner Community

Engage with the Fabric engineering team, hear of product updates, business opportunities, and resources in the Fabric Partner Community.

Top Solution Authors