percentile

Return the p^th^ percentile ranked value of the specified field (or a list of percentile values if given a list of percentile ranks). When percentile is applied to large numbers of distinct values (such as from a continuous metric) the returned value is approximate rather than exact.

put|reduce percentile(field, p)
Parameter Description Required?
field The field to use as a value Yes
p The percentile rank to select, in range 0...1 (or a list of percentile ranks). A value (or list of values) will be returned whose rank is equal to or greater than p * number of pointswhen the points are sorted by the value of the field. Yes

Example: 10th and 90th percentiles

This example uses the batch processor.

// On historical data, you can apply reducer to get a single computation,
// or batch your historical data by time, then reduce per batch period.

sub historical_points() { 
  emit -from :0: -limit 10 
  | put value = Math.floor(Math.random() * 100)
}
historical_points 
| reduce pct10 = percentile(value, 0.1), pct90 = percentile(value, 0.9)
| view table -title "10th and 90th percentile of historical points"
;
historical_points 
| batch 5 
| reduce pct10 = percentile(value, 0.1), pct90 = percentile(value, 0.9) 
| view table 
    -update "append" 
    -title "10th and 90th percentile per 5-second batch, historical"
;

// On live streaming data, you must batch by time, then reduce per batch period.

sub live_points() { 
  emit -limit 10 
  | put value = Math.floor(Math.random() * 100)
}
live_points 
| batch 3 
| reduce pct10 = percentile(value, 0.1), pct90 = percentile(value, 0.9)
| view table 
    -update "append" 
    -title "10th and 90th percentile per 3-second batch, live"

Example: quartiles

This example uses a list of percentile ranks to retrieve the quartile values of the data (minimum, 25%, median, 75%, and maximum). Because of the large number of x values, approximate quartile values are returned, though min and max will always be exact.

const QUARTILES = [0, 0.25, 0.5, 0.75, 1];
emit -limit 10001 -from Date.new(0)
| put x = count() - 1 // our data value ranges from 0 to 10000
| reduce Q = percentile(x, QUARTILES)
| put min = Q[0], Q1 = Q[1], Q2 = Q[2], Q3 = Q[3], max = Q[4]
| remove Q
| view table