Forum Discussion

Anonymous's avatar
Anonymous
Not applicable
6 years ago
Solved

Convert text to dates in a column with mixed date types

Hi all,

 

I'm trying to do time studies using an excel source file with a monthdaytime column populated with data coming from multiple countries.  In the date column, half of the dates are written as monthday, the other half are written as daymonth.  PowerBI recognizes the monthday values as dates but classifies the daymonth values as text. 

 

How can I convert the daymonth text values into date values also?  

 

The daymonth values convert to "Error" when I try data type transform in Power Query Editor.  

 

I'm next going to try the PowerQuery DateTimeFromText function on the monthdaytime column but I don't know how this will affect the monthday values already being recognized as dates in the same column.

 

Any help appreciated!

  • Spoiler
     

    Is this what you try to achieve? You can use the custom column option and create the rules needed to do the transformation displayed in the tables. Hope this helps. 

  • Hi Anonymous ,

     

    Do you have another column identify the date column type? For example the date 2/1, If can be first day of february or the second day of january if you do not have any other column can identify the format.

     

    If you do have one, we can create a custom column depends on it easily in Power Query Editor.

     

     

    if [Type] = "MMDD" then Date.FromText([Date]) else Date.FromText([Date],"fr-SN")

     

     

     


    BTW, pbix as attached.

     

    Best regards,

    Community Support Team _ Dong Li
    If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

12 Replies

  • natabird3's avatar
    natabird3
    Icon for Skilled Sharer rankSkilled Sharer

    if you can already easily identify which half is month year and which half is year month you can separate the columns and align them properly. Meaning if you load to power bi one column is only month year and the other is only year month you can use the conversions within Power bI like in edit queries in transform tab, you can split column by number of characters and reorder the columns to match the one that works fine and then merge them again. Then combine with the other column as it will be blank for the first half and blank for the second half once merged will be one column with date. Hope this helps. 

    • Anonymous's avatar
      Anonymous
      Not applicable

      Thanks very much for your quick reply.  I neglected to mention the source file is live, meaning new data is added to it daily.  The solution you recommended would work for a static file but I'm hoping there's a solution that auto converts the new data coming in into a date format.   

      • natabird3's avatar
        natabird3
        Icon for Skilled Sharer rankSkilled Sharer

        Because it was saying using an excel source file so i assumed is an excel file. Then you will need to write a calculated column with if statements.

  • natabird3's avatar
    natabird3
    Icon for Skilled Sharer rankSkilled Sharer
    Spoiler
     

    Is this what you try to achieve? You can use the custom column option and create the rules needed to do the transformation displayed in the tables. Hope this helps. 

    • Anonymous's avatar
      Anonymous
      Not applicable

      Hi, thanks for your suggestion and apologize for the delayed response.

      If I understand your screen shot correctly, I would create an if-then statement for each day, i.e. up to 365 if-thens?

      • natabird3's avatar
        natabird3
        Icon for Skilled Sharer rankSkilled Sharer

        its definitely not a pretty way but is a quick way to get what you need. Maybe there is a way to add an if statement so that if number is between 1-31 and 1-12 or something like that but not sure to be honest as the way the data comes in power bi is usually in a different format and so having it to flip just the day and month is more difficult then if you had the year I would say.

  • v-lid-msft's avatar
    v-lid-msft
    Icon for Community Support rankCommunity Support

    Hi Anonymous ,


    How about the result after you follow the suggestions mentioned in my original post?Could you please provide more details about it If it doesn't meet your requirement?

     

    Best regards,

    Community Support Team _ Dong Li
    If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

  • Anonymous's avatar
    Anonymous
    Not applicable

    Hello, 

    I do have and download from SAP which i cant change and have mixed date formats see the example please.

    any idea how to convert it to dates 

     

    date
    M 11.2020
    M 02.2021
    D 05.02.2021
    D 19.02.2021
    W 14.2021
    D 09.04.2021
    W 15.2021
    W 16.2021

    thanks