I am using an expression transform, to consolidate the simple math for a status tag.
The expression below is always 0, even though the value is 50 and the ideal min is 60. I was assuming that since that's false, it would jump to the next if. before I change this to a script transform, any suggestions on why the expression isn't working?
Bonus points if you have me get off of the awful if statement below. thanks!
if( {view.custom.ideal_min} <= {value} <= {view.custom.ideal_max}, 0,
if( {view.custom.acceptable_min} <= {value} <= {view.custom.ideal_min}, -1,
if( {value} <= {view.custom.acceptable_min}, -2,
if( {view.custom.ideal_max} <= {value} <= {view.custom.acceptable_max}, 1,
if( {value} >= {view.custom.acceptable_max}, 2, 999)
)
)
)
)
I don't recall expression syntax supporting chained comparisons.
Your code but working:
if(
and({view.custom.ideal_min} <= {value}, {value} <= {view.custom.ideal_max}), 0,
if(
and({view.custom.acceptable_min} <= {value}, {value} <= {view.custom.ideal_min}), -1,
if(
{value} <= {view.custom.acceptable_min}, -2,
if(
and({view.custom.ideal_max} <= {value}, {value} <= {view.custom.acceptable_max}), 1,
if(
{value} >= {view.custom.acceptable_max}, 2,
999
)
)
)
)
)
or in a simpler way:
if({value} <= {view.custom.acceptable_min}, -2,
if({value} <= {view.custom.ideal_min}, -1,
if({value} <= {view.custom.ideal_max}, 0,
if({value} <= {view.custom.acceptable_max}, 1,
if({value} >= {view.custom.acceptable_max}, 2, 999)
)
)
)
)
Note: Hadn't tested, make sure is correct.
A CASE expression is often times easier on the eyes:
case(
true,
{value} <= {view.custom.acceptable_min}, -2,
{value} <= {view.custom.ideal_min}, -1,
{value} <= {view.custom.ideal_max}, 0,
{value} <= {view.custom.acceptable_max}, 1,
2) // # 999 doesn't seem reachable, so I've omitted it
i like the case/switch better but I wasn't sure if you could put expressions in the case. thanks, I'll test it out.
danielps1818:
if({value} <= {view.custom.acceptable_min}, -2,
if({value} <= {view.custom.ideal_min}, -1,
if({value} <= {view.custom.ideal_max}, 0,
if({value} <= {view.custom.acceptable_max}, 1,
if({value} >= {view.custom.acceptable_max}, 2, 999)
)
)
)
)
If you do ever use nested ifs though, nesting them with indents is woeful to read. Just stick them all at the start
if({value} <= {view.custom.acceptable_min}, -2,
if({value} <= {view.custom.ideal_min}, -1,
if({value} <= {view.custom.ideal_max}, 0,
if({value} <= {view.custom.acceptable_max}, 1,
if({value} >= {view.custom.acceptable_max}, 2, 999
)))))
That's much cleaner to read! thanks