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

Calling all Data Engineers! Fabric Data Engineer (Exam DP-700) live sessions are back! Starting October 16th. Sign up.

Reply
Rana__Fahad
New Member

No of Days between 2 columns - Direct query mode

Dears, 

 

I am trying to create a new column based on 2 tables on my database (Number of days between 2 dates), with Direct query mode. I used the following Dax: 

colname = DATEDIFF(
FIRSTDATE(table1[col]),
LASTDATE(table2[col]),
DAY). but I got the following error : Function 'FIRSTDATE' is not allowed as part of calculated column DAX expressions on DirectQuery models.
 
Thank you
1 ACCEPTED SOLUTION
Anonymous
Not applicable

Hi @Rana__Fahad ,

When using DirectQuery, certain DAX functions are not supported because they cannot be converted into SQL syntax.
Try use native query in desktop or in datasource,like:

SELECT 
    table1.col1,
    table2.col2,
    DATEDIFF(day, table1.col1, table2.col2) AS colname
FROM 
    table1 
INNER JOIN 
    table2 
ON 
    table1.id = table2.id

Import data from a database using native database query - Power Query | Microsoft Learn

Best Regards,
Gao

Community Support Team

 

If there is any post helps, then please consider Accept it as the solution  to help the other members find it more quickly.
If I misunderstand your needs or you still have problems on it, please feel free to let us know. Thanks a lot!

How to get your questions answered quickly --  How to provide sample data in the Power BI Forum

View solution in original post

8 REPLIES 8
DOLEARY85
Resident Rockstar
Resident Rockstar

Hi,

 

you could try Min and Max instead of first/last

 

Measure =
CALCULATE(
    DATEDIFF(
        MIN(Table2[col]),
        MAX(Table1[col]),
        DAY
    ),
    ALL(Table1),
    ALL(Table2)
)

If I answered your question, please mark my post as solution, Appreciate your Kudos 👍

Dear DOLEARY85Thank you for you time & effort , I want to create a column not a measure. I have many records that I need the number of days for each record. 

It should still work as a column, are you getting an issue when you try it?

 

If I answered your question, please mark my post as solution, Appreciate your Kudos 👍

 

I appricate your fast response , yes I am getting the same result for all records. 

Ah okay, 

 

let's try removing the all statement and calculating min/max individually,

 

Column =
    DATEDIFF(
        CALCULATE(MIN(Table2[Column2])),
        CALCULATE(MAX(Table1[Dat1])),
        DAY
    )
 
If I answered your question, please mark my post as solution, Appreciate your Kudos 👍

Okay, i think you're going to have the same issue with most functions that are going to require row context.

 

A measure with the same calculation should work and provide a number for each row as you're using calculate to change the context. 

 

If I answered your question, please mark my post as solution, Appreciate your Kudos 👍

I got the following error : Function 'MIN' is not allowed as part of calculated column DAX expressions on DirectQuery models.

Anonymous
Not applicable

Hi @Rana__Fahad ,

When using DirectQuery, certain DAX functions are not supported because they cannot be converted into SQL syntax.
Try use native query in desktop or in datasource,like:

SELECT 
    table1.col1,
    table2.col2,
    DATEDIFF(day, table1.col1, table2.col2) AS colname
FROM 
    table1 
INNER JOIN 
    table2 
ON 
    table1.id = table2.id

Import data from a database using native database query - Power Query | Microsoft Learn

Best Regards,
Gao

Community Support Team

 

If there is any post helps, then please consider Accept it as the solution  to help the other members find it more quickly.
If I misunderstand your needs or you still have problems on it, please feel free to let us know. Thanks a lot!

How to get your questions answered quickly --  How to provide sample data in the Power BI Forum

Helpful resources

Announcements
FabCon Global Hackathon Carousel

FabCon Global Hackathon

Join the Fabric FabCon Global Hackathon—running virtually through Nov 3. Open to all skill levels. $10,000 in prizes!

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