Forum Discussion
power query and time format
- Jan 22, 2020
Another variant, and if more close to the formula you used in Excel
the script is
let Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content], TrimmText = Table.TransformColumns( Table.TransformColumnTypes( Source, {{"A", type text}}, "en-GB" ), {{"A", Text.Trim, type text}} ), CleanText = Table.TransformColumns( Table.TransformColumnTypes( TrimmText, {{"A", type text}}, "en-GB" ), {{"A", Text.Clean, type text}} ), ConvertToTime = Table.ReplaceValue( CleanText, each [A] , each Time.FromText( Text.Start([A],2) & Text.End([A],2) ), Replacer.ReplaceValue,{"A"} ) in ConvertToTimeIf source data is more clean that could be simply = Time.FromText([A])
Another variant, and if more close to the formula you used in Excel
the script is
let
Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],
TrimmText = Table.TransformColumns(
Table.TransformColumnTypes(
Source,
{{"A", type text}}, "en-GB"
),
{{"A", Text.Trim, type text}}
),
CleanText = Table.TransformColumns(
Table.TransformColumnTypes(
TrimmText,
{{"A", type text}}, "en-GB"
),
{{"A", Text.Clean, type text}}
),
ConvertToTime = Table.ReplaceValue(
CleanText,
each [A] ,
each Time.FromText(
Text.Start([A],2) & Text.End([A],2)
),
Replacer.ReplaceValue,{"A"}
)
in
ConvertToTime
If source data is more clean that could be simply = Time.FromText([A])
Thanks!!!
I am learning a lot.
Enclosed please find my version of your suggestions and the source data (normally comes in a .txt file but as this web does not accept txt files i have converted into xls).
I still have problems with 1 digit single AM times. look on the 215 and also with 24:00
Can you help me with these 2 errors?
Thank you again
- SergeiBaklanJan 25, 2020Diamond Contributor
You are welcome.
Sorry, but I didn't find the source file. Query takes data from horario.txt which was not attached.
- juan jimenezJan 25, 2020Iron ContributorI think i attached an excel file called horario.xlsx as txt is not accepted
- SergeiBaklanJan 25, 2020Diamond Contributor
I see, thank you.
Power Query converts 4 characters text into the time, it has no guess what it could be if less characters. I's add each value with zeros if it less than 4 characters:
= Table.ReplaceValue(#"Tipo cambiado1",each [Column1.1], each Text.PadEnd([Column1.1],4,"0"),Replacer.ReplaceText,{"Column1.1"})As for 2400 Power Query doesn't recognise it as correct time, actually that is 00:00 next day. We may consider that as the bug or as feature, but that's what we have. Thus we may replace 2400 on 0000, or on 23:59 to have more or less correct result
= Table.ReplaceValue(#"Replaced Value","2400","2359",Replacer.ReplaceText,{"Column1.1"})Please check in attached file, source file location is to be adjusted.