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

Calling all Data Engineers! Fabric Data Engineer (Exam DP-700) live sessions are back! Starting October 16th. Sign up.

Reply
gofy758
Regular Visitor

Performance Issue due to Switch Function

I am having serious performance issue due to this measure. Anyone could help please to have an alternative that might not make the report slow? Just to let you know I am new to dax. thanks 🙂

Measure_NightMare  =
VAR caldate =
    SELECTEDVALUE ( 'Calendar'[DayOfWeek] ) 
RETURN
    SWITCH (
        TRUE (),
        caldate = 6,
            CALCULATE (
                SUMX ( 'Fact_TBL', 'Fact_TBL'[Col1] )
                    - SUMX ( 'Fact_TBL', 'Fact_TBL'[Col2] ),
                'Fact_TBL'[Value_Col] >= 40
                    && 'Fact_TBL'[Value_Col] <= 74
            ),
        caldate = 7,
            BLANK ()
       
        ,
        CALCULATE (
                SUMX ( 'Fact_TBL', 'Fact_TBL'[Col1] )
                    - SUMX ( 'Fact_TBL', 'Fact_TBL'[Col2] ),
                'Fact_TBL'[Value_Col] >= 38
                    && 'Fact_TBL'[Value_Col] <= 80
        )
    )
3 REPLIES 3
ppm1
Solution Sage
Solution Sage

That measure doesn't look that bad, and the suggested improvements should only help. How many rows in your fact table? More importantly, how many distinct values do you have in the Value_Col column? Is it whole number? or decimal?

 

Pat

Microsoft Employee
Greg_Deckler
Community Champion
Community Champion

@gofy758 You might try this:

 

Measure_NightMare  =
VAR caldate = SELECTEDVALUE ( 'Calendar'[DayOfWeek] )
RETURN
    SWITCH (
        TRUE(),
        caldate<6,
               VAR __Table = FILTER(ALLSELECTED('Fact_TBL'),[Value_Col] >= 40 && [Value_Col] <= 74)
            RETURN
                SUMX ( __Table, [Col1] ) - SUMX(__Table, [Col2] ),
        caldate = 6,
               VAR __Table = FILTER(ALLSELECTED('Fact_TBL'),[Value_Col] >= 40 && [Value_Col] <= 74)
            RETURN
                SUMX ( __Table, [Col1] ) - SUMX(__Table, [Col2] ),
            BLANK ()
    )

 



Follow on LinkedIn
@ me in replies or I'll lose your thread!!!
Instead of a Kudo, please vote for this idea
Become an expert!: Enterprise DNA
External Tools: MSHGQM
YouTube Channel!: Microsoft Hates Greg
Latest book!:
DAX For Humans

DAX is easy, CALCULATE makes DAX hard...

@gofy758 It's not clear to me why this is slow but I've struggled with SWITCH myself. You may want to check out some of the links referenced here.

 

It's possible rearranging the order of the terms could help if you start with the simplest case. That is:

 

Measure_NightMare =
VAR caldate =
    SELECTEDVALUE ( 'Calendar'[DayOfWeek] )
RETURN
    SWITCH (
        TRUE (),
        caldate = 7, BLANK (),
        caldate = 6,
            CALCULATE (
                SUMX ( 'Fact_TBL', 'Fact_TBL'[Col1] ) - SUMX ( 'Fact_TBL', 'Fact_TBL'[Col2] ),
                'Fact_TBL'[Value_Col] >= 40
                    && 'Fact_TBL'[Value_Col] <= 74
            ),
        CALCULATE (
            SUMX ( 'Fact_TBL', 'Fact_TBL'[Col1] ) - SUMX ( 'Fact_TBL', 'Fact_TBL'[Col2] ),
            'Fact_TBL'[Value_Col] >= 38
                && 'Fact_TBL'[Value_Col] <= 80
        )
    )

 

 

You could also try refactoring it using variables:

 

NightMare = 
VAR caldate =
    SELECTEDVALUE ( 'Calendar'[DayOfWeek] )
VAR case6 =
    CALCULATE (
        SUM ( 'Fact_TBL'[Col1] ) - SUM ( 'Fact_TBL'[Col2] ),
        'Fact_TBL'[Value_Col] >= 40,
        'Fact_TBL'[Value_Col] <= 74
    )
VAR other =
    CALCULATE (
        SUM ( 'Fact_TBL'[Col1] ) - SUM ( 'Fact_TBL'[Col2] ),
        'Fact_TBL'[Value_Col] >= 38,
        'Fact_TBL'[Value_Col] <= 80
    )
RETURN
    SWITCH (
        caldate,
        7, BLANK (),
        6, case6,
        other
    )

Helpful resources

Announcements
FabCon Global Hackathon Carousel

FabCon Global Hackathon

Join the Fabric FabCon Global Hackathon—running virtually through Nov 3. Open to all skill levels. $10,000 in prizes!

October Power BI Update Carousel

Power BI Monthly Update - October 2025

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

FabCon Atlanta 2026 carousel

FabCon Atlanta 2026

Join us at FabCon Atlanta, March 16-20, for the ultimate Fabric, Power BI, AI and SQL community-led event. Save $200 with code FABCOMM.