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
tavolo89
Frequent Visitor

SWITCH statement based on a measure is not working

I've created the following three measures to try and assign a percentile to products based on the number of views.

 

ItemRank_Views = IF (
    NOT ( ISBLANK ( [# Product Views] ) ),
    RANKX (
        FILTER ( ALLSELECTED ('dim Product'[Item ID] ), NOT ( ISBLANK ( [# Product Views] ) ) ),
        [# Product Views], ,asc
    )
)
 
MaxRank_Views = calculate([ItemRank_Views], ALL('dim Product'))
 

 

Percentile - Product Views = ([ItemRank_Views]-1)/([MaxRank_Views]-1)

This can be seen below:
tavolo89_0-1679682393163.png

 

What I'm trying to do is then assign a "bucket" to each of those. For example, products in the 90% to 100% percentiles are called "top 10%". This is my switch statement, created as a calculated column:

 

Percentile Band - Views = SWITCH(TRUE(),
    [Percentile - Product Views] > 0.9 && [Percentile - Product Views] <= 1, "Top 10%",
    [Percentile - Product Views] > 0.8 && [Percentile - Product Views] <= 0.9, "Second 10%",
    [Percentile - Product Views] > 0 && [Percentile - Product Views] <= 0.8, "Whatever",
    [Percentile - Product Views] <0, "wtf",
    "Unknown Band")
 
 
But everything is being returned as "Unknown Band". Why is this?
 
tavolo89_1-1679682799478.png

 

This statement returns the expected result if I use in a measure (rather than a column). But I want to be able to use the value to slice the data so that isn't helpful. I also don't understand why.

 

Grateful for any help or advice here.

 

1 REPLY 1
edhans
Super User
Super User

When you use a measure inside of a column, you are only going to get the results of that measure for that one row. That is part of what Context Transition is doing. You need to wrap that measure with CALCULATE() and add some REMOVEFILTERS() logic to negate the effects of the context transition. You'll need to play with the REMOVEFILTERS though to find out what has to be cleared. I would ignore the SWITCH for now, and just drop those measures directly in the column and look at the results. Then wrap it with CALCULATE([your measure], REMOVEFILTERS(sometable), REMOVEFITLERS(someothertable[somecolumn0)) until it returns what you want. Then reassemble your SWITCH function.



Did I answer your question? Mark my post as a solution!
Did my answers help arrive at a solution? Give it a kudos by clicking the Thumbs Up!

DAX is for Analysis. Power Query is for Data Modeling


Proud to be a Super User!

MCSA: BI Reporting

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.