Skip to content

Instantly share code, notes, and snippets.

@tonmcg
Created December 21, 2020 19:20
Show Gist options
  • Save tonmcg/5e3c049ce925e18bd5ae416d77ee1f75 to your computer and use it in GitHub Desktop.
Save tonmcg/5e3c049ce925e18bd5ae416d77ee1f75 to your computer and use it in GitHub Desktop.
Power Query M Function to Convert Milliseconds to Date (Datetime)
(milliseconds as any, optional returnDateTime as nullable text) =>
let
result =
if milliseconds <> null then
let
javaScriptBaseDate = #datetime(1970,1,1,0,0,0),
//milliseconds = 1576483200000,
isDateTime = if returnDateTime = "false" or returnDateTime = "" then false else true,
seconds = milliseconds / 1000,
durationInSeconds = #duration(0,0,0,seconds),
dateRecord = DateTime.ToRecord(javaScriptBaseDate + durationInSeconds),
finalDate =
if isDateTime then
#datetime(dateRecord[Year],dateRecord[Month],dateRecord[Day],dateRecord[Hour],dateRecord[Minute],dateRecord[Second])
else
#date(dateRecord[Year],dateRecord[Month],dateRecord[Day])
in
finalDate
else
null
in
result
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment