- 3 minutes to read

Formula - Average (avg)

New 5.5

The avg() Formula function is a pretty straight-forward and self-explanatory function. It is used to return the average from the unique values extracted from the Content part.

Quick example

Let's get you started with the following example: For other and more advanced examples, scroll down on this page to the Examples section.

Input Expression Result

  <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
Text Data average expression Unique values

Features

The avg( ) function can be used on any content, as long as data is returned from the Content part. It doesn't matter if the content parameter a plain text or any valid Formula Plugin function. Please notice that the number returned represents the average of the extracted values; more on this later below.

  • Return the average of extracted values from the Content.
  • It returns the average of the unique values from Content.
  • It can be applied on the Content part that returns data; Like jsonpath, xpath functions or even a plain-text, review the Formula user guide for the complete list of functions.

Important

This plugin loads the entire message into RAM, so make sure to apply this function only on small messages.

How to use

The Content part should contain data to use the avg function, (otherwise, it will fail, and you will end up with nothing as the result). But first and foremost, you must configure the Formula plugin:

  • Select Formula as the expression type plugin.
  • Write down the expression to use in the 'Expression' text area.
  • Input Content as a parameter to the function.

Syntax

Use the avg function with any of the following parameters:

  • From Message Body: avg(body)

  • From Message Context: avg(context('MessageContextKey'))

  • Result from previous Formula operation avg(SomeOtherNestedFormulaFunction(Content))

Examples

Below, you will find different common examples of how to use the average function.

Body as Content

"Nodinite" is the message body text; By using the provided expression, the result is 1.

e.g. avg(body)

simpleBody

Message Body


 Nodinite

Expression


avg(body)


Context as Content

"Nodinite" is the value for the corresponding key "id", by using the provided expression, the result is 1.

e.g. count(context('id'))

messageContext

Message Context Value


  Nodinite


Expression


 avg(context('amount'))


Formula Function as Content

The message body is a JSON structure, and by using the provided expression, the result is 3. e.g. avg(jsonpath('a[*].b', body))

jsonpath

Message Body


{a:[{b:'This'},{b:'is'},{b:'Formula Plugin'}]}

Expression


count(jsonpath('a[*].b', body))


Next Step

How to Add or manage Search Fields
How to Add or manage Log Views

Expression Type Plugins are used in Search Fields
What are Search Fields?
What are Search Field Expressions?
What are Message Types?
What are Log Views?