Liquid Studio Documentation
Data Mapping / Data Mapper Functions / By Category / Aggregate / Average
In This Topic
    In This Topic

    Function Name
    Retrieve the average value within a context
    Context Context within which to determine average value [optional]
    Value Value to retrieve average of
    Result Average value within context
    None -


    The Data Mapper Aggregate Average function allows you to retrieve average values within specific contexts. The Context input is optional, so you can simply retrieve the average value for the whole data source by not connecting it. If you do connect the Context input, the average is taken within the specified parent node. To apply the Aggregate Average function, use the following process:

    Create a new Data Mapper file, dragging your XML data source and targets into the editor area. For this example we are using sellers_source.xsd, an XML Schema Definition inferred from the following source:

    Source XML
    Copy Code
      <name>John Davidson</name>
      <name>Jane Johnson</name>
      <name>Mark Smith</name>

    For our target schema we will be using sellers_target.xsd, inferred from the following XML:

    Target XML
    Copy Code
     <sales_data type="average">28430</sales_data>
     <sales_assistant seller_name="Ken Jameson">
     <sales_assistant seller_name="Lisa Matthews">
     <sales_assistant seller_name="Paul Norton">

    The data models sales for individual salespeople within a retail organisation. Let's assume that, as well as mapping the salespeople and their data, we also want to determine the average sales figure across all staff members. We will record this in the "sales_data" element together with a String indicating the type of data we are recording, as in the target data above. Here is the Data Mapper with Source and Target imported:

    Data Mapper Source and Target

    Drag the Aggregate Average function from the Component Palette into the mapping area:

    Aggregate Average Component

    Average Added

    The Average function takes two inputs, representing the value to retrieve the average instance of and the context in which it should be calculated as the average. The output of the function is the sum of all input values divided by the number of values. Both inputs and output of the Average function may be connected to items in the XML Reader/ Writer or other components in the Mapper.

    Connect the Context input of the Average function to the "sellers" output in the XML Reader and the Value input to the "sales" output.

    Reader to Average

    This will output the average of sales across all sellers. In this case, the effect would be the same if we did not connect the Context input, however if we had categories of sellers, for example in different stores, we would perhaps want to calculate the average sales for each store, in which case the Context input would be different. Connect the Average output to the Element Value within the "sales_data" element in the XML Writer.

    Average to Writer

    This will ensure that the "sales_data" element is written out with the average sales value inside it each time the data is mapped.

    Now we need to define the type of data we are including in the "sales_data" element. Drag a Constant Value from the Data Type section onto the Mapper. Right-click it and choose Show Properties. Choose a String Data Type and enter "average" as the Value, or anything else you prefer.

    Constant Properties

    Connect the Constant output to the "type" input in the XML Writer.

    Constant to Writer

    Finally let's make the remaining input and output connections. Remember to map "sellers" to "sales_staff" and "sales" to "amount" - although we have used these as references we have not yet mapped them. We do not need to map anything to the "sales_data" element input as the Element Value will ensure the element itself is mapped.

    Inputs and Outputs Connected

    We can now execute the transform by pressing Shift-F5 or the Execute button (Execute). The transform is applied and the file we selected as output opens in the editor:

    Output XML
    Copy Code
     <sales_data type="average">18016.6666666667</sales_data>
     <sales_assistant seller_name="John Davidson">
     <sales_assistant seller_name="Jane Johnson">
     <sales_assistant seller_name="Mark Smith">

    The output contains the data values mapped from the source and the average sales figure across sellers, included in the "sales_data" element, together with a String indicating the meaning of the value.

    If you do not connect the Context input of the Average function, the result will be the average for the data source as a whole.
    The Average function divides the total of all values in the context then divides this by the number of values included in the sum. This means that the output may be a different numerical type than the input values.
    See Also