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

Register now to learn Fabric in free live sessions led by the best Microsoft experts. From Apr 16 to May 9, in English and Spanish.

Reply
avachris
Frequent Visitor

Change Filter with and if statement

Hi I would like to create a measure where the filter context changes based upon what is the selected value. 

I basically want to apply a filter for certain rows in another table and remove the filter for rows where the selectedvalue is not "x"

for example:

 

example measure=

var filtervalues = if selectedvalue="x" then treatas({"filter1","filter2"},dimtable[dim]) else all(dimtable))

 

return calculate ([measure],filtervalues)

 

I was having an issue with the if statement returning a table and I'm not sure how to get around this. 

1 ACCEPTED SOLUTION

Hi @avachris 

 

Both IF and SWITCH are intended to return scalar results and don't support table results.

 

As a workaround, you can return different scalar expressions which contain different filters according to the conditions. This is supported. For example, 

Example measure =
IF (
    SELECTEDVALUE ( 'table'[column] ) = "x",
    CALCULATE ( [measure], TREATAS ( { "filter1", "filter2" }, dimtable[dim] ) ),
    CALCULATE ( [measure], ALL ( dimtable ) )
)

 

Best Regards,
Community Support Team _ Jing
If this post helps, please Accept it as Solution to help other members find it.

View solution in original post

3 REPLIES 3
FreemanZ
Super User
Super User

hi @avachris 

unable to follow your reasoning. dataset with expected result would be more telling.

Hi basically I am trying to set a dynamic table variable so that depending on a condition a different filter table is applied within the calculation context. 

I was trying to do this with a switch or an if but i think creating a variable via an iterator makes sense 

Hi @avachris 

 

Both IF and SWITCH are intended to return scalar results and don't support table results.

 

As a workaround, you can return different scalar expressions which contain different filters according to the conditions. This is supported. For example, 

Example measure =
IF (
    SELECTEDVALUE ( 'table'[column] ) = "x",
    CALCULATE ( [measure], TREATAS ( { "filter1", "filter2" }, dimtable[dim] ) ),
    CALCULATE ( [measure], ALL ( dimtable ) )
)

 

Best Regards,
Community Support Team _ Jing
If this post helps, please Accept it as Solution to help other members find it.

Helpful resources

Announcements
Microsoft Fabric Learn Together

Microsoft Fabric Learn Together

Covering the world! 9:00-10:30 AM Sydney, 4:00-5:30 PM CET (Paris/Berlin), 7:00-8:30 PM Mexico City

PBI_APRIL_CAROUSEL1

Power BI Monthly Update - April 2024

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

April Fabric Community Update

Fabric Community Update - April 2024

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

Top Solution Authors