# 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.

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.
{% endhint %}

<figure><img src="/files/ZDH5pocRFmIy6ehgGkIy" alt=""><figcaption><p>Formula</p></figcaption></figure>

***

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

{% endtab %}

{% tab title="Second Tab" %}

{% endtab %}
{% endtabs %}

{% 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.

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:

a = 1

b = 2

booking\_type = R
{% endhint %}

<figure><img src="/files/UnN9DeP2qH6Bo7o8UDM5" 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**

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

<figure><img src="/files/w0GJFa0ojtpGHE6HAiSI" alt=""><figcaption><p>Formula - [a]+[b]</p></figcaption></figure>

**Workflow 2**

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

<figure><img src="/files/X4atE8VADjL6YoIMLP5T" 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 %}


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://academy.pentaho.com/pentaho-data-integration/data-integration/enrich-data/scripting/formula.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
