Forum Discussion

Amazing_Random's avatar
3 years ago
Solved

Formatting date text string to a custom format

For a report I have to use a link into the meter name. I have been able to crack down the id aspect of the link. but the date on such link is quite off to the date/time formats in PBI.

&chartEndDate=2023-01-10T19%3A00%3A00.000Z 

This is how the date is formatted in the link

And this is how the date time format is in the column I have:

Relief date (same as chartEndDate in link)
10/01/2023 5:30:00pm.

For the link to work I'd need to have the column be in the same way as the link so this should be in the column instead of the above:

2023-01-10T19%3A00%3A00.000Z 

TIA

  • Do you need something like this? This formula uses the UTCNOW() function to get the current UTC time and the FORMAT function to format the datetime in the desired format. You can replace UTCNOW() function with the column you have.

     

    Column = CONCATENATE(
        FORMAT(UTCNOW(), "yyyy-MM-ddT"),
        SUBSTITUTE(
                FORMAT(UTCNOW(), "HH:mm:ss.fffZ"),
                ":",
                "%3A"
        )
    ) 

     

     

     

17 Replies

  • does that mean you want to convert "2023-01-10T19%3A00%3A00.000Z " to "10/01/2023 5:30:00pm"?

    • Amazing_Random's avatar
      Amazing_Random
      Helper I

      The opposite! I need to convert "10/01/2023 5:30:00pm" to "2023-01-10T19%3A00%3A00.000Z " for the link to work

      Edit: I understand the hours are different, just an example

       

      • ryan_mayu's avatar
        ryan_mayu
        Super User

        could you pls explain the converting logic? 

        the first part before T is easier to understand. How about the part after letter T?

  • bolfri's avatar
    bolfri
    Solution Sage

    Do you need something like this? This formula uses the UTCNOW() function to get the current UTC time and the FORMAT function to format the datetime in the desired format. You can replace UTCNOW() function with the column you have.

     

    Column = CONCATENATE(
        FORMAT(UTCNOW(), "yyyy-MM-ddT"),
        SUBSTITUTE(
                FORMAT(UTCNOW(), "HH:mm:ss.fffZ"),
                ":",
                "%3A"
        )
    ) 

     

     

     

    • Amazing_Random's avatar
      Amazing_Random
      Helper I

      Yours was very close, I had managed to convert the date into the yyyy-mm-ddTHH:mm:ss format so I just needed ti use:

      SUBSTITUTE(
                  FORMAT(Column, "YYYY-mm-ddTHH:mm:ss.fffZ"),
                  ":",
                  "%3A"

       

      Thanks for the help!

  • JB505's avatar
    JB505
    Frequent Visitor
    If you can't use find, here is a variation going off of what ryan_mayu posted. Could probably be a little cleaner. Wasn't sure if midnight would be 00 or 24, but should be an easy change if needed.
     
    Column 2 =
    VAR _day = LEFT('Table'[column], 2)
    VAR _month = MID('Table'[column], 4, 2)
    VAR _year = MID('Table'[column], 7, 4)
    VAR _hourstart = IF(MID('Table'[column], 13, 1) = ":", 13, 14)
    VAR _hour = IF(_hourstart = 13, MID('Table'[column], 12, 1), MID('Table'[column], 12, 2))
    VAR _minute = LEFT(RIGHT('Table'[column], 7), 2)
    VAR _second = LEFT(RIGHT('Table'[column], 4), 2)
    VAR _am_pm = IF(RIGHT('Table'[column], 2) = "pm", "pm", "am")
    VAR _hourconversion =
    IF(_am_pm = "am",
        SWITCH(
            TRUE(),
            _hour IN {"1", "2", "3", "4", "5", "6", "7", "8", "9"}, "0" & _hour,
            _hour = "12", "00",
            _hour
        ),
        SWITCH(
            TRUE(),
            _hour IN {"1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11"}, FORMAT(FORMAT(_hour, 0) + 12, ""),
            _hour
        )
    )

    RETURN
    _year & "-" & _month & "-" & _day & "T" & _hourconversion & "%3A" & _minute & "%3A" & _second & ".000Z"
     

     

    • Amazing_Random's avatar
      Amazing_Random
      Helper I

      Hi! This one is much closer but I'm experiencing issues with some rows. It seems it doesn't work for all the dates I have

      • JB505's avatar
        JB505
        Frequent Visitor

        Amazing_Random 
        Can you include both columns with the original date and the new formatted date?