Skip to content
Documentation
Updated 3 minutes to read
Markdown

The avg function, one of the Math functions in the Formula plugin for Search Field Expressions, returns the average of the numeric values in the content.

Lowest, highest, and average line total of an order with min, max, and avg, animated walkthrough (7.12)
Animated walkthrough: on the Order Entry XML of order ORD-10011 in the Order-to-Cash demo, with the line totals 399.80, 498.00, and 125.00, min returns 125.00, max returns 498.00, and avg returns 340.9333333333333333333333333, which is 1022.80 / 3, not rounded; use round(avg(...), 2) for 340.93.

avg(content [, unique])
Parameter Required Description
content ✅ Yes The numeric values to average: body, context, or another Formula function such as xPath or jsonPath.
unique ❌ No Unquoted true or false; default false for this function. false: a value that appears several times counts every time. true: identical values count once. Values are compared as text, so 100 and 100.00 are different values. A quoted 'true' is an error.
  • One number: the average (mean) of the numeric values in content.
  • The average isn't rounded: it keeps up to 28 decimals, such as 340.9333333333333333333333333 for 1022.80 / 3. New 7.12 Wrap it in round to shorten it: round(avg(...), 2) returns 340.93.
  • When content holds no numeric value, avg returns nothing.

Quoting, nesting, and how content is resolved are described in the Formula language rules.

Example 1: Average the order amounts in an XML message

Section titled “Example 1: Average the order amounts in an XML message”

Message Body:

<ns0:Orders xmlns:ns0="SupplyChain.Schemas/1.0">
<Order>
<Id>1337</Id>
<Amount>4</Amount>
<City>New York</City>
<Partner>QWNtZQ==</Partner>
</Order>
<Order>
<Id>1338</Id>
<Amount>2</Amount>
<City>Paris</City>
<Partner>QWNtZQ==</Partner>
</Order>
</ns0:Orders>
avg(xPath('Orders/Order/Amount', body))
3

xPath extracts the amounts 4 and 2; their average is 3.

Test Expression tab: avg(xPath('Orders/Order/Amount', body)) on two orders with amounts 4 and 2 returns 3 (7.12)
The namespace prefix ns0: doesn't need to be part of the XPath; Orders/Order/Amount matches it.

Example 2: Average numeric values extracted from JSON

Section titled “Example 2: Average numeric values extracted from JSON”

Message Body:

{"a":[{"b":1},{"b":2},{"b":6}]}
avg(jsonPath('a[*].b', body))
3

jsonPath extracts 1, 2, and 6; their average is 3.

Test Expression tab: avg of jsonPath over every b in the array a, on the JSON values 1, 2, and 6, returns 3 (7.12)
a[*].b selects b in every element of the array a; the result is one number, not three.

  • By default every occurrence counts: lines of 100, 100, and 400 average 200. With avg(..., true) identical values count once, so the average is (100 + 400) / 2 = 250.

...

Version:
Release date: N/A
Loading...

Explore Nodinite for 30 days – completely free

  • Find out why this is the leading O&M software
  • Prove your business case
  • Test the self-service for the business side

Loading...

See how Nodinite resolves your technical issues, in a LIVE Demo!

Once you submit this request:

  • We will give you a short call to understand the issues you want to tackle.
  • Understand solutions you have tried and why were you not happy with them.
  • Book a convenient slot for a LIVE demo!

Loading...