Skip to main content
cancel
Showing results for 
Search instead for 
Did you mean: 

Register now to learn Fabric in free live sessions led by the best Microsoft experts. From Apr 16 to May 9, in English and Spanish.

Reply
smjzahid
Helper V
Helper V

How to add HH:MM:SS and convert to Minutes only in Power BI

I have a column of data type (duration) that stores value in HH:MM:SS format. I want to convert everything in this column to MINUTES only in Power BI, How do I achieve this in Power Query or DAX, (if possible can we do this in 1 single step using Power Query) if not then can we do this using DAX 

 

Note: The below shown column has more than 1000 rows, (and more data will be added after every reporting period) I have truncated this here for explanation purpose

 

This is what I have 

smjzahid_0-1637513367740.png

 

This is what I want it to show on Power BI dashboard, (Hours and seconds converted to Minutes) and finally show the total sum of this column in MINUTES only (see below)

image.png

1 ACCEPTED SOLUTION

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WUorViVYyMLQyNLIyMABzICImViZmYJFYAA==", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [#"Possession Handed back Time Only" = _t]),
    #"Total Minutes" = Table.AddColumn(Source, "Total Minutes", each Duration.TotalMinutes(Duration.From([Possession Handed back Time Only])))
in
    #"Total Minutes"

Screenshot 2021-11-22 151000.png


Thanks to the great efforts by MS engineers to simplify syntax of DAX! Most beginners are SUCCESSFULLY MISLED to think that they could easily master DAX; but it turns out that the intricacy of the most frequently used RANKX() is still way beyond their comprehension!

DAX is simple, but NOT EASY!

View solution in original post

7 REPLIES 7
v-angzheng-msft
Community Support
Community Support

Hi, @smjzahid 

 

If you want to do it in just one step, then the answer @CNENFRNL  provided will work for you.
If you just want it to show up as one step in Power Query, then you can also combine other more steps in one step to make it look like one step. Then the answer @ronrsnfld  provided would work.

 

In terms of making the steps concise, you could use formula:

code from @CNENFRNL 

#"Total Minutes" = Table.AddColumn(Source, "Total Minutes", each Duration.TotalMinutes(Duration.From([Possession Handed back Time Only])))

or

#"Calculated Total Minutes" = Table.TransformColumns(Table.AddColumn(Source, "TotalMinutes", each Duration.FromText([Duration])),{{"TotalMinutes", Duration.TotalMinutes, type number}})

Result:

vangzhengmsft_0-1637730588902.png

 

If you want to implement it via DAX, then you can create this measure.

_TotalMinutes = 

var _text=MAX('Table'[Duration])
var _len=LEN(_text)

var _p1=FIND(":",_text,1,_len+1)
var _p2=FIND(":",_text,_p1+1,_len+1)

var _hour=VALUE(MID(_text,1,_p1-1))
var _minutes=VALUE(MID(_text,_p1+1,_p2-_p1-1))
return _hour*60+_minutes

Result:

vangzhengmsft_1-1637730661912.png

 

Please refer to the attachment below for details. Hope this helps.

 

 

Best Regards,
Community Support Team _ Zeon Zheng


If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

ronrsnfld
Super User
Super User

Please show the actual M-Code you are using.

It seems to work fine here, using the data you show in your question:

 

ronrsnfld_1-1637590199498.png

 

 

 

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WyivNyVGK1YlWMjC0MjSyMjAAcyAiJlYmZmCRWAA=", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [#"Possession Handed back Time Only" = _t]),
   
//set datatype to "duration"
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"Possession Handed back Time Only", type duration}}),

//remove the empty rows
    #"Filtered Rows" = Table.SelectRows(#"Changed Type", each ([Possession Handed back Time Only] <> null)),

//compute the minutes for each row
    #"Added Custom" = Table.AddColumn(#"Filtered Rows", "Total Minutes Only", each Duration.TotalMinutes([Possession Handed back Time Only]), Int64.Type),
    
//Rename the columns according to your specs
    #"Renamed Columns" = Table.RenameColumns(#"Added Custom",{{"Possession Handed back Time Only", "HH:MM:SS"}})
in
    #"Renamed Columns"

 

ronrsnfld_0-1637590177315.png

 

Then it's just a matter of adding the Totals row to the Table Visual on the Power BI desktop

 

 

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WUorViVYyMLQyNLIyMABzICImViZmYJFYAA==", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [#"Possession Handed back Time Only" = _t]),
    #"Total Minutes" = Table.AddColumn(Source, "Total Minutes", each Duration.TotalMinutes(Duration.From([Possession Handed back Time Only])))
in
    #"Total Minutes"

Thanks to the great efforts by MS engineers to simplify syntax of DAX! Most beginners are SUCCESSFULLY MISLED to think that they could easily master DAX; but it turns out that the intricacy of the most frequently used RANKX() is still way beyond their comprehension!

DAX is simple, but NOT EASY!

I was requesting the code that the OP was using, as your idea was working fine here.  Somehow the connection to his post didn't carry over.

 

CNENFRNL
Community Champion
Community Champion

Duration.TotalMinutes()

Thanks to the great efforts by MS engineers to simplify syntax of DAX! Most beginners are SUCCESSFULLY MISLED to think that they could easily master DAX; but it turns out that the intricacy of the most frequently used RANKX() is still way beyond their comprehension!

DAX is simple, but NOT EASY!

@CNENFRNL 

 

Tried your suggestion. but it throws below error, I have tried convertin the column to duration as well as to TEXT, throws the same error, 

 

any other suggestion please

 

smjzahid_0-1637587176422.png

 

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WUorViVYyMLQyNLIyMABzICImViZmYJFYAA==", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [#"Possession Handed back Time Only" = _t]),
    #"Total Minutes" = Table.AddColumn(Source, "Total Minutes", each Duration.TotalMinutes(Duration.From([Possession Handed back Time Only])))
in
    #"Total Minutes"

Screenshot 2021-11-22 151000.png


Thanks to the great efforts by MS engineers to simplify syntax of DAX! Most beginners are SUCCESSFULLY MISLED to think that they could easily master DAX; but it turns out that the intricacy of the most frequently used RANKX() is still way beyond their comprehension!

DAX is simple, but NOT EASY!

Helpful resources

Announcements
Microsoft Fabric Learn Together

Microsoft Fabric Learn Together

Covering the world! 9:00-10:30 AM Sydney, 4:00-5:30 PM CET (Paris/Berlin), 7:00-8:30 PM Mexico City

PBI_APRIL_CAROUSEL1

Power BI Monthly Update - April 2024

Check out the April 2024 Power BI update to learn about new features.

April Fabric Community Update

Fabric Community Update - April 2024

Find out what's new and trending in the Fabric Community.

Top Solution Authors
Top Kudoed Authors