R script in Power BI returns date as Microsoft.OleDb.Date

后端 未结 2 1404
滥情空心
滥情空心 2021-01-12 23:43

I\'m trying to transform a table in Power BI using the Run R Script functionality in Edit Query. The source of the table is a csv file with a colum

2条回答
  •  青春惊慌失措
    2021-01-13 00:17

    I wandered upon this answer last week and found that it didn't work for me. I'd receive an error from the R script that "character string is not in a standard unambiguous format." I'm assuming this is due to the many updates that have happened with Power BI in the years since the original answer, because as far as I could tell, all dates were in the exact same format (this error did not occur if I ran the data separately in R/RStudio). I figured I'd leave my solution for those who happen upon this like I did.

    I did the exact same thing as vestland's solution, except instead of changing the data type to text, I had to change it to a whole number:

    1) Edit query. 2) Convert all date columns to "Whole Number." 3) Run R Script, and convert date columns from numbers to date:

    In R, this requires that you use as.Date() or lubridate::as_date(), with the origin argument, origin = "1899-12-30" to get the correct date when you convert from whole number back to date. (This is the origin instead of "1900-01-01" because Excel/Power BI don't account for two leap years in early 20th century, I've heard.)

提交回复
热议问题