Replace negative values with 0 (thresholding) in a metric



I am trying to replace the following simple custom expression engine function with a built-in:

function customThreshold(series) {

    var data =;

    for (var i = 0; i < series.count(); i++) {
        if (data[i] < 0) {
            data[i] = 0;

    return series;

The metric using it is simply customThreshold(MySimpleMetric). It works fine but I see the engine is spending some time in this function for large evaluation intervals so I’m looking for a more native way to do it that could (maybe) be faster.

Thanks for your ideas :slight_smile:



Try this:
expression: "max(0, MySimpleMetric)"



I thought it would work but there’s a side effect: max(0, MySimpleMetric) resets the missing() to 0 (per the logic you described here) which breaks some additional logic.

Unless this can be fixed I need to find another way.

(MySimpleMetric >= 0) * MySimpleMetric would work but I’m not sure if it’s a big gain from my customThreshold function.



available(MySimpleMetric) ? max(0, MySimpleMetric) : null

But we should probably carry over missing information. @rohit.sureka what do you think?



I tried max(makeMissing(100, 0), MySimpleMetric) but it seems that max() is ignoring fully missing values:

max(makeMissing(100, 0), identity(-1)) returns identity(-1)



@romain.juban Since we are picking the max value, we are transforming the signal and the new signal can deterministically say what the max value is and hence there is no missing information. I believe what you want to write is:

MySimpleMetric >= 0 ? MySimpleMetric : null

@lerela - as much as possible you should use the existing expression / metric engine, since the expressions are compiled and will be extremely fast in comparison to any custom java script code.

So your expression (MySimpleMetric >= 0) * MySimpleMetric will be a significant gain from the custom java script function too.



What if you have the value 3 with missing at 100? When you take the max of that and 0 is it going to take 0 or 3?