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

Earn the coveted Fabric Analytics Engineer certification. 100% off your exam for a limited time only!

Reply
Anonymous
Not applicable

Error in R Script

Hello! I am getting an error about ambiguous strings when I try to run the following R script in Power BI. I am trying to determind a status for a part per day based on the create date and complete date. It works great in Rstudio with no errors. Here's some sample data: 

 

IdOldIdPartIdProcessStepIdCreateDateCompleteDateOrderDueDateCheck
1525988045047532021-06-10T16:38:09.5970000null06/12/2021FALSE
1525985045047432021-06-10T16:38:06.4100000null06/14/2021FALSE
1525982045047332021-06-10T16:38:03.3500000null06/15/2021FALSE
1525979045047232021-06-10T16:37:59.8000000null06/16/2021FALSE
1525976045047132021-06-10T16:37:56.71300006/20/202106/17/2021FALSE

 

 

Here's the R script: 

 

 

# 'dataset' holds the input data for this script

library(tidyverse)
library(lubridate)

start_date <- as.Date(as.POSIXct(parse_date_time('2018-10-04', orders="ymd")))

end_date <- as.Date(today(), format= "%Y-%m-%d")

date_range <- as.list(seq.Date(start_date, end_date, by = "day"))


for (i in seq_along(date_range)) {


dataset <- dataset %>%
dplyr::mutate(!!paste0(as.Date(start_date + i-1, format= "%Y-%m-%d")) := case_when(CompleteDate == as.Date(start_date + i-1, format= "%Y-%m-%d") ~ "ORDERED",
CreateDate == as.Date(start_date + i-1, format= "%Y-%m-%d") ~ "NEW",
CreateDate < as.Date(start_date + i-1, format= "%Y-%m-%d") & (is.na(CompleteDate) | CompleteDate > as.Date(start_date + i-1, format= "%Y-%m-%d")) ~ "InProcess"))
}

 

 

And here is the error: 

 

MWoo8_0-1624979372600.png

 

I have tried changing the format of that input and have not gotten anywhere, and then I thought defining the date format might work but that did not either. Any help is appreciated!!

1 ACCEPTED SOLUTION
V-pazhen-msft
Community Support
Community Support

@Anonymous 

Power bi somehow resturns "Microsft.OleDb.Date" as the result. Please refer to R script in Power BI returns date as Microsoft.OleDb.Date - Stack Overflow 

 

1. As mentioned in the solution, first change the date type to Text type. 

#"Changed Type" = Table.TransformColumnTypes(Source,{{"Id", Int64.Type}, {"OldId", Int64.Type}, {"PartId", Int64.Type}, {"ProcessStepId", Int64.Type}, {"CreateDate", type datetime}, {"CompleteDate", type text}, {"Order", Int64.Type}, {"DueDate", type text}, {"Check", type text}}),

 

2. Use output instead of dataset, and add as.date() to convert it to date. 

output <- dataset %>%
as.Date(start_date + i-1, format= ""%Y-%m-%d"") & (is.na(as.Date(CompleteDate)) |
as.Date(CompleteDate) > as.Date(start_date + i-1, format= ""%Y-%m-%d"")) ~ ""InProcess""))#(lf)}",[dataset=#"Changed Type"]),

 

3. The output will text and remembe rto change it to date type. Here is the full code:

let
Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("dcw9CsMwDAXgu3hOHP1Yku2tQztla7eQ+1+jKrjUpLbgwQNJ33EEFJKSc1gCeJJAMvHCHgLCFXRFeKFWzhVKlGLg49v28TnadEPy/rjtz3s4l68qvZqmqsaEMFbTQKVe5anKkWWiyr9qpVdprFqVEjNMVB2o2qs4VTUaclMbR3Dh7cefbw==", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Id = _t, OldId = _t, PartId = _t, ProcessStepId = _t, CreateDate = _t, CompleteDate = _t, Order = _t, DueDate = _t, Check = _t]),
#"Changed Type" = Table.TransformColumnTypes(Source,{{"Id", Int64.Type}, {"OldId", Int64.Type}, {"PartId", Int64.Type}, {"ProcessStepId", Int64.Type}, {"CreateDate", type datetime}, {"CompleteDate", type text}, {"Order", Int64.Type}, {"DueDate", type text}, {"Check", type text}}),
#"Run R script" = R.Execute("# 'dataset' holds the input data for this script#(lf)library(tidyverse)#(lf)library(lubridate)#(lf)#(lf)start_date <- as.Date(as.POSIXct(parse_date_time('2018-10-04', orders=""ymd"")))#(lf)#(lf)end_date <- as.Date(today(), format= ""%Y-%m-%d"")#(lf)#(lf)date_range <- as.list(seq.Date(start_date, end_date, by = ""day""))#(lf)#(lf)#(lf)for (i in seq_along(date_range)) {#(lf)#(lf)#(lf)output <- dataset %>%#(lf)dplyr::mutate(!!paste0(as.Date(start_date + i-1, format= ""%Y-%m-%d"")) := case_when(as.Date(CompleteDate) == as.Date(start_date + i-1, format= ""%Y-%m-%d"") ~ ""ORDERED"",#(lf)as.Date(CreateDate) == as.Date(start_date + i-1, format= ""%Y-%m-%d"") ~ ""NEW"",#(lf)as.Date(CreateDate) < as.Date(start_date + i-1, format= ""%Y-%m-%d"") & (is.na(as.Date(CompleteDate)) | as.Date(CompleteDate) > as.Date(start_date + i-1, format= ""%Y-%m-%d"")) ~ ""InProcess""))#(lf)}",[dataset=#"Changed Type"]),
output = #"Run R script"{[Name="output"]}[Value],
#"Changed Type1" = Table.TransformColumnTypes(output,{{"CompleteDate", type date}, {"DueDate", type date}})
in
#"Changed Type1"

Vpazhenmsft_0-1625133243463.png

 

 

Paul Zheng _ Community Support Team
If this post helps, please Accept it as the solution to help the other members find it more quickly.

View solution in original post

3 REPLIES 3
V-pazhen-msft
Community Support
Community Support

@Anonymous 

Power bi somehow resturns "Microsft.OleDb.Date" as the result. Please refer to R script in Power BI returns date as Microsoft.OleDb.Date - Stack Overflow 

 

1. As mentioned in the solution, first change the date type to Text type. 

#"Changed Type" = Table.TransformColumnTypes(Source,{{"Id", Int64.Type}, {"OldId", Int64.Type}, {"PartId", Int64.Type}, {"ProcessStepId", Int64.Type}, {"CreateDate", type datetime}, {"CompleteDate", type text}, {"Order", Int64.Type}, {"DueDate", type text}, {"Check", type text}}),

 

2. Use output instead of dataset, and add as.date() to convert it to date. 

output <- dataset %>%
as.Date(start_date + i-1, format= ""%Y-%m-%d"") & (is.na(as.Date(CompleteDate)) |
as.Date(CompleteDate) > as.Date(start_date + i-1, format= ""%Y-%m-%d"")) ~ ""InProcess""))#(lf)}",[dataset=#"Changed Type"]),

 

3. The output will text and remembe rto change it to date type. Here is the full code:

let
Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("dcw9CsMwDAXgu3hOHP1Yku2tQztla7eQ+1+jKrjUpLbgwQNJ33EEFJKSc1gCeJJAMvHCHgLCFXRFeKFWzhVKlGLg49v28TnadEPy/rjtz3s4l68qvZqmqsaEMFbTQKVe5anKkWWiyr9qpVdprFqVEjNMVB2o2qs4VTUaclMbR3Dh7cefbw==", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Id = _t, OldId = _t, PartId = _t, ProcessStepId = _t, CreateDate = _t, CompleteDate = _t, Order = _t, DueDate = _t, Check = _t]),
#"Changed Type" = Table.TransformColumnTypes(Source,{{"Id", Int64.Type}, {"OldId", Int64.Type}, {"PartId", Int64.Type}, {"ProcessStepId", Int64.Type}, {"CreateDate", type datetime}, {"CompleteDate", type text}, {"Order", Int64.Type}, {"DueDate", type text}, {"Check", type text}}),
#"Run R script" = R.Execute("# 'dataset' holds the input data for this script#(lf)library(tidyverse)#(lf)library(lubridate)#(lf)#(lf)start_date <- as.Date(as.POSIXct(parse_date_time('2018-10-04', orders=""ymd"")))#(lf)#(lf)end_date <- as.Date(today(), format= ""%Y-%m-%d"")#(lf)#(lf)date_range <- as.list(seq.Date(start_date, end_date, by = ""day""))#(lf)#(lf)#(lf)for (i in seq_along(date_range)) {#(lf)#(lf)#(lf)output <- dataset %>%#(lf)dplyr::mutate(!!paste0(as.Date(start_date + i-1, format= ""%Y-%m-%d"")) := case_when(as.Date(CompleteDate) == as.Date(start_date + i-1, format= ""%Y-%m-%d"") ~ ""ORDERED"",#(lf)as.Date(CreateDate) == as.Date(start_date + i-1, format= ""%Y-%m-%d"") ~ ""NEW"",#(lf)as.Date(CreateDate) < as.Date(start_date + i-1, format= ""%Y-%m-%d"") & (is.na(as.Date(CompleteDate)) | as.Date(CompleteDate) > as.Date(start_date + i-1, format= ""%Y-%m-%d"")) ~ ""InProcess""))#(lf)}",[dataset=#"Changed Type"]),
output = #"Run R script"{[Name="output"]}[Value],
#"Changed Type1" = Table.TransformColumnTypes(output,{{"CompleteDate", type date}, {"DueDate", type date}})
in
#"Changed Type1"

Vpazhenmsft_0-1625133243463.png

 

 

Paul Zheng _ Community Support Team
If this post helps, please Accept it as the solution to help the other members find it more quickly.

Anonymous
Not applicable

Thank you so much, this is definitely helping! However, it is not showing the "InProcess", "Ordered", or "New" statuses in the columns that were created, they're all null. It looks like yours is showing up correctly, so wondering what is different.

 

Columns showing correctly but all null: 

 

MWoo8_0-1625228608497.png

 

Code (changed date columns to text prior to running):

# 'dataset' holds the input data for this script

library(tidyverse)
library(lubridate)

start_date <- as.Date(as.POSIXct(parse_date_time('2018-10-04', orders="ymd")))

end_date <- as.Date(as.POSIXct(parse_date_time('2018-10-07', orders="ymd")))

date_range <- as.list(seq.Date(start_date, end_date, by = "day"))


for (i in seq_along(date_range)) {


dataset<- dataset %>% 
    mutate(!!paste0(start_date + i-1) := case_when( as.Date(CompleteDate, format= "%Y-%m-%d") == as.Date(start_date + i-1, format= "%Y-%m-%d") ~  "ORDERED",  
                             as.Date(CreateDate, format= "%Y-%m-%d") == as.Date(start_date + i-1, format= "%Y-%m-%d") ~ "NEW",
                             as.Date(CreateDate,format= "%Y-%m-%d") < as.Date(start_date + i-1, format= "%Y-%m-%d") & (is.na(as.Date(CompleteDate, format= "%Y-%m-%d")) | as.Date(CompleteDate, format= "%Y-%m-%d") > as.Date(start_date + i-1, format= "%Y-%m-%d")) ~ "InProcess"))


}

output <- dataset

 

 I used output <- dataset at the end because if I have output in the loop, it only adds one column and I am looking to add one column for each date in the date range. 

Anonymous
Not applicable

Actually, ended up getting it to work using someone's comment in Stack Overflow to change the Date columns to whole numbers, and then set an orgin to "1899-12-30". Thanks!!

Helpful resources

Announcements
April AMA free

Microsoft Fabric AMA Livestream

Join us Tuesday, April 09, 9:00 – 10:00 AM PST for a live, expert-led Q&A session on all things Microsoft Fabric!

March Fabric Community Update

Fabric Community Update - March 2024

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

Top Solution Authors
Top Kudoed Authors