Within BizTalk there is a functoid called Record Count that will return the number of instances of a repeated record or repeated element that occur in a message instance. The input to this functoid is the record or element to be counted.
As an example take the following Source schema, where the Source message has a repeated record called Box and each Box has a repeated element called Item:
An instance of this Source schema may look as follows; 2 box records - one with 2 items and one with only 1 item.
Our destination schema has a number of elements and a repeated box record. The top level elements contain totals for the number of boxes and the overall number of items. Each box record contains a single element representing the number of items in that box.
Using the Record Count functoid it is easy to map the top level elements, producing the expected totals of 2 boxes and 3 items:
We now need to map the total number of items per box, but how will we do this? We have already seen that the record count functoid returns the total number of instances for the entire message, and unfortunately it does not allow you to specify a scoping parameter. In order to acheive Scoped Record Counting we will need to make use of a combination of functoids.
As you can see above, by linking to a Logical Existence functoid from the record/element to be counted we can then feed the output into a Value Mapping functoid. Set the other Value Mapping parameter to "1" and link the output to a Cumulative Sum functoid. Set the other Cumulative Sum functoid parameter to "1" to limit the scope of the Cumulative Sum.
This gives us the expected results of Items per Box of 2 and 1 respectively.
I ran into this issue with a larger schema on a more complex map, but the eventual solution is still the same. Hopefully this simplified example will act as a good reminder to me and save someone out there a few minutes of brain scratching.