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

Power BI is turning 10! Let’s celebrate together with dataviz contests, interactive sessions, and giveaways. Register now.

Reply
Guido_Beulen
Helper I
Helper I

Measure in SWITCH function suddenly ignores Date context: outside of switch it works. Why is this?

Hi all, 

 

I have a measure that calculates the latest score for a variable (expressed as a percentage) based on the last available date that has data, looking like:

2.8.2. Latest Score =

CALCULATE(

[Gemiddelde beschikbaarheid], FILTER(ALLSELECTED('0 DIM Datum'),'0 DIM Datum'[Date]>=MIN('0 DIM Datum'[Date]) && '0 DIM Datum'[Date]<=MAX('0 DIM Datum'[Date])),FILTER('1 2 Maas koffie details','1 2 Maas koffie details'[Datum] = MAX('1 2 Maas koffie details'[Datum]))).

This measure in a table or card work fine with my date slicer and returns the latest value available in the selected time period. 

 

When I put this in my SWITCH function, it suddenly ignores date context. The SWITCH function looks like:

 

Most Recent Score =

VAR Mindate = MIN('0 DIM Datum'[Date])

VAR Maxdate = MAX('0 DIM Datum'[Date])

RETURN

SWITCH(

TRUE(),

[2.8.1.], '1 Kwaliteit'[Koppelcode KPI] = "2.8.2.",

FORMAT([2.8.2. Latest Score],"00.0%"),

BLANK() )

 

I also tried hardcoding the date context in the SWITCH function, but it still ignores the slicer: 

/FORMAT(CALCULATE( [Maas Gemiddelde beschikbaarheid],FILTER(ALLSELECTED('0 DIM Datum'),'0 DIM Datum'[Date]>=Mindate && '0 DIM Datum'[Date]<=Maxdate), FILTER('1 2 Maas koffie details','1 2 Maas koffie details'[Datum] = MAX('1 2 Maas koffie details'[Datum]))),"00.0%")

2 REPLIES 2
Guido_Beulen
Helper I
Helper I

debug KPI LM.png

Here's a picture of the measure in the table, with the Debug measures and the slicer around. Outside of the SWITCH-table it shows as 99,6 which is correct, inside it says 99,8 which is the Q2 value. 

Hi @Guido_Beulen 

The problem occurs because the SWITCH function evaluates its conditions before the rest of the measure calculation, the inner CALCULATE in your [2.8.2. Latest Score] measure loses the outer filter context when evaluated inside SWITCH and your date variables (Mindate/Maxdate) are evaluated in a different context than expected

Try to use KEEPFILTERS to preserve context

Most Recent Score =
VAR Mindate = MIN('0 DIM Datum'[Date])
VAR Maxdate = MAX('0 DIM Datum'[Date])
RETURN
SWITCH(
    TRUE(),
    [2.8.1.], '1 Kwaliteit'[Koppelcode KPI] = "2.8.2.",
    FORMAT(
        CALCULATE(
            [Gemiddelde beschikbaarheid],
            KEEPFILTERS('0 DIM Datum'[Date] >= Mindate && '0 DIM Datum'[Date] <= Maxdate),
            FILTER(
                '1 2 Maas koffie details',
                '1 2 Maas koffie details'[Datum] = MAX('1 2 Maas koffie details'[Datum])
            )
        ),
        "00.0%"
    ),
    BLANK()
)

Helpful resources

Announcements
Join our Fabric User Panel

Join our Fabric User Panel

This is your chance to engage directly with the engineering team behind Fabric and Power BI. Share your experiences and shape the future.

June 2025 Power BI Update Carousel

Power BI Monthly Update - June 2025

Check out the June 2025 Power BI update to learn about new features.

June 2025 community update carousel

Fabric Community Update - June 2025

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