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

Register now to learn Fabric in free live sessions led by the best Microsoft experts. From Apr 16 to May 9, in English and Spanish.

Reply
gutovanzin
Frequent Visitor

Get current time in UNIX timestamp

Hello!

 

I created a blank Query that returns the current date and time in ISO format.

= DateTimeZone.SwitchZone(DateTimeZone.LocalNow(),-3,0)
Returns: 2021-01-13T16:03:28.8506372-03:00

 

Is it possible to get the same result however on UNIX Timestamp? Like:

1610519337000

 

Best, 
Luis

 

1 ACCEPTED SOLUTION
edhans
Super User
Super User

Try this @gutovanzin 

 

 

 = Duration.TotalSeconds(DateTime.LocalNow() - #datetime(1970,1,1,0,0,0))

 

 

It simply gives you the total seconds from Jan 1, 1970 at midnight through now. Replace my DateTime.LocalNow() with your DateTimeZone.SwitchZone() function and it will work just fine.

 

edhans_0-1610568179212.png

You can round that with Number.Round if you don't need all of the decimals.

 



Did I answer your question? Mark my post as a solution!
Did my answers help arrive at a solution? Give it a kudos by clicking the Thumbs Up!

DAX is for Analysis. Power Query is for Data Modeling


Proud to be a Super User!

MCSA: BI Reporting

View solution in original post

6 REPLIES 6
ruconard
Employee
Employee

If you are looking for a formula to the the current UTC Epoch Time, try this:

= Number.Round(Duration.TotalSeconds(DateTimeZone.UtcNow() - DateTimeZone.FromText("1970-01-01T00:00:00Z")))

 

If you need the value in millisecond UTC Epoch Time, use this:

 

= Number.Round(Duration.TotalSeconds(DateTimeZone.UtcNow() - DateTimeZone.FromText("1970-01-01T00:00:00Z")) * 1000)

 

 

edhans
Super User
Super User

@gutovanzin - I have written a more complete article on this at my blog site. It won't go live until 9am Pacific on Friday, Jan 15 though. But you can see it then. In the mean time, if you have any questions let me know. Otherwise if you could give thumbs up to any posts that have helped and mark one or more as the solution so this thread can be known to be solved it would be appreciated.



Did I answer your question? Mark my post as a solution!
Did my answers help arrive at a solution? Give it a kudos by clicking the Thumbs Up!

DAX is for Analysis. Power Query is for Data Modeling


Proud to be a Super User!

MCSA: BI Reporting
edhans
Super User
Super User

Try this @gutovanzin 

 

 

 = Duration.TotalSeconds(DateTime.LocalNow() - #datetime(1970,1,1,0,0,0))

 

 

It simply gives you the total seconds from Jan 1, 1970 at midnight through now. Replace my DateTime.LocalNow() with your DateTimeZone.SwitchZone() function and it will work just fine.

 

edhans_0-1610568179212.png

You can round that with Number.Round if you don't need all of the decimals.

 



Did I answer your question? Mark my post as a solution!
Did my answers help arrive at a solution? Give it a kudos by clicking the Thumbs Up!

DAX is for Analysis. Power Query is for Data Modeling


Proud to be a Super User!

MCSA: BI Reporting

Hello!
Thanks for the help, i'm trying to get my timezone, but got this error:

 

= Duration.TotalSeconds(DateTimeZone.SwitchZone(DateTime.LocalNow(),-3) - #datetime(1970,1,1,0,0,0))

 

Expression.Error: We cannot convert the value #datetime(2021, 1, 14, 8, 40, 13.0448086) to type DateTimeZone.
Details:
Value=14/01/2021 08:40:13
Type=[Type]

 

Any suggestion?
Thanks!

You cannot use DateTime.LocalNow in a timezone function. It doesn't have any timezone data. You need to use DateTimeZone.FixedLocalNow or DateTimeZone.LocalNow.

 

However, I am not certian you should use any of those anyway. Unix Epoch time is always UTC +0, and when you load this report in the service, DateTime.LocalNow() as I used above also calculate as UTC +0, so it will be apples to apples. Once you get it into Unix Epoch time, then add (3600 * n) seconds to your final answer, where N is your timezone offset.



Did I answer your question? Mark my post as a solution!
Did my answers help arrive at a solution? Give it a kudos by clicking the Thumbs Up!

DAX is for Analysis. Power Query is for Data Modeling


Proud to be a Super User!

MCSA: BI Reporting
blopez11
Resident Rockstar
Resident Rockstar

Helpful resources

Announcements
Microsoft Fabric Learn Together

Microsoft Fabric Learn Together

Covering the world! 9:00-10:30 AM Sydney, 4:00-5:30 PM CET (Paris/Berlin), 7:00-8:30 PM Mexico City

PBI_APRIL_CAROUSEL1

Power BI Monthly Update - April 2024

Check out the April 2024 Power BI update to learn about new features.

April Fabric Community Update

Fabric Community Update - April 2024

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

Top Solution Authors
Top Kudoed Authors