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

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

Reply
LucMarv
Helper I
Helper I

Run a block of code within a SWITCH()

Hi, 

All the examples of a switch include a very simple structure... 

For example:

= SWITCH([Month], 1, "January", 2, "February", 3, "March", 4, "April" , 5, "May", 6, "June", 7, "July", 8, "August" , 9, "September", 10, "October", 11, "November", 12, "December" , "Unknown month number" )

 

I have two blocks of code, and I only want to run one, depending on a condition. How do I run a BLOCK of code within a SWITCH()?

 

MEASURE_NAME =

SWITCH(

TRUE(),

 

SELECTEDVALUE(grouping[group]) = "all",

var total = CALCULATE(sum(data[value]), FILTER(data, [sex] <> "Persons" && ALL(grouping)))

var females = calculate(sum(data[value]), FILTER(data, [sex] = "Female" && ALL(grouping)))

 

,

SELECTEDVALUE(grouping[group]) <> "all",

var total = CALCULATE(sum(data[value]), FILTER(data, [sex] <> "Persons" ))

var females = calculate(sum(data[value]), FILTER(data, [sex] = "Female"))

)

 

RETURN

format(DIVIDE(females, total), "0.0%")

 

 

It seems to complain about the ',' in between the blocks, indicating to me that it struggles to evaluate multiple rows when the condition in the switch is true. I tried to put the blocks (in bold) in (  ) but that does not help.

 

Any suggestions? 

2 ACCEPTED SOLUTIONS
jdbuchanan71
Super User
Super User

Try writing them as two seperate measures then using the measures in the switch.

Measure_Name =
VAR _Seletcion =
    SELECTEDVALUE ( grouping[group] )
RETURN
    SWITCH (
        TRUE (),
        _Selection = "all", [All Measure],
        _Selection <> "all", [Other Measure]
    )

View solution in original post

Yeah, this solution works and I have implemented this. I am not a big fan of it though, because I dont really like creating so many measures. I have a relatively simple app, and have created over 100 measures already... 

It works, but its not neat.

Thanks for the guidance!

View solution in original post

2 REPLIES 2
jdbuchanan71
Super User
Super User

Try writing them as two seperate measures then using the measures in the switch.

Measure_Name =
VAR _Seletcion =
    SELECTEDVALUE ( grouping[group] )
RETURN
    SWITCH (
        TRUE (),
        _Selection = "all", [All Measure],
        _Selection <> "all", [Other Measure]
    )

Yeah, this solution works and I have implemented this. I am not a big fan of it though, because I dont really like creating so many measures. I have a relatively simple app, and have created over 100 measures already... 

It works, but its not neat.

Thanks for the guidance!

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.