Skip to main content
cancel
Showing results for 
Search instead for 
Did you mean: 

Register now to learn Fabric in free live sessions led by the best Microsoft experts. From Apr 16 to May 9, in English and Spanish.

Reply
Anonymous
Not applicable

Problems with Date Format and R Script

Hello everybody,

 

I'm not getting the result I need, when I try to build a small GANTT-like Chart via R in PBI.

 

See the code as following:

 


`dataset` = read.csv('C:/Users/cgmeiner/AppData/Local/Radio/REditorWrapper_00f11c68-b4d0-4572-9e0d-c4ba7e30421c/input_df_9a681b40-b3ce-4e89-aed9-0fb6c51643f0.csv', check.names = FALSE, encoding = "UTF-8", blank.lines.skip = FALSE);
library(ggplot2) # Visualization
library(date)

#lct <- Sys.getlocale("LC_TIME"); Sys.setlocale("LC_TIME", "C")
#Sys.setlocale("LC_TIME", lct)


#Start3 <- as.date(paste(substr(dataset$Start,6,7),"-",substr(dataset$Start,9,10), "-", substr(dataset$Start, 0, 4)))
#Ende3 <- as.date(paste(substr(dataset$Ende,6,7),"-",substr(dataset$Ende,9,10), "-", substr(dataset$Ende, 0, 4)))


ggplot(dataset,aes(x=as.date(paste(substr(dataset$Start,6,7),"-",substr(dataset$Start,9,10), "-", substr(dataset$Start, 0, 4))),
                   y=dataset$Hauptkasse_Name, color=PZN_Company)) +
  geom_segment(aes(x=as.date(paste(substr(dataset$Start,6,7),"-",substr(dataset$Start,9,10), "-", substr(dataset$Start, 0, 4))),
                   xend=as.date(paste(substr(dataset$Ende,6,7),"-",substr(dataset$Ende,9,10), "-", substr(dataset$Ende, 0, 4))),
                   yend=dataset$Hauptkasse_Name),size=10) +
  scale_colour_discrete(guide=guide_legend(override.aes=list(size=10))) +

ggtitle("Tender Duration") + xlab("") + ylab("") + theme_bw()+
 


scale_x_date(date_breaks = "1 month") +
 
theme(axis.text.x = element_text(angle = 90)) 

 

For some reasons, the Start/Ende fields are defined as date in PBI, but as soon as the fields hit R they turn into either a factor or a character. Now I created the workaround above. Still I get the following error message:

"Error: Invalid input: date_trans works with objects of class Date only".

 

This is related to scale_x_date function. I'd be happy for any support or tipps from all of you.

 

many thanks in advance

 

Christoph

 

 

1 ACCEPTED SOLUTION
Anonymous
Not applicable

Thanks for your reply and the link for another way for a GANTT Chart in R.
In the meantime I solved my issue. When using datetime format instead of date format everything is working.
Thanks and br

View solution in original post

2 REPLIES 2
v-yulgu-msft
Employee
Employee

Hi @Anonymous,

 

In your scenario, since Start and End fields are both set to date format, why did you split and combine string via "paste(substr(dataset$Start,6,7),"-",substr(dataset$Start,9,10), "-", substr(dataset$Start, 0, 4))"?

 

Could you get expected output running above code in R tool rather than in Power BI? Besides, please share your data source .csv file so that I can test for you.

 

Here is an example that generate Gantt chart using R script, please see:

Gantt chart using R

 

Regards,
Yuliana Gu

Community Support Team _ Yuliana Gu
If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.
Anonymous
Not applicable

Thanks for your reply and the link for another way for a GANTT Chart in R.
In the meantime I solved my issue. When using datetime format instead of date format everything is working.
Thanks and br

Helpful resources

Announcements
Microsoft Fabric Learn Together

Microsoft Fabric Learn Together

Covering the world! 9:00-10:30 AM Sydney, 4:00-5:30 PM CET (Paris/Berlin), 7:00-8:30 PM Mexico City

PBI_APRIL_CAROUSEL1

Power BI Monthly Update - April 2024

Check out the April 2024 Power BI update to learn about new features.

April Fabric Community Update

Fabric Community Update - April 2024

Find out what's new and trending in the Fabric Community.