Forum Discussion

tavolo89's avatar
tavolo89
Frequent Visitor
3 years ago

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:

 

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?
 

 

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

  • edhans's avatar
    edhans
    Icon for Community Champion rankCommunity Champion

    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.