WITH sample AS ( SELECT col,3.1 value_percentile_5,87.5 value_percentile_95 FROM UNNEST(ARRAY[1,3,5,7,9,11,13,15,17,19,21,23,25,27,29,31,33,35,37,39,90,134]) AS col ) SELECT `justfunctions.eu.outliers_percentiles`(col, value_percentile_5, value_percentile_95) FROM sample
/*--Output--
['−1', '−1', 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1]
*/
CREATE OR REPLACE FUNCTION `your_project_id.your_dataset_id.outliers_percentiles`(`col` FLOAT64, `value_percentile_5` FLOAT64, `value_percentile_95` FLOAT64)
RETURNS INT64 AS (CASE
WHEN
col < value_percentile_5 THEN -1
WHEN
col > value_percentile_95 THEN 1
ELSE 0
END
)
OPTIONS ( description = '''Detects if a <value> is an outlier based on then 5th percentile <value_percentile_5> and 95th percentile <value_percentile_95>. Assigns -1 for lower bound outliers, 1 for upper bound outliers, and 0 for non-outliers.''')
SQL User Defined Function (SQL UDF)
See something wrong? Contact us or report an issue on Github.