InfoPath 2007 - Conditional SUM
This blog post will show you how to create an expression box in an InfoPath 2007 form whose value is based on the result of an conditional sum. Consider the following InfoPath form template:
The previous image shows a simple expense report. To support the introduction of the expense information, a Repeating Table control is used with three columns:
- Expense - Expense Description. A simple Text Box control;
- Value - Expense value. A simple Text Box control;
- Expense Type - Drop-down List Box control that allows 4 expense types: Food, Land Travel, Air Travel and Parking.
Below the repeating table there are 5 expression boxes that show the total amount of the expense report and the total amount for each expense type. The total amount expression box is based on a simple sum expression and each of the expense type expression boxes are based on conditional sums filtered by the value of the expense type Drop-down List Box control. The expressions used for each expense type are the following:
| Expression Box | Expression | XPath Expression |
| Expense Total | sum(expensevalue) | sum(my:accounting/my:expensevalue) |
| Food Expense Total | sum(accounting[expensetype = "Food"]/expensevalue) | sum(my:accounting[my:expensetype = "Food"]/my:expensevalue) |
| Land Travel Expense Total | sum(accounting[expensetype = "Land Travel"]/expensevalue) | sum(my:accounting[my:expensetype = "Land Travel"]/my:expensevalue) |
| Air Travel Expense Total | sum(accounting[expensetype = "Air Travel"]/expensevalue) | sum(my:accounting[my:expensetype = "Air Travel"]/my:expensevalue) |
| Parking Expense Total | sum(accounting[expensetype = "Parking"]/expensevalue) | sum(my:accounting[my:expensetype = "Parking"]/my:expensevalue) |
As the previous table shows, the conditional sum expressions for each expense type are relatively simple and pretty straightforward for those who are familiarized with XPath, since XPath syntax is used for each expression. This comes as no surprise since the underlying data of the InfoPath form is stored in XML. The following image shows an expense report example filled with some sample values:
