# Critical Path in Gantt Chart

### What is Critical Path?

The Critical Path is the longest chain of dependent tasks in a project that decides the earliest possible finish date. If any task on this path is delayed, the whole project gets delayed too.

### How Does It Work?

* Most project tools (like MS Project, Smartsheet, etc.) calculate the Critical Path by identifying:
  * The task with the latest end date
  * All predecessor tasks linked by dependencies (like Finish-to-Start) that lead to the task

## Displaying the Critical Path in a Gantt Chart in Power BI

To visualize the Critical Path in a Gantt chart, you first need to prepare your data using Power Query; once the critical tasks are identified, you can then display the Critical Path using a xViz Gantt chart visual in Power BI.

{% hint style="success" %}
The Critical Path is closely linked to task dependencies. To learn how task dependencies are visualized in the xViz Gantt Chart, please refer to the documentation on [Connectors](https://docs.xviz.com/project-management/connectors)
{% endhint %}

### Step 1: Create Custom Function in Power Query

1. Create a new blank query, rename it to `GetCriticalPathTasks`, and paste the below code:

```powerquery
(TaskTable as table) as list =>
let
    // task with last End Date
    MaxEndTask = Table.Max(TaskTable, "End Date"),
    FinalTask = MaxEndTask[Task Name],

    // Predecessors tasks
    GetAllPredecessors = (targets as list, found as list) =>
        let
            step = Table.SelectRows(TaskTable, each List.Contains(targets, [Connect To]) and not List.Contains(found, [Task Name])),
            newTasks = step[Task Name],
            allFound = List.Union({found, newTasks}),
            next = if List.IsEmpty(newTasks) then allFound else @GetAllPredecessors(newTasks, allFound)
        in
            next,

    // list of tasks on critical path
    Result = List.Distinct(List.Combine({{FinalTask}, GetAllPredecessors({FinalTask}, {})}))
in
    Result
```

{% hint style="danger" %}

* Replace "End Date" with the actual column name you’re using for the task end date.
* Replace "Connect To" with the name of the column that represents the predecessor or linked task.
* Replace "Task Name" with the column name used for your project task names.
  {% endhint %}

2. Navigate to the Task Table in Power Query and add a custom column that returns `1` for critical path tasks and `0` for all others using the below code:

```powerquery
if List.Contains(GetCriticalPathTasks(ChangedTypes), [Task Name]) then 1 else 0
```

{% hint style="danger" %}
Replace "Task Name" with the column name used for your project task names
{% endhint %}

This will create a new column with a value of `1` for critical path tasks and `0` for all other tasks, which can then be used in the xViz Gantt chart to visually highlight the critical path.

<figure><img src="https://382510089-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FIuuIZWiXEx25eV75hmmJ%2Fuploads%2Fb5yh7Xtqkn1z0i2mYON3%2Fimage.png?alt=media&#x26;token=021defbf-5676-4de1-9c6f-3686355f3455" alt=""><figcaption><p>Image 1: Critical Path column</p></figcaption></figure>

### Step 2: Display Critical Path in Gantt Chart

1. Now, import [xViz Gantt Chart](https://appsource.microsoft.com/en-us/product/power-bi-visuals/WA200000891) from Microsoft AppSource into your report.

{% hint style="success" %}
If this is your first time working with custom visuals, then here are the steps to [import a custom visual from Microsoft AppSource](https://learn.microsoft.com/en-us/power-bi/developer/visuals/import-visual#import-a-power-bi-visual-directly-from-appsource).
{% endhint %}

2. Populate the Display Measure Data field using the Critical Path column created in Power Query.

<figure><img src="https://382510089-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FIuuIZWiXEx25eV75hmmJ%2Fuploads%2Fvi5gxXcrWZLoTfynkxP9%2Fimage.png?alt=media&#x26;token=57340f7f-216f-438c-81ff-ab1477b80a25" alt=""><figcaption><p>Image 2: Populare Critical Path Column</p></figcaption></figure>

3. Now apply conditional formatting using the rule below to visually highlight the Critical Path.

<figure><img src="https://382510089-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FIuuIZWiXEx25eV75hmmJ%2Fuploads%2FjSOfgVFvtVekWPJg0HBc%2Fimage.png?alt=media&#x26;token=ed9bf6a3-7113-4724-aba7-6642e960b320" alt="" width="335"><figcaption><p>Image 3: Apply Conditional Formatting</p></figcaption></figure>

{% hint style="info" %}
For more details on how to apply conditional formatting in xViz Gantt chart, please refer to the documentation on [steps-to-enable-conditional-formatting](https://docs.xviz.com/project-management/alerting-techniques/steps-to-enable-conditional-formatting "mention")
{% endhint %}

As a result, the Critical Path will be highlighted, 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%2FTPUYJ0NlSpVctw3EFH2l%2Fimage.png?alt=media&#x26;token=0bdcf08d-3255-4e02-8914-13f317602bd1" alt=""><figcaption><p>Image 4: Critical Path</p></figcaption></figure>

Here is an example report file:

{% file src="<https://382510089-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FIuuIZWiXEx25eV75hmmJ%2Fuploads%2FSMKBTiqQqeYzjUcdqvLY%2FxViz%20Gantt%20Chart%20-%20Critical%20Path.pbix?alt=media&token=0e665331-8e76-4bb6-9a5b-b5ec52a154d1>" %}
