Change Dataset column from Character to Date

I am trying to change the type of a column in a dataset from Power Bi from Character to Date.

The results from str(WeekStartDate) is chr "2021-06-28T00:00:00.0000000"

I need to change the type of this dataset column to Date. I have tried:

dataset.WeekStartDate <- as.date(WeekStartDate)

and every other way I can find, but it either will not change, or if it does change the script give me a data.frame error, since it creates a new data.frame(WeekStartDate)

I could not find a way to upload a sample CVS file, so I will past a few rows below.

Line,WeekStartDate,Percentage
Line1,2021-06-28T00:00:00.0000000,17.5066313
Line1,2021-07-05T00:00:00.0000000,22.71062271
Line1,2021-07-12T00:00:00.0000000,44.24057085
Line1,2021-07-19T00:00:00.0000000,68.34817013
Line1,2021-07-26T00:00:00.0000000,61.95965418
Line1,2021-08-02T00:00:00.0000000,22.14983713
Line1,2021-08-09T00:00:00.0000000,28.43691149
Line2,2021-06-28T00:00:00.0000000,25.73633441
Line2,2021-07-05T00:00:00.0000000,32.07489451
Line2,2021-07-12T00:00:00.0000000,21.69795038
Line2,2021-07-19T00:00:00.0000000,40.61744966
Line2,2021-07-26T00:00:00.0000000,37.43225806
Line2,2021-08-02T00:00:00.0000000,37.1459695
Line2,2021-08-09T00:00:00.0000000,16.46602972

Hi, Mark. How are you reading your CSV file? When I use read_csv() from the readr library the values are converted to date-time values.

csv <- readr::read_csv("~/temp/data.csv")
str(csv$WeekStartDate)
> str(csv$WeekStartDate)
POSIXct[1:4], format: "2021-06-28" "2021-07-05" "2021-07-12" "2021-07-19"
the_datetime <- "2021-06-28T00:00:00.0000000"
lubridate::as_date(the_datetime)
#> [1] "2021-06-28"

Here is the code that Power BI R Visual creates.

# The following code to create a dataframe and remove duplicated rows is always executed and acts as a preamble for your script: 

#  <- data.frame()
#  <- unique()

# Paste or type your script code here:
library(ggplot2)
library(scales)
library(extrafont)

loadfonts(device="win")

I figured it out. I must of typed something wrong when I tried this yesterday.

dataset$WeekStartDate <-ymd(str_sub(dataset$WeekStartDate,1,10))

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.