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

Earn a 50% discount on the DP-600 certification exam by completing the Fabric 30 Days to Learn It challenge.

Reply
djallarii
Helper I
Helper I

Is it possible to store DAX operators as a string and recall them in an expression?

We track metric actual values against metric target values and color code the actual values based on a pre-determined polarity. Some actual values are better if they are higher than the target and some actual values are better if they are lower than the target.

 

I'd like to store the DAX operators for each metric in a table and retrieve that operator in an IF statement when comparing values.

 

For example use something like this:

color_code = if ( metrics[ actual ] & polarity[ operator ] & metrics[ target ] , "red" , "green" )

 

To return this:

color_code = if ( 80 >= 75 , "red" , "green" )

 

With this result:

color_code = 80

 

 

1 ACCEPTED SOLUTION
AnthonyGenovese
Resolver III
Resolver III

Hey @djallarii 

So it won't work like that, but you can use your setup to get the output you are looking for. You would do something like this
var greaterthan = if ( metrics[ actual ]>= metrics[ target ] , "red" , "green" )
var lessthan = if ( metrics[ actual ]<= metrics[ target ] , "red" , "green" )
Var color_code = if (polarity[ operator ] =">=", greaterthan, lessthan)
return color_code
If this post was helpful, please kudos or accept the answer as a solution.
~ Anthony Genovese
Need more PBI help? PM me for affordable, dedicated training or consultant recomendations!

View solution in original post

5 REPLIES 5
AnthonyGenovese
Resolver III
Resolver III

Hey @djallarii 

So it won't work like that, but you can use your setup to get the output you are looking for. You would do something like this
var greaterthan = if ( metrics[ actual ]>= metrics[ target ] , "red" , "green" )
var lessthan = if ( metrics[ actual ]<= metrics[ target ] , "red" , "green" )
Var color_code = if (polarity[ operator ] =">=", greaterthan, lessthan)
return color_code
If this post was helpful, please kudos or accept the answer as a solution.
~ Anthony Genovese
Need more PBI help? PM me for affordable, dedicated training or consultant recomendations!

Awesome, I understand what you're doing here but I'm curious if we can make it a bit more dynamic than hardcoding the colors.

 

Here's an example file:

Color Format.pbix

 

Can you apply your logic to achieve similar results using the polarity_ID to return the color formats from the color_format_true and color_format_false fields?

 

I'm thinking a SWITCH function using the polarity_ID could work where each expression and related operator is returned but I get duplicate results per metric.

 

Something like:
If polarity ID = 1 then evaluate SelectedActual > SelectedTarget, ColorTrue, ColorFalse
If polarity ID = 2 then evaluate SelectedActual < SelectedTarget, ColorTrue, ColorFalse
If polarity ID = 3 then evaluate SelectedActual <= SelectedTarget, ColorTrue, ColorFalse
etc.

 

I can't download the file, can you provide a table example in the reply? And how you want to use the measure? (What visual, and where)


@AnthonyGenovese I wanted to let you to know I was able to get the intended results using your original post and accepted it as the solution.

 

I am still curious if you can figure out a solution to my follow up example, however. If you have a chance to take a look, I would appreciate it!

I uploaded the file to Google Drive and my personal OneDrive to try again:

 

Google Drive Link - 

https://drive.google.com/file/d/1CbPcYgLduKUY47oUueflzIwEMTvlhS4r/view?usp=drive_link

 

OneDrive Link - 

https://1drv.ms/u/s!AoXwZT-xo7jRgfUcCYqWcjoD6Divcw?e=ffxJBd

 

Otherwise here's the tables:

metric_table

metric_IDmetric_nametargetactualpolarity_ID
1Metric 11001001
2Metric 2791002
3Metric 3801003
4Metric 4991004
5Metric 5151005
6Metric 6801006

 

color_table

metric_IDpolaritypolarity_IDcolor_format_truecolor_format_false
1>1RedGreen
2<2GreenRed
3<=3YellowOrange
4>=4#00427a#41bee8
5!=5#ffd100#80c342
6=6#a05da5Grey

 

The return statement in this measure should be dynamic if possible which is where the idea for a SWITCH statement came in.

color_formula.PNG

Helpful resources

Announcements
PBI_APRIL_CAROUSEL1

Power BI Monthly Update - April 2024

Check out the April 2024 Power BI update to learn about new features.

Top Solution Authors