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

The FabCon + SQLCon recap series starts April 14th at 8am Pacific. If you’re tracking where AI is going inside Fabric, this first session is a can't miss. Register now

Reply
yjk3140
Helper I
Helper I

Calculating difference between two timestamps using DAX

Hi,

I need to create a DAX measure which calculates difference in hours between two timestamp columns in a table.

the date format for those columns is yyyy-mm-ddThh:mm:ss:00.000+0000 ex) 2022-05-03T16:35:00.000+0000.

I tried using DATEDIFF(column1,column2,hour) but it didn't work but threw an error.

 

I was able to get the result I want through the sql query below in databricks using unix_timestamp function

select (unix_timestamp(coumn1)-unix_timestamp(column2))/3600 as hourly_diff from table1.

 

Could anyone help with this query?

 

Thanks for any kind of help in advance!

 

 

 

5 REPLIES 5
Daryl-Lynch-Bzy
Community Champion
Community Champion

Hi @yjk3140 - I spotted this community posing that suggested that Direct Query was not available for datetimestamps with milliseconds.  Solved: Convert UNIX timestamp to DateTime using Direct Qu... - Microsoft Power BI Community.  Is there anyway to change the Databricks table to include the Unix TimeStamp as columns without the need for the addition of the unix_timestamp() function in Power BI query?

Daryl-Lynch-Bzy
Community Champion
Community Champion

Hi @yjk3140  - Should you please share some same data, the DAX measure formula, the error message that the measure threw.  If possible could you create a PBIX file that replicates the error with anonymous data.

Hi @Daryl-Lynch-Bzy thanks for the response.

This is a screenshot of data for these two timestamp columns

yjk3140_0-1661423384192.png

and the DAX query I wrote is 

duration = DATEDIFF(table1[timestamp1],table1[timestamp2],HOUR)
 
I tried to see the created column through the table visual but I'm getting an error saying "we couldn't fold the expression to the data source. Please try a simple expression. Getting the same error when I'm trying to use it in a filter for other visuals.
 
When I run the SQL query 
select (unix_timestamp(timestamp1)-unix_timestamp(timstamp2))/3600 as hourly_diff from table1, 
then I'm getting the result that I exactly want. 
yjk3140_1-1661424135658.png

Is there any solution to get the same result through DAX query in PowerBI?

 

Thank you for your help in advance!

 
 

Hi @yjk3140 are you using Direct Query connection?  and what is the data source?  It appears that Power BI Data Model does not understand how to convert the DATEDIFF function into Direct Query that you data can understand.  Based on the SQL example you provided, you may have to switch to use a UNIX timefunction like this:

Hour Difference = DIVIDE ( Unixtimestamp1 - Unixtimestamp2 , 3600 )

 

Hi @Daryl-Lynch-Bzy , I'm connecting Direct Query to Databricks SQL Endpoint from PowerBI..

How can I get the Unixtimestmp using DAX? As the timestamps I have are not in the format..

 

really appreciate your help

Helpful resources

Announcements
New to Fabric survey Carousel

New to Fabric Survey

If you have recently started exploring Fabric, we'd love to hear how it's going. Your feedback can help with product improvements.

Power BI DataViz World Championships carousel

Power BI DataViz World Championships - June 2026

A new Power BI DataViz World Championship is coming this June! Don't miss out on submitting your entry.

Join our Fabric User Panel

Join our Fabric User Panel

Share feedback directly with Fabric product managers, participate in targeted research studies and influence the Fabric roadmap.

March Power BI Update Carousel

Power BI Community Update - March 2026

Check out the March 2026 Power BI update to learn about new features.