Forum Discussion

smjzahid's avatar
smjzahid
Helper V
4 years ago
Solved

How to add TIME values in DAX or Power Query

I have a colum of type TIME datatype and I want to sum the values in this column to find out TOTAL HOURS or TOTAL MINUTES WORKED.   Doing this in excel is really straight forward, but I cannot do t...
  • v-kelly-msft's avatar
    4 years ago

    Hi  smjzahid ,

     

    Using below M codes to get the total hours and minutes:

    let
        Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WMrIyNLAyMFWK1YlWMrAyArINoGwDA6CUUmwsAA==", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [#"Total Time" = _t]),
        #"Changed Type with Locale" = Table.TransformColumnTypes(Source, {{"Total Time", type time}}, "zh-CN"),
        #"Added Custom" = Table.AddColumn(#"Changed Type with Locale", "hours", each Time.Hour([Total Time])+Time.Minute([Total Time])/60+Time.Second([Total Time])/3600),
        #"Added Custom1" = Table.AddColumn(#"Added Custom", "Total hours", each List.Sum(#"Added Custom"[hours])),
        #"Extracted Text Range" = Table.TransformColumns(#"Added Custom1", {{"Total hours", each Text.Middle(Text.From(_, "en-US"), 0, 5), type text}}),
        #"Changed Type" = Table.TransformColumnTypes(#"Extracted Text Range",{{"Total hours", type number}}),
        #"Added Custom2" = Table.AddColumn(#"Changed Type", "Minutes", each Time.Hour([Total Time])*60+Time.Minute([Total Time])+Time.Second([Total Time])/60),
        #"Added Custom3" = Table.AddColumn(#"Added Custom2", "Total Minutes", each List.Sum(#"Added Custom2"[Minutes])),
        #"Removed Columns" = Table.RemoveColumns(#"Added Custom3",{"Minutes", "hours"})
    in
        #"Removed Columns"

    And you will see:

     

    Or you could create 4 columns using dax expressions to get the result.

    For details,pls see attached.

     

    Best Regards,
    Kelly

    Did I answer your question? Mark my reply as a solution!