cancel
Showing results for 
Search instead for 
Did you mean: 
Reply
Firewtc343
Regular Visitor

Data text to time convet

I have a query that returns a column PTTTIM as text. it displays 324. I want to convert this to a 24 hour format time 00:03:24 and so on based on the text displayed.  

 

I have tried =Time.FromText but it just errors out.

 

Any help would be appreciated.  

 

Firewtc343_0-1614197173190.png

 

1 ACCEPTED SOLUTION
v-alq-msft
Community Support
Community Support

Hi, @Firewtc343 

 

As is suggested by @primolee , the format of the text can not be parsed correctly by 'Time.FromText'. I'd like to suggest you manually parse the text. I created data to reprodce your scenario. The pbix file is attached in the end.

Table:

 c1.png

 

You may add two new steps with the following steps.

 

= Table.TransformColumns(#"Changed Type",{"PTTTIM",each 
let 
txt = _,
l = Text.Length(_),
res = 
if l=1 then "00:00:0"&txt
else if l=2 then "00:00:"&txt
else if l=3 then "00:0"&Text.Start(txt,1)&":"&Text.End(txt,2)
else if l=4 then "00:"&Text.Start(txt,2)&":"&Text.End(txt,2)
else if l=5 then "0"&Text.Start(txt,1)&":"&Text.Middle(txt,1,2)&":"&Text.End(txt,2)
else if l=6 then Text.Start(txt,2)&":"&Text.Middle(txt,2,2)&":"&Text.End(txt,2)
else null 
in 
res
}
),
= Table.TransformColumnTypes(Custom1,{{"PTTTIM", type time}})

 

 

Result:

c2.png

 

Best Regards

Allan

 

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

View solution in original post

3 REPLIES 3
v-alq-msft
Community Support
Community Support

Hi, @Firewtc343 

 

As is suggested by @primolee , the format of the text can not be parsed correctly by 'Time.FromText'. I'd like to suggest you manually parse the text. I created data to reprodce your scenario. The pbix file is attached in the end.

Table:

 c1.png

 

You may add two new steps with the following steps.

 

= Table.TransformColumns(#"Changed Type",{"PTTTIM",each 
let 
txt = _,
l = Text.Length(_),
res = 
if l=1 then "00:00:0"&txt
else if l=2 then "00:00:"&txt
else if l=3 then "00:0"&Text.Start(txt,1)&":"&Text.End(txt,2)
else if l=4 then "00:"&Text.Start(txt,2)&":"&Text.End(txt,2)
else if l=5 then "0"&Text.Start(txt,1)&":"&Text.Middle(txt,1,2)&":"&Text.End(txt,2)
else if l=6 then Text.Start(txt,2)&":"&Text.Middle(txt,2,2)&":"&Text.End(txt,2)
else null 
in 
res
}
),
= Table.TransformColumnTypes(Custom1,{{"PTTTIM", type time}})

 

 

Result:

c2.png

 

Best Regards

Allan

 

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

View solution in original post

Thank you very much. 

primolee
Resolver I
Resolver I

Hello @Firewtc343 

 

By default, Time.FromText will split the first 2 digits into hour, second 2 digits to minute and third 2 digits to seconds.  In other words, 324 will become 32:04 which would be an error because hour can only be from 0 to 23.

 

You either have to import data using "0324", or manually add steps to split PTTTIM column and restructure them.

 

Best regards,

David

Helpful resources

Announcements
PBI User Groups

Welcome to the User Group Public Preview

Check out new user group experience and if you are a leader please create your group

April Update

Check it Out!

Click here to read more about the April 2021 Updates!

MBAS Carousel

Sign up for our May 4th event!

May the fourth be with you, join us online!

secondImage

The largest Power BI virtual conference

100+ sessions, 100+ speakers, Product managers, MVPs, and experts. All about Power BI. Attend online or watch the recordings.

Top Solution Authors