> For the complete documentation index, see [llms.txt](https://doc.wearepatchworks.com/product-documentation/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://doc.wearepatchworks.com/product-documentation/process-flows/building-process-flows/process-flow-shapes/standard-shapes/map-shape/working-with-field-transformations/available-transform-functions/number-transform-functions/math-transform-function.md).

# Math transform function

## Introduction

The **math** transform function is used to perform a mathematical operation for selected fields. For example, your incoming payload might include customer records, each with a series of numeric `value` fields that need to be added together so the total can be pushed to a `total` field in the target system.

## Available mathematical operations

The following mathematical operations are available:

* Add
* Subtract
* Multiply
* Divide

## Adding a math transform

In the instructions below, we'll step through the scenario mentioned above where our incoming payload includes customer records, each with three value fields (`value1`, `value2`, `value3`) that must be added together and pushed to a `total` field in the target system.

The steps required are detailed in two stages:

* [Stage 1: Add all source fields to be totalled for the mapping row](#stage-1-add-all-source-fields-to-be-totalled-for-the-mapping-row)
* [Stage 2: Add a concatenate transform function](#stage-2-add-a-concatenate-transform-function)

### Stage 1: Add all source fields to be totalled for the mapping row

To begin, we need to update/add the required mapping row so that it includes all source fields that need to be added together and then pushed to the target.

**Step 1**\
In your process flow, access settings for your map shape:

<div align="left"><figure><img src="https://2440044887-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FLYNcUBVQwSkOMG6KjZfz%2Fuploads%2FuZ8bFdQSrrJQyfJVBNOA%2Fmath%20transform%201.png?alt=media&amp;token=d51e4e8d-2462-41d8-9d07-cf240f578148" alt="" width="277"><figcaption></figcaption></figure></div>

**Step 2**\
Find (or add) the mapping row which requires a **math** transformation. In the example below, we have a row that's currently set to map the source `first name` field into the destination `full name` field:

<div align="left"><figure><img src="https://2440044887-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FLYNcUBVQwSkOMG6KjZfz%2Fuploads%2F8EsEPpynnARxdrnCy65v%2Fmath%20transform%202.png?alt=media&amp;token=bde04bd8-17df-41c4-a88c-0ed42288bfc4" alt="" width="563"><figcaption></figcaption></figure></div>

**Step 3**\
On the source side of the mapping row, we need to include all the fields to be used in our mathematical operation. To do this, click the 'pencil' icon associated with the existing source field:

<div align="left"><figure><img src="https://2440044887-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FLYNcUBVQwSkOMG6KjZfz%2Fuploads%2FUaJ76GWpindFBm4rbChu%2Fmath%20transform%203.png?alt=media&amp;token=d59d67fe-321f-4861-83d8-aabee290090a" alt="" width="563"><figcaption></figcaption></figure></div>

**Step 4**\
Details for the selected field are shown - click the **add source field** button:

<div align="left"><figure><img src="https://2440044887-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FLYNcUBVQwSkOMG6KjZfz%2Fuploads%2FCdEe6Yv9MP2gj84Ir7hX%2Fmath%20transform%204.png?alt=media&amp;token=91587140-47f2-4961-88ce-2bbb2b7255f0" alt="" width="563"><figcaption></figcaption></figure></div>

**Step 5**\
Click the 'pencil' icon associated with the new source field:

<div align="left"><figure><img src="https://2440044887-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FLYNcUBVQwSkOMG6KjZfz%2Fuploads%2FwkZWD3AxqG4reQYLFu7Q%2Fmath%20transform%205.png?alt=media&amp;token=b25c4ed6-4314-4c8f-8f6c-8169b445586c" alt="" width="563"><figcaption></figcaption></figure></div>

**Step 6**\
Move down and update the **display name** and **payload** fields for the second source field that you want to use - for example:

<div align="left"><figure><img src="https://2440044887-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FLYNcUBVQwSkOMG6KjZfz%2Fuploads%2F1zy4dX02BaEKkw99GDCt%2Fmath%20transform%206.png?alt=media&amp;token=f7f29626-7b77-4b37-8302-66a4fa0bfd03" alt="" width="563"><figcaption></figcaption></figure></div>

{% hint style="info" %}
In our example, our source data is coming in via a manual payload so are defining the payload field manually - if you're using a [connection shape](/product-documentation/process-flows/building-process-flows/process-flow-shapes/standard-shapes/connector-shape.md) to receive data, you'll be able to select the required field from the associated schema for your connection.
{% endhint %}

**Step 7**\
Accept these changes to exit back to your mapping rows - notice that there are now two source fields associated with the row you updated:

<div align="left"><figure><img src="https://2440044887-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FLYNcUBVQwSkOMG6KjZfz%2Fuploads%2F1DiRvK8mmIDNN2j7xt6K%2Fmath%20transform%207.png?alt=media&amp;token=435bdb88-e0ad-4ea0-bf25-25666eb50627" alt="" width="563"><figcaption></figcaption></figure></div>

**Step 8**\
Repeat steps 3 to 7 to add any more source fields that you need to include in the mathematical operation.

**Step 9**\
Go to [stage 2](#stage-2-add-a-concatenate-transform-function).

### Stage 2: Add a math transform function

With all required source fields defined for our mapping row, we can add a **math** transform function to define the required calculation based on these fields.

**Step 1**\
Select the **add transform** button for the required mapping rule - for example:

<div align="left"><figure><img src="https://2440044887-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FLYNcUBVQwSkOMG6KjZfz%2Fuploads%2F4QtIdjRvkG1NojSQ3kkL%2Fmath%20transform%20section%202%20a.png?alt=media&amp;token=7cfca483-4b53-48db-8b8b-67b6b19559b0" alt="" width="563"><figcaption></figcaption></figure></div>

**Step 2**\
Click the **add transform** button:

<div align="left"><figure><img src="https://2440044887-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FLYNcUBVQwSkOMG6KjZfz%2Fuploads%2FhVw4zy7Q9NWjWvAxUlyU%2Fconcat%20section%202%20b.png?alt=media&amp;token=b39e5cbf-bd0a-458d-835a-6436a2ad6217" alt="" width="563"><figcaption></figcaption></figure></div>

**Step 3**\
Click in the **name** field and select **math** from the **number** section in the list of transform functions:

<div align="left"><figure><img src="https://2440044887-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FLYNcUBVQwSkOMG6KjZfz%2Fuploads%2FEIjyb6O2irDcM0fdyDDE%2Fmath%20transform%20section%202%20b.png?alt=media&amp;token=80baa722-4360-4b56-95bc-d53186dccd28" alt="" width="563"><figcaption></figcaption></figure></div>

...**math** options are displayed:

<div align="left"><figure><img src="https://2440044887-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FLYNcUBVQwSkOMG6KjZfz%2Fuploads%2FfCmvEmhCvakbKp1AOrS6%2Fmath%20transform%20section%202%20c.png?alt=media&amp;token=c0744e03-f57c-41b3-ac68-9927eb8e20f4" alt="" width="563"><figcaption></figcaption></figure></div>

**Step 4**\
Click in the **operator** field and select the type of calculation to be performed - you can choose from **add**, **subtract**, **multiply** and **divide**:

<div align="left"><figure><img src="https://2440044887-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FLYNcUBVQwSkOMG6KjZfz%2Fuploads%2FGTD1YzZp8zVkRtulrRzN%2Fmath%20transform%20section%202%20d.png?alt=media&amp;token=7a59857f-a84c-49c2-8da9-38c36177c742" alt="" width="563"><figcaption></figcaption></figure></div>

**Step 5**\
Click the **add field** button:

<div align="left"><figure><img src="https://2440044887-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FLYNcUBVQwSkOMG6KjZfz%2Fuploads%2FTocLYdA5H2rR7sLXjAK1%2Fmath%20transform%20section%202%20e.png?alt=media&amp;token=23e3ab61-70ae-4484-ad1f-2bbaaf1494df" alt="" width="563"><figcaption></figcaption></figure></div>

**Step 6**\
Click in **source fields** and select the first source field to be used in the calculation:

<div align="left"><figure><img src="https://2440044887-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FLYNcUBVQwSkOMG6KjZfz%2Fuploads%2FK4CpQc4xXaCRclBnYBX4%2Fmath%20transform%20section%202%20m.png?alt=media&amp;token=c425e5cb-4029-40db-b299-15feeecdc89e" alt="" width="563"><figcaption></figcaption></figure></div>

{% hint style="info" %}
All source fields that were added for this mapping in [stage 1](#stage-1-add-all-required-source-fields-to-the-mapping-row) will be available for selection here.
{% endhint %}

**Step 7**\
Accept your changes.

**Step 8**\
Click the **add field** button again:

<div align="left"><figure><img src="https://2440044887-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FLYNcUBVQwSkOMG6KjZfz%2Fuploads%2Fps5HYWfsVS43GGQ6AJb4%2Fmath%20transform%20section%202%20g.png?alt=media&amp;token=33bd1cb3-8e87-477b-8576-37bd0a7b0f5b" alt="" width="563"><figcaption></figcaption></figure></div>

...and add the next source field to be used - for example:

<div align="left"><figure><img src="https://2440044887-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FLYNcUBVQwSkOMG6KjZfz%2Fuploads%2FCdCfrwWie8UiwFwEy7lb%2Fmath%20transform%20section%202%20n.png?alt=media&amp;token=8bc07a3f-d633-4b22-af0a-1a2b897262c7" alt="" width="563"><figcaption></figcaption></figure></div>

**Step 9**\
Accept your changes.

**Step 10**\
Repeat steps 8 and 9 to include any more source fields to be used in the calculation. Each time you accept a new source field you'll see the sequence that they will be processed when this transform function runs - for example:

<div align="left"><figure><img src="https://2440044887-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FLYNcUBVQwSkOMG6KjZfz%2Fuploads%2FVEWsZ6zHY9dewoR7X9lH%2Fmath%20transform%20section%202%20no.png?alt=media&amp;token=056a7c82-7c06-472b-8b28-c165e682d37d" alt="" width="563"><figcaption></figcaption></figure></div>

{% hint style="info" %}
Fields are processed in the sequence that they are added here.
{% endhint %}

**Step 11**\
Having added all required source fields to be calculated, accept changes:

<div align="left"><figure><img src="https://2440044887-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FLYNcUBVQwSkOMG6KjZfz%2Fuploads%2FBeXh0rgouhkzYMlRCkey%2Fmath%20transform%20section%202%20l.png?alt=media&amp;token=3badc3f4-a846-46b4-be46-8bddfde03acc" alt="" width="563"><figcaption></figcaption></figure></div>

...then save the function:

<div align="left"><figure><img src="https://2440044887-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FLYNcUBVQwSkOMG6KjZfz%2Fuploads%2Fbog0lO68F58f8GgKzq6C%2Fmath%20transform%20section%202%20k.png?alt=media&amp;token=94a1f09b-7444-4819-b606-c13c0d771678" alt="" width="563"><figcaption></figcaption></figure></div>

**Step 12**\
Ensure that the target field for this mapping row is set as required, then save the map shape. Next time the process flow runs, the mathematical operation will be performed for the given source fields and the total value is pushed to the defined target field.\
\
The example below shows an incoming payload before and after the **math** transformation is applied:

<div align="left"><figure><img src="https://2440044887-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FLYNcUBVQwSkOMG6KjZfz%2Fuploads%2FddK7ZIuo4GbUjktImJme%2Fmath%20transform%20section%20example.png?alt=media&amp;token=57a2f370-a455-47f8-b232-5f000a3c14ee" alt=""><figcaption></figcaption></figure></div>
