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

Enhance your career with this limited time 50% discount on Fabric and Power BI exams. Ends August 31st. Request your voucher.

Reply
lazer1
New Member

Convert text string of number time within Power Query into numerical days

Hi,

 

I need to convert at time format like " 300 days, 3 hours, 05 minutes, 23 seconds"  into "300.128738425926" days.

 

I was able to figure out how to do it in excel with a custom function, but I haven't been able to figure out how to do this simply in power query. I don't want to do it with DAX because I need to append two, one is in the correct format, and one is in the text time format. Changing it from the source is not an option. Does anyone have an idea on how to do this?

 

lazer1_0-1668616418377.png

 

1 ACCEPTED SOLUTION
Daryl-Lynch-Bzy
Resident Rockstar
Resident Rockstar

Hi @lazer1 - Great Question!!  I came up with a couple of ways to achieve this.  First the long way, then the better way.

This long winded approach....  We need to separate the Number from the Text, Split this in List, Convert the Hours/Minutes/Seconds into Total Seconds and then divide by ( 24 * 60 * 60 ) and finally add Days.  I looks something like this:

 

DarylLynchBzy_0-1668633451541.png

 

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WMjYwVUhJrCzWUTBWyMgvLQIyTBVyM/NKS1KBTCNjheLU5Py8lGKl2FgA", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [#"Workflow Duration Avg" = _t]),
    #"Get Number and Columns" = Table.AddColumn(Source, "Number Text", each Text.Select( [Workflow Duration Avg] , { "0".."9" , "," }), type text),
    #"Text to List" = Table.AddColumn(#"Get Number and Columns", "Text Split", each Text.Split( [Number Text] , ","), type list),
    #"Transform List to Numbers" = Table.TransformColumns( #"Text to List", {{"Text Split",
 each List.Transform(_ , each Number.From(_) ) }} ),
    #"Duration Seconds" = Table.AddColumn(#"Transform List to Numbers", "Add Seconds", each Duration.TotalSeconds( #duration( 0 , [Text Split]{1}, [Text Split]{2}, [Text Split]{3} )), type number),
    #"Add Result" = Table.AddColumn(#"Duration Seconds", "Calculation", each [Text Split]{0} + [Add Seconds] / ( 24 * 60 * 60 ), type number)
in
    #"Add Result"

 

I would suggest creating a custom function like the following:

(#"Duration Text" as text) as number =>
let
    #"Get Number and Commas" = Text.Select( #"Duration Text" , { "0".."9" , "," }),
    #"Text to List" = Text.Split( #"Get Number and Commas" , ","),
    #"Transform List to Numbers" = List.Transform( #"Text to List" , each Number.From(_) ),
    #"Duration Seconds" = Duration.TotalSeconds( #duration( 0 , #"Transform List to Numbers"{1}, #"Transform List to Numbers"{2}, #"Transform List to Numbers"{3} )),
    #"Add Result" = #"Transform List to Numbers"{0} + #"Duration Seconds" / ( 24 * 60 * 60 )
in
    #"Add Result"

 
We want to use the Duration.FromText - PowerQuery M | Microsoft Learn and conver this to Number.  To achieve this we convert the text into this format "305.03:05:23", convert this to duration, and then convert to number.

DarylLynchBzy_1-1668634367116.png

 

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WMjYwVUhJrCzWUTBWyMgvLQIyTBVyM/NKS1KBTCNjheLU5Py8lGKl2FgA", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [#"Workflow Duration Avg" = _t]),
    #"Replace Text" = Table.AddColumn(Source, "Add Text", each Text.Replace(
 Text.Replace(
  Text.Replace(
   Text.Replace( [Workflow Duration Avg] , 
    " days, ", "."),
   " hours, ", ":" ),
  " minutes, ", ":"),
 " seconds", "" ), type text),
    #"Convert to Duration" = Table.AddColumn(#"Replace Text", "Add Duration", each Duration.FromText( [Add Text] ), type duration),
    #"Covert to Number" = Table.TransformColumnTypes(#"Convert to Duration",{{"Add Duration", type number}})
in
    #"Covert to Number"

 

 

 

 

View solution in original post

3 REPLIES 3
Daryl-Lynch-Bzy
Resident Rockstar
Resident Rockstar

Hi @lazer1 - Great Question!!  I came up with a couple of ways to achieve this.  First the long way, then the better way.

This long winded approach....  We need to separate the Number from the Text, Split this in List, Convert the Hours/Minutes/Seconds into Total Seconds and then divide by ( 24 * 60 * 60 ) and finally add Days.  I looks something like this:

 

DarylLynchBzy_0-1668633451541.png

 

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WMjYwVUhJrCzWUTBWyMgvLQIyTBVyM/NKS1KBTCNjheLU5Py8lGKl2FgA", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [#"Workflow Duration Avg" = _t]),
    #"Get Number and Columns" = Table.AddColumn(Source, "Number Text", each Text.Select( [Workflow Duration Avg] , { "0".."9" , "," }), type text),
    #"Text to List" = Table.AddColumn(#"Get Number and Columns", "Text Split", each Text.Split( [Number Text] , ","), type list),
    #"Transform List to Numbers" = Table.TransformColumns( #"Text to List", {{"Text Split",
 each List.Transform(_ , each Number.From(_) ) }} ),
    #"Duration Seconds" = Table.AddColumn(#"Transform List to Numbers", "Add Seconds", each Duration.TotalSeconds( #duration( 0 , [Text Split]{1}, [Text Split]{2}, [Text Split]{3} )), type number),
    #"Add Result" = Table.AddColumn(#"Duration Seconds", "Calculation", each [Text Split]{0} + [Add Seconds] / ( 24 * 60 * 60 ), type number)
in
    #"Add Result"

 

I would suggest creating a custom function like the following:

(#"Duration Text" as text) as number =>
let
    #"Get Number and Commas" = Text.Select( #"Duration Text" , { "0".."9" , "," }),
    #"Text to List" = Text.Split( #"Get Number and Commas" , ","),
    #"Transform List to Numbers" = List.Transform( #"Text to List" , each Number.From(_) ),
    #"Duration Seconds" = Duration.TotalSeconds( #duration( 0 , #"Transform List to Numbers"{1}, #"Transform List to Numbers"{2}, #"Transform List to Numbers"{3} )),
    #"Add Result" = #"Transform List to Numbers"{0} + #"Duration Seconds" / ( 24 * 60 * 60 )
in
    #"Add Result"

 
We want to use the Duration.FromText - PowerQuery M | Microsoft Learn and conver this to Number.  To achieve this we convert the text into this format "305.03:05:23", convert this to duration, and then convert to number.

DarylLynchBzy_1-1668634367116.png

 

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WMjYwVUhJrCzWUTBWyMgvLQIyTBVyM/NKS1KBTCNjheLU5Py8lGKl2FgA", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [#"Workflow Duration Avg" = _t]),
    #"Replace Text" = Table.AddColumn(Source, "Add Text", each Text.Replace(
 Text.Replace(
  Text.Replace(
   Text.Replace( [Workflow Duration Avg] , 
    " days, ", "."),
   " hours, ", ":" ),
  " minutes, ", ":"),
 " seconds", "" ), type text),
    #"Convert to Duration" = Table.AddColumn(#"Replace Text", "Add Duration", each Duration.FromText( [Add Text] ), type duration),
    #"Covert to Number" = Table.TransformColumnTypes(#"Convert to Duration",{{"Add Duration", type number}})
in
    #"Covert to Number"

 

 

 

 

AntrikshSharma
Super User
Super User

@lazer1 3 hours, 05 minutes, 23 seconds should be 11123 

=3*60*60
+5*60
+23

 

Yes. Plus the seconds for 300 days. So converting the text string of

300 days, 3 hours, 05 minutes, 23 seconds

into raw seconds is what I am trying to figure out how to do. I can do it with a split column, then all the math, but I have many source columns with this info... so trying to figure out if I could do the calculation you did (plus the day seconds) in an add column, or in power query m

Helpful resources

Announcements
July PBI25 Carousel

Power BI Monthly Update - July 2025

Check out the July 2025 Power BI update to learn about new features.

Join our Fabric User Panel

Join our Fabric User Panel

This is your chance to engage directly with the engineering team behind Fabric and Power BI. Share your experiences and shape the future.

June 2025 community update carousel

Fabric Community Update - June 2025

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

Top Solution Authors