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

Get certified in Microsoft Fabric—for free! For a limited time, the Microsoft Fabric Community team will be offering free DP-600 exam vouchers. Prepare now

Reply
SamuelDesguin
Regular Visitor

Using SWITCH with variables and filters in measures

Dear all,
I have been running into a problem for which I could not find a solution here. Basically, I want to switch views between different ByCountry view or ByProduct view, depending on an unconnected slicer. The following formula has been working like a charm (here I put generic names to make it clearer):

 

SWITCH(
    VALUES(Slicer[DesiredFilter]), 
        "ByCountry",CALCULATE([TotalSales], TREATAS(VALUES(Slicer[Value]),'Sales'[Country])),
        "ByProduct",CALCULATE([TotalSales], TREATAS(VALUES(Slicer[Value]),'Sales'[Product])),
        [TotalSales])

 

To take it further (to be able to combine filters), I would now like to store my filters within a variable. It works when I simply store a filter in a variable, like so:

 

var SelectedFilter = TREATAS(VALUES(Slicer[Value]),'Sales'[Country])
return 
CALCULATE([TotalSales],SelectedFilter)

 

But if I want to switch between filters within my variable, it doesn't work anymore. I'm trying like this:

 

var SelectedFilter = SWITCH(VALUES(Slicer[Key]), 
        "ByCountry",TREATAS(VALUES(Slicer[Value]),'Sales'[Country]),
        "ByProduct",TREATAS(VALUES(Slicer[Value]),'Sales'[Product]),
        ALLSELECTED())
return
CALCULATE([TotalSales],SelectedFilter)

 

But got the message: "The expression refers to multiple columns. Multiple columns cannot be converted to a scalar value.

Can anyone help me with this? Any help is greatly appreciated!



1 ACCEPTED SOLUTION

You can't return a table in a SWITCH (or an IF), which is the problem with your measure.  You'll need to switch between full expressions (like Amit suggested).  You could explore using CROSSJOIN to precombine your filters or using Calculation Groups, but I don't know if that would work.

 

Pat

 





Did I answer your question? Mark my post as a solution! Kudos are also appreciated!

To learn more about Power BI, follow me on Twitter or subscribe on YouTube.


@mahoneypa HoosierBI on YouTube


View solution in original post

4 REPLIES 4
amitchandak
Super User
Super User

@SamuelDesguin , Try like

 

SWITCH(
    selectedvalue(Slicer[DesiredFilter]), 
        "ByCountry",CALCULATE([TotalSales], TREATAS(VALUES(Slicer[Value]),'Sales'[Country])),
        "ByProduct",CALCULATE([TotalSales], TREATAS(VALUES(Slicer[Value]),'Sales'[Product])),
        [TotalSales])
Join us as experts from around the world come together to shape the future of data and AI!
At the Microsoft Analytics Community Conference, global leaders and influential voices are stepping up to share their knowledge and help you master the latest in Microsoft Fabric, Copilot, and Purview.
️ November 12th-14th, 2024
 Online Event
Register Here

Hi @amitchandak !
Thanks for trying to solve this.
This is correct but does not solve my problem, because what I wanted to do is to isolate the filters in variables so that I could cross multiple filters in my calculate at the end. Eventually, it would look kind of like this:

Var Filter1 = ...
Var Filter2 = ...
Var Filter3 = ...
Var SelectedFilter1 = SWITCH.. (selects one of the filters above)
Var SelectedFilter2 = SWITCH.. (selects one of the filters above)
Return CALCULATE([TotalSales], SelectedFilter1, SelectedFilter2)

And my problem here is with this SWITCH function that apparently does not return what my CALCULATE function would like as filters. Any ideas for that? Thanks!

You can't return a table in a SWITCH (or an IF), which is the problem with your measure.  You'll need to switch between full expressions (like Amit suggested).  You could explore using CROSSJOIN to precombine your filters or using Calculation Groups, but I don't know if that would work.

 

Pat

 





Did I answer your question? Mark my post as a solution! Kudos are also appreciated!

To learn more about Power BI, follow me on Twitter or subscribe on YouTube.


@mahoneypa HoosierBI on YouTube


Aaah right @mahoneypat , that makes sense. I'll try another way, then ! Thanks a lot.

Helpful resources

Announcements
OCT PBI Update Carousel

Power BI Monthly Update - October 2024

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

September Hackathon Carousel

Microsoft Fabric & AI Learning Hackathon

Learn from experts, get hands-on experience, and win awesome prizes.

October NL Carousel

Fabric Community Update - October 2024

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