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

Join us for an expert-led overview of the tools and concepts you'll need to become a Certified Power BI Data Analyst and pass exam PL-300. Register now.

Reply
ricgin
Regular Visitor

Days between two dates columns in DirectQuery

 Hello community, 

 

I tried, unsuccesfuly, to subtract two date in DirectQuery but impossible.. 

How can I do this?

 

 

 

11 REPLIES 11
fpk81
New Member

Hi, so I found a way to make it work: First I change the type of the respective date column (e.g. [birthday]) from Date (or Date Time) to Whole number. Then create a new column with the formula DateTimeLocalNow(); I called it "NOW". After this change this column-type to a whole number as well (it needs to be an extra step). Then add a new colum eg "AGE" with a simple subtraction: "[NOW] - [birthday]". It will work with direct query and it will work, when opening visuals in powerbi online. Hope it helps! Please mark it as solution, if it works for you as well, THX!

NRB_UK
New Member

I have been able to get this working by using the DATEVALUE DAX command.  This seems to reconfirm that these are Date fields.

Please check the results you get as the first time I tried this the results worked on 90% of the calculations.  Had to refresh to ensure all values were correct.

 

Example :

 

DATEVALUE(Table_Name[Date_Field_One]) - DATEVALUE(Table_Name[Date_Field_Two])

 

The returning field can then be formatted as required.

 

 

Mi2n
Microsoft Employee
Microsoft Employee

You might have to enable "Allow unrestricted measures in DirectQuery mode" from File -> Options and Settings -> Options -> DirectQuery, in order to use the DATEDIFF() function in order to subtract two dates.

 

But, beware, this will not restrict any potentially costly queries if you try to use them.

ricgin
Regular Visitor

Thanks, but I have an error message.. DATEDIFF() is not supported in DirectQuery mode.. 

Maybe avaible in the next release?

pqian
Microsoft Employee
Microsoft Employee

@ricgin, in the query editor, you should be able to substract two date columns by simply using the "-" operator. There's a ribbon operation for this: select the two columns, go to Add Column -> Time -> Substract.

 

The resultant column is of type Duration. When loaded into AS this will get converted to a bigint, which is accurate to 0.1ms. Then you can use simple math to extract the portion of this field. For example, to get to the total number of days:

Column = [TimeDifference] / 1E7 / 60 / 60 / 24

 

 

Anonymous
Not applicable

I get the same result via this method. 

  ! This step results in a query that is not supported in DirectQuery mode.

Thanks, but doesn't work in DirectQuery mode..

pqian
Microsoft Employee
Microsoft Employee

@ricgin Given that the operation folds to SQL, it should work just fine in DQ mode.

 

What errors are you getting if you try this? What's the formula in the formula after the substraction?

when I create a new column (in the query editor) to substract 2 columns, PowerBI Desktop said that : if I want to apply the changes I need to switch to import mode

Anonymous
Not applicable

Me too! 

pqian
Microsoft Employee
Microsoft Employee

It's actually quite interesting to note the way PowerQuery does this substraction...

 

select ...,
convert(bigint, ( select convert(bigint, convert(bigint, datediff("ns", dateadd("ms", datediff("ms", dateadd("d", datediff("d", convert(datetime2, '0001-01-01 00:00:00'), [_].[StartDate]), convert(datetime2, '0001-01-01 00:00:00')), [_].[StartDate]), dateadd("d", datediff("d", convert(datetime2, '0001-01-01 00:00:00'), [_].[StartDate]), convert(datetime2, '0001-01-01 00:00:00'))), [_].[StartDate])) / 100) + (convert(bigint, datediff("ms", dateadd("d", datediff("d", convert(datetime2, '0001-01-01 00:00:00'), [_].[StartDate]), convert(datetime2, '0001-01-01 00:00:00')), [_].[StartDate])) * 10000 + convert(bigint, datediff("d", convert(datetime2, '0001-01-01 00:00:00'), [_].[StartDate])) * 864000000000) as [$Item] ) - ( select convert(bigint, convert(bigint, datediff("ns", dateadd("ms", datediff("ms", dateadd("d", datediff("d", convert(datetime2, '0001-01-01 00:00:00'), [_].[EndDate]), convert(datetime2, '0001-01-01 00:00:00')), [_].[EndDate]), dateadd("d", datediff("d", convert(datetime2, '0001-01-01 00:00:00'), [_].[EndDate]), convert(datetime2, '0001-01-01 00:00:00'))), [_].[EndDate])) / 100) + (convert(bigint, datediff("ms", dateadd("d", datediff("d", convert(datetime2, '0001-01-01 00:00:00'), [_].[EndDate]), convert(datetime2, '0001-01-01 00:00:00')), [_].[EndDate])) * 10000 + convert(bigint, datediff("d", convert(datetime2, '0001-01-01 00:00:00'), [_].[EndDate])) * 864000000000) as [$Item] )) as [TimeDifference]

Helpful resources

Announcements
Join our Fabric User Panel

Join our Fabric User Panel

This is your chance to engage directly with the engineering team behind Fabric and Power BI. Share your experiences and shape the future.

June 2025 Power BI Update Carousel

Power BI Monthly Update - June 2025

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

June 2025 community update carousel

Fabric Community Update - June 2025

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