# 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](/project-management/connectors.md)
{% 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="/files/nLs9UZDQOK76L7ihA4YY" 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="/files/gbRBvGexl5NZcFkDuXCR" 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="/files/Jve33eH4muJkINYB0zbd" 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](/project-management/alerting-techniques/steps-to-enable-conditional-formatting.md)
{% endhint %}

As a result, the Critical Path will be highlighted, as shown in the image below:

<figure><img src="/files/mXILsbRPlqt2eqLZtoQu" alt=""><figcaption><p>Image 4: Critical Path</p></figcaption></figure>

Here is an example report file:

{% file src="/files/6kcQIXom4aFLiJpFXjCQ" %}


---

# 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://docs.xviz.com/project-management/tips-and-tricks/critical-path-in-gantt-chart.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.
