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
Anonymous
Not applicable

Converting DateTime into Unix Timestamp integer (calculated column & measure)

I see lots of posts on converting a unix timestamp integer into a datetime (timestamp).  How would one take a DateTime value (2018-08-22 10:11:12) and convert it into a a unix timestamp integer (i.e. number of seconds after Jan 1 1970).

 

I would like to do it in a calculated column (M), but it would be nice to know how to do it in a measure as well (DAX). Thanks.

1 ACCEPTED SOLUTION

Hi @Anonymous

 

You may try to create a column as below.

Column=Duration.TotalSeconds([Column1]-#datetime(1970, 1, 1, 0, 0, 0))

1.png

 

Regards,

Cherie

Community Support Team _ Cherie Chen
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

4 REPLIES 4
v-cherch-msft
Employee
Employee

Hi @Anonymous

 

You may try this measure to convert Date Time into Unix Timestamp integer.

 

Measure =
DATEDIFF ( DATE ( 1970, 1, 1 ), MAX ( Table2[Column1] ), SECOND )

1.png

Regards,

Cherie

Community Support Team _ Cherie Chen
If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.
Anonymous
Not applicable

Thank you.

I did some trial and error with a Calculated Column (M code) and think I got this to work correctly.

 

= Table.AddColumn(#"Changed Type", "TimestampUNIX", each Duration.TotalSeconds([Timestamp] - DateTime.FromText("1970-01-01 00:00:00")))

Someone can tell me if they see a problem with this or have a more efficient method.

Hi @Anonymous

 

You may try to create a column as below.

Column=Duration.TotalSeconds([Column1]-#datetime(1970, 1, 1, 0, 0, 0))

1.png

 

Regards,

Cherie

Community Support Team _ Cherie Chen
If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

hmm I get an error when I use this in M query:

 

Expression.Error: We cannot apply operator - to types Number and DateTime.
Details:
    Operator=-
    Left=1583959530
    Right=1/1/1970 12:00:00

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.