Forum Discussion

Thankyouverymuc's avatar
Thankyouverymuc
New Member
4 years ago
Solved

Get Time from DateTime field

Dear Power BI community, 

 

Can anyone please help me with a DAX formula with extracting the time value from a datetime field.

See for example the below datetime values and I want to extract the timevalue from the datetime field

So for the output, I would like to see 11:17:56 or 3:00:00 in a new column - I want to keep the current column which shows datetime and add a new column with the correct formula. 

Can anyone please help me? 

Thank you guys! 

23 Replies

  • TheoC's avatar
    TheoC
    Community Champion

    Sorry to jump in on this forum but noticed there wasn't a DAX solution on here.  If you're looking for a DAX solution, you can extract the Time from a DateTime column by doing the following:

    1.  Click on new Calculated Column

    2.  Time = FORMAT ( 'TableName'[DateTimeColumnName] , "hh:mm:ss" )

    3. Convert the new Time column from Text to Time.

     

    Hope this helps.

     

    Theo

     

     

    • arothberg's avatar
      arothberg
      Frequent Visitor

      How would I control what timezeone is used when the datetime is formatted? i.e. the datetime is stored in UTC but I want to format it into a fixed timezone (for example US/Eastern).

      • TheoC's avatar
        TheoC
        Community Champion

        Hi arothberg, you need to know the difference in hours between UTC and your target timezone. Once you know that, then all you need to do is create a calculated column:

        AdjustedTime = FORMAT ( 'Table'[TimeColumn] - TIME ( 0 , 0 , 0 ) , "hh:mm:ss" )
         
        The ( 0 , 0 , 0 ) represents Hours , Minutes , Seconds.  So if you want to take 5 hours off your TimeColumn, then just write ( 5 , 0 , 0 ) in the above formula.  Make sure to adjust the - / + according to the timezone you're after.
         
        Hope this helps.
         
        Theo
         
    • Anonymous's avatar
      Anonymous
      Not applicable

      Only thing consistent about Power BI is its inconsistency over the years. Tried the same 3 steps in DAX and am getting 12AM across all records lol. 

  • Syk's avatar
    Syk
    Resident Rockstar

    Right click on column and hit duplicate
    Then click on the new column and go to transform > Time > Time only

     

  • Hi Guys! Thank you both for your quick responses but I can't work through the Power Query because I don't have the rights yet so could you please give me a solution with a DAX formula? In Excel, there are various ways through which I can convert it but somehow I can't find it with DAX.

  • For a pure DAX solution - I came up with the below.  Not sure if it is more or less efficient than making a second copy of the column ... I just don't prefer to do that. 

     

    The dax below would be used to make a Calculated column.  Or - you can use the logic in a measure.

     

    TimeOfDateTime =  TIME(  HOUR(  FactStuff[CreateDateTime]  ),  MINUTE(  FactStuff[CreateDateTime]  ),  SECOND( FactStuff[CreateDateTime]  )  )

  • Manishlmn_89's avatar
    Manishlmn_89
    Frequent Visitor

    Use this measure to convert UTC to PST or anything.. this is dynamic, based on time , date also will change.. Let utc 12 AM, automatically it will give 8 or 9PM before day .

    Current EST_Today =
    VAR UTC_DateTime = now() // Variable to hold the original UTC datetime value from your table.
    VAR Year = YEAR(UTC_DateTime)         // Extracts the year from the UTC datetime to calculate DST boundaries for that specific year.
    VAR DST_Start = DATE(Year, 314 - WEEKDAY(DATE(Year, 38), 2))  // Calculates the start of DST (Second Sunday in March).
    VAR DST_End = DATE(Year, 117 - WEEKDAY(DATE(Year, 111), 2))   // Calculates the end of DST (First Sunday in November).
    VAR IsDST = IF(UTC_DateTime >= DST_Start && UTC_DateTime < DST_End, -4, -5)  // Determines if the datetime falls in DST period; adjusts offset to -4 for DST (EDT), otherwise -5 for standard time (EST).
    VAR LocalDateTime = UTC_DateTime  +IsDST/24
    RETURN FORMAT(LocalDateTime, "M/DD/YYYY HH:MM AM/PM")