# Tree Transform

The *Tree Transform* object in Astera 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.

In this document, we will learn to use the *Tree Transform* object in Astera.

### Use Case

In this case, we are using the [*XML/JSON File Source*](https://documentation.astera.com/astera-data-stack-v10/dataflows/sources/xml-json-file-source) to extract our source data. You can download this sample data from here.

![](https://3083465318-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FsR50Wa7EwZGlmPSAMkkf%2Fuploads%2F7gApvQ2c18IMgo0Jdo1j%2F1_sample_data.PNG?alt=media\&token=2bab9bc6-9b84-4f5c-9cc9-4db0fb841ab6)

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 source object’s *header > Preview Output.*

![](https://3083465318-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FsR50Wa7EwZGlmPSAMkkf%2Fuploads%2FNd8vuE3855zEvw7W39Sw%2F3_DataPreview.png?alt=media\&token=976b9c36-39d2-4d28-a220-16425d60cbb5)

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

![](https://3083465318-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FsR50Wa7EwZGlmPSAMkkf%2Fuploads%2FDy4Zjzc4hA1PPYNIAewA%2F2_data.PNG?alt=media\&token=c2ed20d5-a07b-4f37-a580-ab8d4e0b4610)

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 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 a hierarchical data without flattening its structure. For this purpose, we will use the *Tree Transform* Transformation object.

#### Using Tree Transform

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.

![](https://3083465318-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FsR50Wa7EwZGlmPSAMkkf%2Fuploads%2FmVxXW1RR7qbyTyxtonUy%2Fgif-drag.gif?alt=media\&token=46173316-5e42-472c-a697-b6e258316ef9)

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

![](https://3083465318-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FsR50Wa7EwZGlmPSAMkkf%2Fuploads%2FNDd4GtFespjKwIuBVVQy%2Fmapping.gif?alt=media\&token=0337bc09-ae06-4e40-808f-649c6577ebc3)

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.

![](https://3083465318-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FsR50Wa7EwZGlmPSAMkkf%2Fuploads%2F1JUVHfDWgrioaEzX0nqZ%2Fconfigure-tree.gif?alt=media\&token=698ab7ff-0e18-42ef-9931-8155b29f4ba9)

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

![](https://3083465318-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FsR50Wa7EwZGlmPSAMkkf%2Fuploads%2FVbia8EgQEGscK3nPgkZg%2F4_new_field.png?alt=media\&token=9c459da3-5757-48fd-b8d6-f793e02e5d3c)

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

![](https://3083465318-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FsR50Wa7EwZGlmPSAMkkf%2Fuploads%2Fdf9FyjeyhKv9241HJgKa%2F4.5.PNG?alt=media\&token=bc5360a6-8ff2-42df-84d3-19e222c52269)

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

![](https://3083465318-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FsR50Wa7EwZGlmPSAMkkf%2Fuploads%2F93lw2I0e7Dcwke34zm9V%2F5_layout_modification-1593152421383.PNG?alt=media\&token=9a696d02-1368-47d6-919b-c316dfd33e4b)

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.

![](https://3083465318-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FsR50Wa7EwZGlmPSAMkkf%2Fuploads%2FoYcidH4C1pnmJUyiG35C%2F6_field_expression-1593166234680.PNG?alt=media\&token=61df9b22-b8f8-42d2-a759-b6e7f2b8d0e7)

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

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

![](https://3083465318-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FsR50Wa7EwZGlmPSAMkkf%2Fuploads%2FOQ4lXMYvkAFYSscrQ8D1%2F7_collection_modification.png?alt=media\&token=e0b6c0fb-2982-4dbd-bbc3-f839cef4a265)

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

![](https://3083465318-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FsR50Wa7EwZGlmPSAMkkf%2Fuploads%2FSgRnV8Gd4qghSeZkQbRZ%2F8_route_based_on_rule.png?alt=media\&token=60540e95-d1a8-4db4-b45b-e44b09310a97)

7. Add a new rule by clicking on the *Add switch condition* icon ![9\_icon](https://3083465318-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FsR50Wa7EwZGlmPSAMkkf%2Fuploads%2FjJ90jMqd27pc4nuwuwG2%2F11.png?alt=media\&token=d8288fa5-3cee-4c0b-89f8-bf160e65b31c).

Now, write an expression to route late arriving orders and name this rule “LateArrivals”.

![](https://3083465318-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FsR50Wa7EwZGlmPSAMkkf%2Fuploads%2F0MzaBrL6NWcUk1kWlf0h%2F10_rule_for_route.png?alt=media\&token=81061995-b2d3-40d1-a838-cb426ae4f544)

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

![](https://3083465318-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FsR50Wa7EwZGlmPSAMkkf%2Fuploads%2F5h6wZZEMScoL8tRUpaJh%2F11_node_late_arrivals.png?alt=media\&token=d9264803-576e-4898-96e3-4371ed699cff)

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

![](https://3083465318-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FsR50Wa7EwZGlmPSAMkkf%2Fuploads%2F3NONPQ6wZ8g0PROoUcRZ%2F12_preview_output.png?alt=media\&token=c67ef8c5-da55-433c-9324-cd6eed5f6fd5)

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

![](https://3083465318-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FsR50Wa7EwZGlmPSAMkkf%2Fuploads%2FFksTfvBmceOvZGNEbnXV%2F13_data_preview.png?alt=media\&token=9ca012a4-39a6-46f7-acff-91404d7f0fbb)

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.
