Forum Discussion

dsabsi's avatar
dsabsi
Advocate I
9 years ago
Solved

How to convert text (data type) to a date (data type)?

Hello guys, 

 

I need your help with converting a text column towards a data type. 

Should i add a column with a certain formula to achieve this? 

 

See below the error that I'm getting from this: 

Many thanks!!!

 

Cheers,

 

DSabsi 

 

 

 

  • Sure (I included some Dutch as well :-) ):

     

    let
        Source = #table(type table[Verwachte_Start = text],{{"1499032800"},{"1498860000"},{"1493589600"},{"1491170400"}}),
        AddedUTCDateTime = Table.AddColumn(Source, "Verwachte_startdatum/tijd_UTC", each #datetimezone(1970,1,1,0,0,0,0,0) + #duration(0,0,0,Number.From([Verwachte_Start])), type datetimezone),
        AddedLocalDate = Table.AddColumn(AddedUTCDateTime, "Verwachte_startdatum_lokaal", each Date.From([#"Verwachte_startdatum/tijd_UTC"]), type date)
    in
        AddedLocalDate

14 Replies

  • Can you give us a sample of what your text data looks like and what data type you are trying to convert it to?  That would help in providing an answer

    • dsabsi's avatar
      dsabsi
      Advocate I

      Hello guys, 

       

      My apologies for my late response. 

      Hereby the sample of the data that I got from my boss:

       

       

      The format is text and I wish to change it to dates. 

       

      I hope you can help me, I'm really astonished by the amount of replies (many thanks!!!!)

       

      Cheers, 

       

      DSabsi

  • SivaMani's avatar
    SivaMani
    Resident Rockstar

    HI dsabsi

    Go to query editor and create custom column with the following formula,

     

    Date.FromText([DateField],Corresponding culture code)

     

    Regards,

    Siva

    • MarcelBeug's avatar
      MarcelBeug
      Community Champion

      SivaMani How do you know that the culture code should be "en-GB", without any information from dsabsi how his dates look like?

       

      Just to give an impression: below some date formats for April 1, 2017, and the number of culture codes using that format:

      • SivaMani's avatar
        SivaMani
        Resident Rockstar

        MarcelBeug In my experience, Power BI's default date format is MM/DD/YYYY - Because of Locale(by default) in English(United State).  

         

        GB's date format is DD/MM/YYYY.

         

        According to your sample date formats,

        4th (2017-04-01) and 6th(4/1/2017) formats will be detected as a date by Power BI.

        The Rest of them, we have to convert it to the required format. Since it is in English(GB) format. So I have used "en-GB"

         

        I hope I've made it clear enough. 

         

        If you have anything apart from this, just tell me it will be helpful.

         

        Thanks,

        Siva

         

  • Anonymous's avatar
    Anonymous
    Not applicable

    Hi dsabsi,

     

    It will be help if you share some sample data or date structure.

     

    Regards,

    Xiaoxin Sheng

    • dsabsi's avatar
      dsabsi
      Advocate I

      Hi Guys AnonymousSivaManiMarcelBeugblopez11

       

      I found out that the text value is an epoch value in a text value. 

      Do you know how to convert an epoch value in a text format to dates value in date format? 

       

      Cheers, 

       

      DSabsi

      • MarcelBeug's avatar
        MarcelBeug
        Community Champion

        Sure (I included some Dutch as well :-) ):

         

        let
            Source = #table(type table[Verwachte_Start = text],{{"1499032800"},{"1498860000"},{"1493589600"},{"1491170400"}}),
            AddedUTCDateTime = Table.AddColumn(Source, "Verwachte_startdatum/tijd_UTC", each #datetimezone(1970,1,1,0,0,0,0,0) + #duration(0,0,0,Number.From([Verwachte_Start])), type datetimezone),
            AddedLocalDate = Table.AddColumn(AddedUTCDateTime, "Verwachte_startdatum_lokaal", each Date.From([#"Verwachte_startdatum/tijd_UTC"]), type date)
        in
            AddedLocalDate
  • Hi All,

    I'm stuck at creating a custom date column.
    So the thing is i have a date column which is in text datatype as following 09-01-24 (which should be date as in 9 January 2024) and when converting into date it gives error on some dates.

    How can i fix this in pq?