# Tree Transform

The *Tree Transform* object in Astera Data Stack enables users to transform data in a hierarchical structure. Users can create new fields in the parent node based on the data in the collection/child node. *Tree Transform* supports rule-based routing, filtering, merging, and sorting of data while maintaining its hierarchy.

{% embed url="<https://youtu.be/_yrhfQBWPUY>" %}

## **Use Case**

In this case, we are using the [*XML/JSON File Source*](https://documentation.astera.com/v/astera-data-stack-v8/dataflows/sources/xml-json-file-source) to extract our source data.&#x20;

<figure><img src="https://750977703-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FqHxyVNGb7tSdIWecl6Ru%2Fuploads%2FNennI0xvefqEAPFofMkJ%2Fimage.png?alt=media&#x26;token=61eee1e5-9c1f-473d-9749-3de29344e3e9" alt=""><figcaption></figcaption></figure>

The source file contains customers’ information in the parent node and their orders and shipping details in the collection/child node.

You can preview this data by right-clicking on the source object’s *Header > Preview Output.*

<figure><img src="https://750977703-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FqHxyVNGb7tSdIWecl6Ru%2Fuploads%2FL5nVPM70jzt47kjCDJOe%2Fimage.png?alt=media&#x26;token=852547d0-c3af-4c9c-91ba-910f6041336b" alt=""><figcaption></figcaption></figure>

A *Data Preview* window will open, displaying a preview of the hierarchical source data.

<figure><img src="https://750977703-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FqHxyVNGb7tSdIWecl6Ru%2Fuploads%2FtktJoL0uncoDPASo9hHs%2Fimage.png?alt=media&#x26;token=553d1135-6d07-4171-bc15-bc068eb05603" alt=""><figcaption></figcaption></figure>

Now, we want to create a new field in the parent node that contains the count of orders that arrived late for each customer and route these records to a destination file, all while maintaining the hierarchical format of the dataset.

This new field in the parent node will depend on two fields: *RequiredDate* and *ShippedDate*, that are already present in the collection/child node.

In other words, we are trying to transform hierarchical data without flattening its structure.

## **Using Tree Transform Transformation**

1. To get a *Tree Transform* object from the *Toolbox*, go to *Toolbox > Transformations > Tree Transform* and drag and drop the *Tree Transform* object onto the designer.

<figure><img src="https://750977703-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FqHxyVNGb7tSdIWecl6Ru%2Fuploads%2FLwZtzcN9omwAo4G0xGUH%2FTree%20Transformation%20Gif%201.gif?alt=media&#x26;token=acf0ca8b-b44e-452a-bbc0-262d9a227901" alt=""><figcaption></figcaption></figure>

The transformation object is empty right now. This is because we have not mapped any fields to it yet.

2. Auto-map source fields onto the transformation by dragging and dropping the root node of the source object *CustomerOrders* onto the root node of the transformation object – *TreeXform*.

<figure><img src="https://750977703-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FqHxyVNGb7tSdIWecl6Ru%2Fuploads%2FOJuHUncGe2ShpUnoxuJK%2FTree%20Transformation%20Gif%202.gif?alt=media&#x26;token=65f87c2f-ddb6-427f-b8d8-cfe13fc4f2d1" alt=""><figcaption></figcaption></figure>

Now that the mapping is done, let’s configure the *TreeXfrom* object.

## **Configuring the Tree Transform Object**

1. Right-click on the object’s header and select *Properties* from the context menu. A configuration window for *Tree Transform* will open. The first window is the *Layout Builder* window. This is where we can create or delete fields, change their name or data types, mark any field as a variable, or attach expressions to fields.

<figure><img src="https://750977703-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FqHxyVNGb7tSdIWecl6Ru%2Fuploads%2F4yT9xZn6md1cKkYoduuG%2FTree%20Transformation%20Gif%203.gif?alt=media&#x26;token=e8758b23-3d22-4c5e-9712-40a22de4e2fe" alt=""><figcaption></figcaption></figure>

2. Create a new field under the parent node. Let’s name it *LateOrders*.

<figure><img src="https://750977703-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FqHxyVNGb7tSdIWecl6Ru%2Fuploads%2FOTjJK0nz02rCg4n4G5fK%2Fimage.png?alt=media&#x26;token=38f98281-1cbb-42e7-a96a-712a835c907a" alt=""><figcaption></figcaption></figure>

3. Click *Next*, and you will be directed to a *Layout Modifications* window.

<figure><img src="https://750977703-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FqHxyVNGb7tSdIWecl6Ru%2Fuploads%2FoCSPscrsle9ba26eUYBK%2Fimage.png?alt=media&#x26;token=932907ac-e95f-4f06-a2c7-1d5013dc38bf" alt=""><figcaption></figcaption></figure>

4. Select the *LateOrders* field. Here you can modify any field by applying expressions to it.

<figure><img src="https://750977703-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FqHxyVNGb7tSdIWecl6Ru%2Fuploads%2F3q9nMOx4llYASeSuFgtU%2Fimage.png?alt=media&#x26;token=2d1cedd5-224c-4f6f-a329-26e4fdfc1af5" alt=""><figcaption></figcaption></figure>

5. Write the expression that counts the number of late-arriving orders.

Click *“…”* next to the expression box to open the *Expression Builder* where you can make use of the extensive library of built-in functions and expressions in Astera.

<figure><img src="https://750977703-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FqHxyVNGb7tSdIWecl6Ru%2Fuploads%2F8eoRzLZOtJOkcDsN1Xa0%2Fimage.png?alt=media&#x26;token=d91699de-e77b-4616-b7cf-52c5f40cb425" alt=""><figcaption></figcaption></figure>

This creates the field values for *LateOrders* based on the field values of the *RequiredDate* and *ShippedDate* fields.

6. Click on the *Orders* node and a panel *(Options for Collection)* will appear. These options are only available for collection nodes.

<figure><img src="https://750977703-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FqHxyVNGb7tSdIWecl6Ru%2Fuploads%2FHut4NWPsCEyLqag3AEBJ%2Fimage.png?alt=media&#x26;token=ab5945f4-a6f3-465c-9d05-ce47d440ebda" alt=""><figcaption></figcaption></figure>

*Options for Collection*:

* *Show* – Shows collection node in the *Tree Transform* object.
* *Flatten With Item Count* – Flattens data based on the record count in the collection node against each item in the parent node.
* *Flatten Based on Rules* – Flattens a part of hierarchical data based on predefined rules.
* *Route Based on Rules* – Routes and creates subsets of data based on predefined rules.
* *Merge With Item Count* – Merges data based on the record count in the collection node against each item in the parent node.
* *Hide* – Hides collection node from the *Tree Transform* object.
* *Calculation Formula* – An expression box used for writing rules to route or flatten hierarchical data.
* *Sort Based On Keys* – Sorts hierarchical data based on the field in the collection node. Only available for *Show* and *Flatten With Item Count* options.

This is where we will specify the rule for routing late-arriving orders. Select *Route Based on Rules*. A new section for adding rules will appear in the window.

<figure><img src="https://750977703-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FqHxyVNGb7tSdIWecl6Ru%2Fuploads%2FYF2wmVflP8mzBpdeHkrC%2Fimage.png?alt=media&#x26;token=f499cdbd-0271-4fc6-b57e-3a21e38d81db" alt=""><figcaption></figcaption></figure>

7. Add a new rule by clicking on the *Add switch condition* icon ![](https://750977703-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FqHxyVNGb7tSdIWecl6Ru%2Fuploads%2Flhu5Y4d3jHd2Zts4IXsD%2Fimage.png?alt=media\&token=82826ec3-37a6-49bd-a1be-d8bdfe29e51e). Now, write an expression to route late arriving orders and name this rule “LateArrivals”.

<figure><img src="https://750977703-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FqHxyVNGb7tSdIWecl6Ru%2Fuploads%2FF3lagCh5Wi5kqIwXBYvu%2Fimage.png?alt=media&#x26;token=c1d5c98b-4758-4427-ae50-71f816d9ed8e" alt=""><figcaption></figcaption></figure>

8. Click *OK*. Now observe that a new collection node *Orders\_LateArrivals* has been added to the *Tree Transform* object.

<figure><img src="https://750977703-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FqHxyVNGb7tSdIWecl6Ru%2Fuploads%2F0sXa2YZHWNoqmtZLDvEp%2Fimage.png?alt=media&#x26;token=b85185c4-7ead-430e-a874-bcb8f7d0f4f2" alt=""><figcaption></figcaption></figure>

To preview data, right-click on the header of the transformation object and select *Preview Output* from the context menu.

<figure><img src="https://750977703-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FqHxyVNGb7tSdIWecl6Ru%2Fuploads%2FK9FcUP1Gt8AGKRgYdLjV%2Fimage.png?alt=media&#x26;token=30c137da-e14a-4999-9081-97310a935e5f" alt=""><figcaption></figcaption></figure>

A *Data Preview* window will open. On expanding the records, you will get corresponding order details and appended details of late-arriving orders.

<figure><img src="https://750977703-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FqHxyVNGb7tSdIWecl6Ru%2Fuploads%2FLBjmtQcvYvxcIMCHbyac%2Fimage.png?alt=media&#x26;token=afe60a72-95b0-436b-a875-514a81193665" alt=""><figcaption></figcaption></figure>

To store the output for late arriving orders, you can write it to a destination file or use that data further in the dataflow.

This concludes using the *Tree Transform* transformation object in Astera Data Stack.
