Forum Discussion
Round up date time column to remove miliseconds
I have date time column in Power BI that stores values as
| 01-Jan-24 1:33:06.7200000 AM |
I want to store value as
| 01-Jan-24 1:33:07 AM |
Delimiting the column on "." wont work as it will round down the values
This query will round secodrs for each column with type datetime. Is this what you want?
(You can remove my ChangedType step if you have already correct types defined)Before
After
let Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("pdE9CsMwDAXgqxTPyUN/thytWUqvEDLkArn/WHfoVA8uFtoefIin40jE6+u6VyGxB1G05QwnT0snUoU7faL9SW00WzqXX4RDDMo0hWhwQa46iWjBJnOXWHAFU+8SC6lwG0KMIdordhzJwY5a5pDSHgTqvvgfJG/f9saQ8w0=", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [#"Date / heure réception information" = _t, #"Date / Heure Premier Accès Acquittement" = _t, Site.Code = _t]), ChangedType = Table.TransformColumns(Source, {{"Date / heure réception information", DateTime.From}, {"Date / Heure Premier Accès Acquittement", DateTime.From}}), // Rouded seconds - only for columns with type datetime Transformed = Table.TransformColumns(ChangedType, List.Transform(Table.SelectRows(Table.Schema(ChangedType), each [Kind] = "datetime")[Name], (x)=> {x, each [ time = Time.From(_), hours = Time.Hour(time), minutes = Time.Minute(time), seconds = Number.Round(Time.Second(time)), result = Date.From(_) & Time.From((hours * 3600 + minutes * 60 + seconds) / 86400) ][result], type datetime})) in Transformed
12 Replies
- AhmedxSuper User
pls try this code
= Time.From(Time.ToText(DateTime.Time([Date]), "hh:mm:ss"))- AnonymousNot applicable
This will display time as 1:33:06 AM and I want rounded up as 1:33:07
- AhmedxSuper User
pls try this
let f = (x)=>[ H = Time.Hour( x), M = Time.Minute( x), S = Int64.From( Time.Second( x ) ), TV = #time( H, M, S), D = Date.From(x)& TV ][D], Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WMjIwMtE1MASiEANDK2NjKwMzPXMjAxBQUIqNBQA=", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Column1 = _t]), #"Changed Type" = Table.TransformColumnTypes(Source,{{"Column1", type datetime}}), Custom1 = Table.TransformColumns( #"Changed Type",{"Column1",f}) in Custom1
- dufoq3Community Champion
Hi Anonymous ,
v1but still stored as 2024-01-01T01:33:06.7200000
v2
stored as you want 01-Jan-24 1:33:07 AM
let Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WMjDU9UrM0zUyUTC0Mja2MjDTMzcyAAEFR1+l2FgA", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Column1 = _t]), AddedCustom = Table.AddColumn(Source, "Custom", each [ a = Text.BetweenDelimiters([Column1], ":", " ", {0, RelativePosition.FromEnd}), b = Text.PadStart(Text.From(Number.Round(Number.From(a, "en-US"))), 2, "0"), c = Text.BeforeDelimiter([Column1], ":", {0, RelativePosition.FromEnd}), d = Text.AfterDelimiter([Column1], " ", {0, RelativePosition.FromEnd}), e = c & ":" & b & " " & d ][e] ) in AddedCustom- AnonymousNot applicable
When I import this data from excel and convert it to text it automatically loses its .xxxxx (ms) part hence none of the computaion work
- AnonymousNot applicableSpoilerlet
Source = Excel.Workbook(File.Contents("C:\Users\kanishka.taneja\OneDrive - Securitas\Data Analytics Hub\Projects\Richemont KPI Dashboard\Files\Extra\Test\Analyse Alarmes.xlsx"), null, true),
#"Security Alarms_Sheet" = Source{[Item="Security Alarms",Kind="Sheet"]}[Data],
#"Promoted Headers" = Table.PromoteHeaders(#"Security Alarms_Sheet", [PromoteAllScalars=true]),
#"Changed Type" = Table.TransformColumnTypes(#"Promoted Headers",{{"Date / heure réception information", type text}, {"Date / Heure Premier Accès Acquittement", type text}}),
#"Renamed Columns" = Table.RenameColumns(#"Changed Type",{{"Date / heure réception information", "Information"}, {"Date / Heure Premier Accès Acquittement", "Action"}}),
#"Added Custom" = Table.AddColumn(#"Renamed Columns", "Information 2", each [ a = Text.BetweenDelimiters([Information], ":", " ", {0, RelativePosition.FromEnd}),
b = Text.PadStart(Text.From(Number.Round(Number.From(a, "en-US"))), 2, "0"),
c = Text.BeforeDelimiter([Information], ":", {0, RelativePosition.FromEnd}),
d = Text.AfterDelimiter([Information], " ", {0, RelativePosition.FromEnd}),
e = c & ":" & b & " " & d
][e]),
#"Changed Type1" = Table.TransformColumnTypes(#"Added Custom",{{"Information 2", type datetime}})
in
#"Changed Type1"Hi dufoq3 , Thanks for sharing your approach.
I tried it but when I convert to text it loses all the decimal values and also when rounding off the number, will it not cause exception to handle lets say 01:01:59.760 AM?
I have shared my advance editor query with table "security alarms" where this approach does not seem to be working. Is there any way I can share some sample data with you?
Sample dataDate / heure réception information Date / Heure Premier Accès Acquittement Site.Code 01-Jan-2024 00:00:15.707 01-Jan-2024 00:00:33.770 0CH0000354 01-Jan-2024 00:01:24.310 01-Jan-2024 00:00:33.770 0CH0000354 01-Jan-2024 00:03:16.583 01-Jan-2024 00:00:33.770 0CH0000354 01-Jan-2024 00:03:36.920 01-Jan-2024 00:00:33.770 0CH0000354 01-Jan-2024 00:04:18.103 01-Jan-2024 00:04:28.740 0CH0000354 01-Jan-2024 00:04:41.237 01-Jan-2024 00:04:28.740 0CH0000354 01-Jan-2024 00:05:17.867 01-Jan-2024 00:04:28.740 0CH0000354 01-Jan-2024 00:06:01.007 01-Jan-2024 00:04:28.740 0CH0000354 01-Jan-2024 00:06:59.770 01-Jan-2024 00:04:28.740 0CH0000354
Also please that the date time format in excel is dd-mmm-yyyy hh:mm:ss I have updated it to dd-mmm-yyyy hh:mm:ss.000 just to share the data with you.- dufoq3Community Champion
This query will round secodrs for each column with type datetime. Is this what you want?
(You can remove my ChangedType step if you have already correct types defined)Before
After
let Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("pdE9CsMwDAXgqxTPyUN/thytWUqvEDLkArn/WHfoVA8uFtoefIin40jE6+u6VyGxB1G05QwnT0snUoU7faL9SW00WzqXX4RDDMo0hWhwQa46iWjBJnOXWHAFU+8SC6lwG0KMIdordhzJwY5a5pDSHgTqvvgfJG/f9saQ8w0=", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [#"Date / heure réception information" = _t, #"Date / Heure Premier Accès Acquittement" = _t, Site.Code = _t]), ChangedType = Table.TransformColumns(Source, {{"Date / heure réception information", DateTime.From}, {"Date / Heure Premier Accès Acquittement", DateTime.From}}), // Rouded seconds - only for columns with type datetime Transformed = Table.TransformColumns(ChangedType, List.Transform(Table.SelectRows(Table.Schema(ChangedType), each [Kind] = "datetime")[Name], (x)=> {x, each [ time = Time.From(_), hours = Time.Hour(time), minutes = Time.Minute(time), seconds = Number.Round(Time.Second(time)), result = Date.From(_) & Time.From((hours * 3600 + minutes * 60 + seconds) / 86400) ][result], type datetime})) in Transformed
- AnonymousNot applicable
Hi,
Thanks for the solution Ahmedx and dufoq3 offered, and i want to offer some more information for user to refer to.
hello Anonymous , you can also refer to the following code in advanced editor in power query.
let Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WMjDU9UrM0zUyUTC0Mja2MjDTMzcyAAEFR1+l2FgA", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Column1 = _t]), #"Added Custom" = Table.AddColumn(Source, "Custom", each let a=Text.Replace(Text.AfterDelimiter([Column1],":",1),"AM",""), b=Text.BeforeDelimiter([Column1],":",1), c=Number.Round(Decimal.From(a)) in DateTime.FromText(b&":"&Text.From(c))) in #"Added Custom"Output
Best Regards!
Yolo Zhu
If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.
- ZhangKunSuper User
let Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WMjDU9UrM0zUyUTC0Mja2MjDTMzcyAAEFR1+lWB0UBYZWppZARIKKAJwqjJBUxAIA", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Column1 = _t]), 已添加自定义 = Table.AddColumn( Source, "Custom_ok", each let a = DateTime.From([Column1]), b = Number.Round(Duration.TotalSeconds(a - #datetime(2000, 1, 1, 0, 0, 0)), 0, 2) in #datetime(2000, 1, 1, 0, 0, 0) + #duration(0, 0, 0, b) ) in 已添加自定义 - AlienSxSuper User
w/o text conversion
let Source = your_table, rounding = Table.AddColumn( Source, "rounding", (x) => [dtr = DateTime.ToRecord(x[datetime_column]), roundup_seconds = #datetime( dtr[Year], dtr[Month], dtr[Day], dtr[Hour], dtr[Minute], 0 ) + #duration(0, 0, 0, Number.RoundUp(dtr[Second], 0))][roundup_seconds], type datetime ) in rounding