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

Get Fabric Certified for FREE during Fabric Data Days. Don't miss your chance! Request now

Reply
rhiheu
Helper I
Helper I

Unable to calculate the difference between two dates in a Direct Query model

Hi,

I'm having trouble calculating the difference between two date fields in a Direct Query model, I'm looking for the number of hours between them. I've tried three different calculated column formula options and a measure formula and none flag an error in completing the formula, but then when I try to put any of them into a table to view, it just says 'can't display the visual - 'couldn't load the data for this visual':

rhiheu_0-1671418747956.png

I've also tried two custom column formulas and both say 'This step results in a query that is not supported in DirectQuery mode.

rhiheu_1-1671420564878.png

Unfortunately changing to Import mode is not an option.

These are the different formulas I've tried:

Measure:

  • DurationMeasure = SUM('DB'[end_date_time]) - SUM('DB'[start_date_time])
Calculated column:
  • DurationDateDiff = DATEDIFF('DB'[end_date_time],'DB'[start_date_time],HOUR)
  • DurationDateValue = DATEVALUE('DB'[end_date_time]) - DATEVALUE('DB'[start_date_time])
  • DurationSubtraction = 'DB'[end_date_time] - 'DB'[start_date_time]

Transform data > Add custom column:

  • = Table.AddColumn(DB, "DurationCustomColumnDuration.Hours", each Duration.Hours([end_date_time]-[start_date_time]))
  • = Table.AddColumn(DB, "DurationCustomColumn", each [end_date_time] - [start_date_time])
Any help would be appreciated, thanks.
5 REPLIES 5
DataInsights
Super User
Super User

@rhiheu,

 

What database are you using? I got the measure below to work in a DirectQuery table that uses SQL Server:

 

Duration Hours = ( MAX ( FactTable[EndDateTime] ) - MAX ( FactTable[StartDateTime] ) ) * 24

 





Did I answer your question? Mark my post as a solution!

Proud to be a Super User!




Hi @DataInsights, sorry I forgot to mention that - it's a PostgreSQL database. Thanks for the suggestion, unfortunately

Duration Hours = ( MAX ( FactTable[EndDateTime] ) - MAX ( FactTable[StartDateTime] ) ) * 24

Also doesn't error when creating it, but then says 'Can't display the visual' when I try and put in a table. 

@rhiheu,

 

Have you tried using custom SQL in the PostgreSQL connector?





Did I answer your question? Mark my post as a solution!

Proud to be a Super User!




Hi @DataInsights, no I haven't, how do I use custom SQL in the PostgreSQL connector?

@rhiheu,

 

In Advanced options, enter custom SQL in the "SQL statement" box:

 

DataInsights_0-1673359775123.png

 





Did I answer your question? Mark my post as a solution!

Proud to be a Super User!




Helpful resources

Announcements
November Power BI Update Carousel

Power BI Monthly Update - November 2025

Check out the November 2025 Power BI update to learn about new features.

Fabric Data Days Carousel

Fabric Data Days

Advance your Data & AI career with 50 days of live learning, contests, hands-on challenges, study groups & certifications and more!

FabCon Atlanta 2026 carousel

FabCon Atlanta 2026

Join us at FabCon Atlanta, March 16-20, for the ultimate Fabric, Power BI, AI and SQL community-led event. Save $200 with code FABCOMM.

Top Solution Authors