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

Data Days is here! Join us now for 60+ days of learning, challenges, and connection. Learn more

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
Fabric Data Days is here Carousel

Fabric Data Days 2026

Don't miss out on Data Days, June 15 through August 7. Learn Fabric, Power BI, SQL, AI and more.

May Power BI Update Carousel

Power BI Monthly Update - May 2026

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

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.