Example: Finding the Sum of Multiple Data Points

Top Previous Topic Next Topic  Print this topic

In this example, we aim to calculate the sum of all attributes contained within a repeating element from the data source. This is often the case for examples, such as invoices, where we might have multiple line items containing a charge in each. The sum of all line items will result in the total payment due. We will use a common numerical function called sum() to sum all of the "price" attributes for each product purchases in our invoice.

 

1. In the Create Expression Dialog, we will first need to select the sum function by choosing the Add a Function option.

 

sum1

Figure 1: The sum function is selected from the function Options Menu.

 

2. Once the sum function is added into the Expression text box, we need to replace the argument indicated as a "node-set" for a repeated item. In this case, we want to sum all of the occurrences of the "price" attribute, so we will navigate to and select this item in the Data Tree. Next, select Insert the selected path from Data Tree to add the path into the expression box. We now have an expression that will sum all of the numerical values present in the "price" attribute for each product element in the data source.

 

sum2

Figure 2: The path for the "price" attribute is added to the expression.

 

3. After the expression is set, select Save in order to insert the Field into the Design Surface. It is recommended that you test your results by previewing the output. In our example, you can see in Figure 4 that the output is the sum of all individual product prices, indicating that our expression works as intended.

 

sumfield

Figure 3: The field is inserted into the Design Surface.

 

sumresult

Figure 3: The expression yields the sum of all invoice product prices.