top of page
Writer's pictureDhruvin Shah

Create Gantt Chart To Show Duration Between Start Time And End Time Using R Within Power BI


Overview

In this article, we will talk about how we can create a Gantt Chart using R script visual within the Power BI.

In Power BI, there are many custom visuals which provide flexibility to create a Gantt Chart. But my requirement was somewhat different. I have the following scenario and the following types of data.


Scenario

Please refer to the below screenshot.



I have information for different groups with their start time and end time. I want to represent a filled area with the line (duration) for every group.


Example

If Group 1 has a duration from 1:00 AM to 3:00 AM, I want to show the highlighted area between 1:00 PM to 3:00 PM. To represent this type of duration, I have developed the Gantt Chart.


Code

I have uploaded the sample data source file and sample Power BI file on GitHub. You can directly download the code using the following URL.


Prerequisites

Before we can proceed further, you must have the following software installed on your machine. I have already mentioned the installation process in my previous articles. I also mentioned the link with the name of the software.

Now, letā€™s get started!


Step 1

Add the R script visual in Power BI page.



Step 2

Drag the fields within R visuals.



Step 3

Open the code window and click on "Edit script in external editor".



Step 4

We have used the following two packages in the development of the Gantt Chart.

  • ggplot2

  • scales

If R Studio doesnā€™t have those packages installed, install the packages.

Go to the Packages tab, click on "Install". Type the name of the package and click on the "Install" button.



Step 5

Paste the following code.

  1. library(ggplot2)Ā Ā Ā Ā Ā Ā 

  2. library(scales)Ā Ā Ā Ā Ā 

  3. #Ā ConvertĀ timestampĀ toĀ POSIXctĀ andĀ performĀ sort.Ā Ā 

  4. dataset$`StartĀ Time`<-as.POSIXct(dataset$`StartĀ Time`,format="%Y-%m-%dT%H:%M:%OS",Ā tzĀ ="GMT")Ā Ā Ā Ā 

  5. dataset$`EndĀ Time`<-as.POSIXct(dataset$`EndĀ Time`,format="%Y-%m-%dT%H:%M:%OS",Ā tz="GMT")Ā Ā Ā Ā 

  6. ggplot(dataset,aes(x=dataset$`StartĀ Time`,Ā y=dataset$Group,Ā color=Ā dataset$Group))Ā +Ā Ā Ā 

  7. Ā geom_segment(aes(x=dataset$`StartĀ Time`,xend=dataset$`EndĀ Time`,yend=dataset$Group),size=15)Ā +Ā Ā Ā Ā 

  8. scale_colour_discrete(guide=guide_legend(override.aes=list(size=10)))Ā +Ā Ā Ā Ā 

  9. scale_x_datetime(breaksĀ =Ā date_breaks("2Ā hour"),Ā labels=date_format("%H:%M"))+Ā Ā Ā Ā 

  10. ggtitle("RescueĀ byĀ StartĀ TimeĀ andĀ EndĀ Time")Ā +Ā xlab("Time")Ā +Ā ylab("GroupĀ Name")Ā +Ā theme_bw()Ā  Ā 

Step 6

Select all the code and click the "Run" button.



Step 7

We can see the output in the tab of Plots. It is showing the following type of Gantt Chart.



Step 8

Copy the same code and place the code within the R Editor in Power BI and click on "Run" button.



Step 9

The R visual will look like following.



Step 10

Publish the report in your App Workspace. Make sure you have a Pro license activated, otherwise, you are not able to see the R script visualization.


Conclusion

This is how we can create a Gantt Chart within Power BI. Stay connected with me for these types of amazing articles in the future.

0 comments

ėŒ“źø€


bottom of page