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
jkoclejda
Helper I
Helper I

SWITCH + SELECTEDVALUE function doesn't work

Hi! I have a table with date, income statement categories and values. The table looks like below:

 

datecategoryvalue
2023-01-01margin1000
2023-01-01income3000
2023-01-01percentage margin 
2023-01-02margin1200
2023-01-02income5000
2023-01-02percentage margin 

 

I made a measure to create income statement:

statement = 
SWITCH(
  SELECTEDVALUE(table_name[category]),

  "percentage margin", [margin] / [income],

  SUM(table_name[value])
)

[margin] = CALCULATE(SUM(table_name[value]), table_name[category] = "margin")
[income] = CALCULATE(SUM(table_name[value]), table_name[category] = "income")

 

Unfortunately, as a result, I have an empty value under the "percentage margin" category. 

The function only worked when I created a separate table with categories with no relation to the table with values. Then the measure looked like this:

statement = 
SWITCH(
  SELECTEDVALUE(table_name_2[category]),

  "margin", [margin],

  "percentage margin", [margin] / [income],

  "income", [income]

)

 

But it isn't an option because the real table has much more categories and referring to each one slows down the entire visualization greatly.

 

I have no idea what could be wrong, that's why I'm asking for help or some advice.

2 REPLIES 2
Anonymous
Not applicable

Hi @jkoclejda ,

 

Has your problem been solved? If the problem has been solved you can mark the reply for the standard answer to help the other members find it more quickly. If not, please point it out. Thanks in advance.

 

Best Regards,

Neeko Tang

If this post  helps, then please consider Accept it as the solution  to help the other members find it more quickly. 

DallasBaba
Skilled Sharer
Skilled Sharer

Hi @jkoclejda 

Using a DIVIDE function will return a blank value if the denominator is zero or empty, which is a better approach than using the / operator sign.

 

Please try the following:

statement =
SWITCH(
SELECTEDVALUE(table_name[category]),

"percentage margin", DIVIDE([margin], [income]),

SUM(table_name[value])
)


[margin] = CALCULATE(SUM(table_name[value]), table_name[category] = "margin")
[income] = CALCULATE(SUM(table_name[value]), table_name[category] = "income")


================= OR ===================

statement =
SWITCH(
SELECTEDVALUE(table_name[category]),

"percentage margin", IF(ISBLANK([margin]) || ISBLANK([income]), BLANK(), DIVIDE([margin], [income])),

SUM(table_name[value])
)


[margin] = CALCULATE(SUM(table_name[value]), table_name[category] = "margin")
[income] = CALCULATE(SUM(table_name[value]), table_name[category] = "income")

 

See relevant solution on stackoverflow

https://stackoverflow.com/questions/64438710/can-we-calculate-a-measure-with-percentage-in-power-bi-...

 

Please let me know if this help.

 

Thanks

Babatunde Dallas

Thanks
Dallas

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.