# Data Structure for Columnar Group View

This document explains the process to dynamically convert a standard data structure to the data structure required to plot a columnar group view in the xViz Performance Flow visual

<figure><img src="https://382510089-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FIuuIZWiXEx25eV75hmmJ%2Fuploads%2F63UllXh11t0ztttamziW%2Fimage.png?alt=media&#x26;token=53b4200b-4351-428b-aee8-70b034018e9e" alt=""><figcaption><p><em>Image 1: Column Group (Swim lane) View in xViz Performance Flow | Power BI</em></p></figcaption></figure>

{% hint style="success" %}
**Prerequisite** - Before proceeding with this page, please go through the following pages to ensure a comprehensive understanding of the configurations required:

* [Data Structure](https://docs.xviz.com/performance-management/configuring-the-chart-structure)
* [Swim Lanes](https://docs.xviz.com/performance-management/configuring-the-chart-structure/grouping-swim-lanes)
  {% endhint %}

Let’s explore this with an example. We have a dataset with specific IDs, but we don't have the Parent ID, Group ID, and Subtree ID included. However, we can easily create these columns in [Power Query](https://learn.microsoft.com/en-us/power-bi/transform-model/desktop-query-overview#power-query-editor) using a few transformation steps.

{% hint style="success" %}
Reference links have been provided next to each step for your convenience, allowing you to explore more about that specific transformation if you are encountering it for the first time.
{% endhint %}

### Steps to dynamically convert your data for the Swimlane view:

1. We have a Source table with the columns Id, Title, Subtitle, and Group Name. The Group Name column represents the groups that we used for grouping to create swim lanes.

<figure><img src="https://382510089-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FIuuIZWiXEx25eV75hmmJ%2Fuploads%2FCHXIUR6ArKnn9GtwFv06%2Fimage.png?alt=media&#x26;token=5045131a-52cb-4f81-a330-58e4f67cc34f" alt=""><figcaption><p><em>Image 2: Source data structure</em></p></figcaption></figure>

2. We sort the data by "Group Name" column to ensure that unique group names are arranged in the correct sequence.

<figure><img src="https://382510089-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FIuuIZWiXEx25eV75hmmJ%2Fuploads%2FoNHnNxMxcEwFXrDxNEBX%2Fimage.png?alt=media&#x26;token=c4217df3-36b8-4e88-a8b9-30463beeefa7" alt=""><figcaption><p><em>Image 3: Data sorted by the Group Name column</em></p></figcaption></figure>

{% hint style="info" %}
Sort the Group Name column according to your requirements for preferred group order. This will ensure that the Subtree Id and Group Id are ordered according to the sorted column.
{% endhint %}

3. Add an Index column starting from 1 - [Add an index column | Power Query](https://learn.microsoft.com/en-us/power-query/add-index-column)

<figure><img src="https://382510089-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FIuuIZWiXEx25eV75hmmJ%2Fuploads%2FlwabcsZlhgA4XhavjDZ6%2Fimage.png?alt=media&#x26;token=b2ad265c-5bca-4f71-b85b-827d6a99062e" alt=""><figcaption><p><em>Image 4: Index column added</em></p></figcaption></figure>

4. Next, we will add the "Subtree Id" column - [Add a custom column | Power Query](https://learn.microsoft.com/en-us/power-query/add-custom-column#create-a-custom-column)\
   Create a Custom Column - paste the code below as its definition and name the column as "Subtree Id":

{% code overflow="wrap" %}

```powerquery
let

        // Create a list of unique Group Name values
        UniqueGroupName = List.Distinct(#"Sorted Rows"[Group Name]),

        // Create a mapping table from Group Name values to numbers
        GroupNameMapping = List.Zip({UniqueGroupName, List.Transform({1..List.Count(UniqueGroupName)}, each _)}),

        // Convert mapping to a record 
        GroupNameRecord = Record.FromList(List.Transform(GroupNameMapping, each _{1}), List.Transform(GroupNameMapping, each _{0})),

        // Lookup the Group Name number from the record
        GroupNameNumber = Record.Field(GroupNameRecord, [Group Name]),

        // Subtree ID
        SubtreeID = "G" & Text.From(GroupNameNumber),

        // if the current row is the first in its Group Name
        CurrentGroupName = [Group Name],
        CurrentIndex = [Index],
        PreviousRows = Table.SelectRows(#"Added Index", each [Group Name] = CurrentGroupName and [Index] < CurrentIndex),
        IsFirst = Table.RowCount(PreviousRows) = 0
    in
        if IsFirst then SubtreeID else null
```

{% endcode %}

{% hint style="danger" %}
Make sure to replace the "Group Name" text in the above code with the name of the column you used for grouping.
{% endhint %}

This will result in a new column named "Subtree Id" is displayed in the image below:

<figure><img src="https://382510089-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FIuuIZWiXEx25eV75hmmJ%2Fuploads%2FOQuRzJNcSWbczLbUWXVF%2Fimage.png?alt=media&#x26;token=e20cab56-549f-40f8-bc5d-69283bcb47fc" alt=""><figcaption><p><em>Image 5: Subtree Id column added</em></p></figcaption></figure>

5. Then, we will add the "Parent Id" column - [Add a custom column | Power Query](https://learn.microsoft.com/en-us/power-query/add-custom-column#create-a-custom-column)\
   Create a Custom Column - paste the code below as its definition and name the column as "PId":&#x20;

{% code overflow="wrap" %}

```powerquery
let
            // Get the Group Name and Index
            CurrentGroupName = [Group Name],
            CurrentIndex = [Index],
            // Filter previous rows with same Group Name
            PreviousRows = Table.SelectRows(#"Added Resident Id", each [Group Name] = CurrentGroupName and [Index] < CurrentIndex),
            // If there are previous rows, get the ID of the most recent one
            PreviousID = if Table.RowCount(PreviousRows) > 0 then PreviousRows{Table.RowCount(PreviousRows) - 1}[Id] else null
        in
            PreviousID
```

{% endcode %}

{% hint style="danger" %}
Make sure to replace the "Group Name" text in the above code with the name of the column you used for grouping.
{% endhint %}

This will result in a new column named "PId" is displayed in the image below:

<figure><img src="https://382510089-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FIuuIZWiXEx25eV75hmmJ%2Fuploads%2FPXTZaoz8hw02ajDgI959%2Fimage.png?alt=media&#x26;token=08626d46-1162-404e-ae05-0d2e70e30f84" alt=""><figcaption><p><em>Image 6: Added Parent ID column</em></p></figcaption></figure>

6. Now, we will add the "Group Id" column- [Add a custom column | Power Query](https://learn.microsoft.com/en-us/power-query/add-custom-column#create-a-custom-column)\
   Create a Custom Column. Then enter double quotes in the custom formula tab to create a blank column for now. Name this column as "Group Id":

<figure><img src="https://382510089-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FIuuIZWiXEx25eV75hmmJ%2Fuploads%2FQW6eom0x2IT44GxqMWiw%2Fimage.png?alt=media&#x26;token=bf7667bc-5517-4342-a2aa-2f7e930a7dd4" alt="" width="563"><figcaption><p><em>Image 7: Group Id column definition</em></p></figcaption></figure>

This will result in a blank column named "Group Id" is displayed in the image below:

<figure><img src="https://382510089-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FIuuIZWiXEx25eV75hmmJ%2Fuploads%2Fp3hnw2RfLKNN5WwguYKl%2Fimage.png?alt=media&#x26;token=a7a836bf-297d-4db4-93cb-6f06e95ec2aa" alt=""><figcaption><p><em>Image 8: Blank Group ID column added</em></p></figcaption></figure>

7. We will now perform basic transformations, such as changing data types and reordering columns, as shown in the image below:

<figure><img src="https://382510089-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FIuuIZWiXEx25eV75hmmJ%2Fuploads%2FGrUs3sFQZWA9z6dpiiQ7%2Fimage.png?alt=media&#x26;token=4cfe1157-a852-4126-98e5-156b5a1e058f" alt=""><figcaption><p><em>Image 9: Basic Transformations</em></p></figcaption></figure>

8. Now, we will create a static table to define the groups in the visual.&#x20;
   1. Click on "Enter Data" in the Home tab of Power Query Editor and input the data - [Enter Data Directly | Power BI](https://learn.microsoft.com/en-us/power-bi/connect-data/desktop-enter-data-directly-into-desktop)
   2. Enter the same values in both the Group Id and Id columns which we got in Subtree Id during transformation.&#x20;
   3. &#x20;Now, add the Group Names to the Title column to create a suitable structure for swim lanes, leaving the remaining columns blank.

{% hint style="info" %}

* After entering the data, we will append both queries. Ensure that the structure and column names in the static table match those in the source table.
* You can choose to maintain this table in any data source of your choice and connect to the report
  {% endhint %}

<figure><img src="https://382510089-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FIuuIZWiXEx25eV75hmmJ%2Fuploads%2F5woKWCSgxKEM1pAtpZ5P%2Fimage.png?alt=media&#x26;token=343c775c-db90-48e7-85f2-ca1207fd4a92" alt=""><figcaption><p><em>Image 10: Static Table of Groups</em></p></figcaption></figure>

9. Finally, we will append the Static Groups table to the Source table - [Append Queries | Power Query](https://learn.microsoft.com/en-us/power-query/append-queries)

<figure><img src="https://382510089-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FIuuIZWiXEx25eV75hmmJ%2Fuploads%2FDeCyBYYTaUHQaD9ZX1y9%2Fimage.png?alt=media&#x26;token=49218f49-8a17-47f9-8ec3-184e767d72a8" alt=""><figcaption><p><em>Image 11: Appending Static Table to the Source Table</em></p></figcaption></figure>

Our data structure is now ready!

<figure><img src="https://382510089-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FIuuIZWiXEx25eV75hmmJ%2Fuploads%2FIUxbuDdKJWphqc0QeTOZ%2Fimage.png?alt=media&#x26;token=fc098a9b-41e9-4d1c-a554-d8fb7b1d16dd" alt=""><figcaption><p><em>Image 12: Data Structure suitable for Swim Lanes</em></p></figcaption></figure>

Finally, populate the data columns in the visual to display the data in swim lanes, as shown in the image below:

<figure><img src="https://382510089-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FIuuIZWiXEx25eV75hmmJ%2Fuploads%2FXoD7YJv6KDMPOfcUte8P%2Fimage.png?alt=media&#x26;token=5560f8c5-b58f-4511-857b-2dbf8503e9de" alt=""><figcaption><p><em>Image 13: Column Group (Swim lane) View in xViz Performance Flow | Power BI</em></p></figcaption></figure>

{% hint style="danger" %}
The transformation code blocks are suited to create simple groups and linear structures of nodes within groups as displayed in the last image.&#x20;

*If the parent node is in a group, the child node will automatically be included in the same group and displayed below the parent node. To position nodes side by side within a group, skip adding Parent ID but provide the Subtree ID for both nodes.*
{% endhint %}

### Sample Report File - Columnar Group View

{% file src="<https://382510089-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FIuuIZWiXEx25eV75hmmJ%2Fuploads%2FQNDpYmtBvAmm6GoHNyyy%2FxViz%20Performance%20Flow%20-%20Transformation%20for%20Swim%20Lanes%20Structure.pbix?alt=media&token=6d459153-7293-4eb7-abe9-7901ecb61780>" %}

{% hint style="success" %}
This example is inspired by common, real-life dataset structures. Your source data structure may vary and may require additional data transformations to align with this example.
{% endhint %}

> Refer to the documentation link below for details on the group structure, which allows multiple nodes to be displayed side by side within a single group - [data-structure-for-wide-group-views](https://docs.xviz.com/performance-management/configuring-the-chart-structure/grouping-swim-lanes/data-structure-for-wide-group-views "mention")
