The avgif aggregation function in APL allows you to calculate the average value of a field, but only for records that satisfy a given condition. This function is particularly useful when you need to perform a filtered aggregation, such as finding the average response time for requests that returned a specific status code or filtering by geographic regions. The avgif function is highly valuable in scenarios like log analysis, performance monitoring, and anomaly detection, where focusing on subsets of data can provide more accurate insights.

For users of other query languages

If you come from other query languages, this section explains how to adjust your existing queries to achieve the same results in APL.

Usage

Syntax

summarize avgif(expr, predicate) by grouping_field

Parameters

  • expr: The field for which you want to calculate the average.
  • predicate: A boolean condition that filters which records are included in the calculation.
  • grouping_field: (Optional) A field by which you want to group the results.

Returns

The function returns the average of the values from the expr field for the records that satisfy the predicate. If no records match the condition, the result is null.

Use case examples

In this example, you calculate the average request duration for HTTP status 200 in different cities.

Query

['sample-http-logs'] 
| summarize avgif(req_duration_ms, status == "200") by ['geo.city']

Run in Playground

Output

geo.cityavg_req_duration_ms
New York325
London400
Tokyo275

This query calculates the average request duration (req_duration_ms) for HTTP requests that returned a status of 200 (status == "200"), grouped by the city where the request originated (geo.city).

  • minif: Returns the minimum value of an expression, filtered by a predicate. Use when you want to find the smallest value for a subset of data.
  • maxif: Returns the maximum value of an expression, filtered by a predicate. Use when you are looking for the largest value within specific conditions.
  • countif: Counts the number of records that match a condition. Use when you want to know how many records meet a specific criterion.
  • sumif: Sums the values of a field that match a given condition. Ideal for calculating the total of a subset of data.