# Formula

{% hint style="warning" %}
#### Workshop - Formula

The Formula step in Pentaho Data Integration (PDI) is a powerful transformation component that allows you to create new fields or modify existing ones using mathematical expressions, string manipulations, and logical operations.&#x20;

It provides a JavaScript-like syntax for writing formulas that can perform calculations, concatenate text, apply conditional logic, and execute various built-in functions on your data rows. The Formula step is particularly useful for data cleansing, creating calculated fields, performing date/time operations, and implementing business rules within your data transformation workflows.&#x20;
{% endhint %}

<figure><img src="https://content.gitbook.com/content/7IROMjOTNySvPnpnJs27/blobs/CMSZTr65bdppo11Ny9WS/Formula.png" alt=""><figcaption><p>Formula</p></figcaption></figure>

***

{% tabs %}
{% tab title="First Tab" %}


{% hint style="success" %}

{% endhint %}
{% endtab %}

{% tab title="Second Tab" %}

{% endtab %}
{% endtabs %}

{% hint style="info" %}

{% endhint %}

{% tabs %}
{% tab title="1. Generate rows" %}
{% hint style="info" %}
#### Generate rows

Generate rows outputs a specified number of rows. By default, the rows are empty; however, they can contain several static fields. This step is used primarily for testing purposes. It may be useful for generating a fixed number of rows, for example, you want exactly 12 rows for 12 months.&#x20;

Sometimes you may use Generate Rows to generate one row that is an initiating point for your transformation. For example, you might generate one row that contains two or three field values that you might use to parameterize your SQL and then generate the real rows.

Used to generate some testing data:&#x20;

a = 1&#x20;

b = 2&#x20;

booking\_type = R
{% endhint %}

<figure><img src="https://content.gitbook.com/content/7IROMjOTNySvPnpnJs27/blobs/wjRcQdIxrwg1QeLkAHuX/GR%20-%20formula1.png" alt=""><figcaption><p>Generate rows</p></figcaption></figure>
{% endtab %}

{% tab title="2. Formula" %}
{% hint style="info" %}
#### Formula

The Formula step can calculate Formula Expressions within a data stream. It can be used to create simple calculations like \[A]+\[B] or more complex business logic with a lot of nested if / then logic.
{% endhint %}

#### Workflow 1&#x20;

Simple formula: result = \[a] + \[b]

<figure><img src="https://content.gitbook.com/content/7IROMjOTNySvPnpnJs27/blobs/9NrtufFFLt2xW1cpJO55/Formula%201.png" alt=""><figcaption><p>Formula - [a]+[b]</p></figcaption></figure>

#### Workflow 2&#x20;

Logic: result = IF(\[booking\_type]=”R”;\[b]-\[a])

<figure><img src="https://content.gitbook.com/content/7IROMjOTNySvPnpnJs27/blobs/pYziONJDcDAfNZjE34nV/Formula%202.png" alt=""><figcaption><p>Formula -  IF([booking_type]=”R”;[b]-[a])</p></figcaption></figure>

{% hint style="info" %}
The data stream fields are referenced with the syntax: \[data field]
{% endhint %}

{% embed url="http://wiki.pentaho.com/display/Reporting/Formula+Expressions" %}

{% embed url="http://docs.oasis-open.org/office/v1.2/OpenDocument-v1.2-part2.html" %}
Link to OASIS Open Document Format
{% endembed %}
{% endtab %}
{% endtabs %}
