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
TcT85
Helper III
Helper III

Direct Query compatible DAX calculation on time difference

Hi,

 

Need some help with this DAX formula.

 

Im using direct query and I have tried to some dax formula that was not compatible with Direct Query.

I need to calculate the minimum cycle time for each product between the PCBserialnumbers.

 

ProductPcbSerialNumberFinished Date
Vehicle55528222022-03-21 21:43
Vehicle57053912022-03-21 19:56
Vehicle57053922022-03-21 19:58
Vehicle57063102022-03-21 20:00
Vehicle57063112022-03-21 22:00
Vehicle57280952022-03-21 21:41
Vehicle57281102022-03-21 19:55
Vehicle57281112022-03-21 19:59
Textile57281172022-03-21 22:12
Textile67942962022-03-21 19:36
Textile67942972022-03-21 19:35
Textile69753562022-03-21 21:38
Textile69754012022-03-21 21:53
Textile69755602022-03-21 19:47
Textile69755612022-03-21 19:49
Textile69762472022-03-21 21:00
Textile69762482022-03-21 21:03
Textile69763372022-03-21 20:44
Textile69763382022-03-21 20:48
Textile72228772022-03-21 21:21
2 ACCEPTED SOLUTIONS
Icey
Community Support
Community Support

Hi @TcT85 ,

 

Please check if this is what you want:

DAX Date Previous1 =
VAR CurDate_ =
    MAX ( PD_PcbProductionData[Finished Date] )
VAR CurProduct_ =
    MAX ( PD_PcbProductionData[Product] )
RETURN
    CALCULATE (
        MAX ( PD_PcbProductionData[Finished Date] ),
        PD_PcbProductionData[Product] = CurProduct_,
        PD_PcbProductionData[Finished Date] < CurDate_,
        ALLSELECTED ( PD_PcbProductionData )
    )

Icey_0-1650940041945.png

 

EARLIER function is mostly used in the context of calculated columns. It is not supported in this scenario.

 

 

Best Regards,

Icey

 

If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

View solution in original post

Icey
Community Support
Community Support

Hi @TcT85 ,

 

Use MAX() / MIN() function like so:

Measure =
DATEDIFF (
    MAX ( PD_PcbProductionData[Finished Date] ),
    [DAX Date Previous1],
    SECOND
)

Icey_0-1650964376903.png

 

Best Regards,

Icey

 

If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

View solution in original post

7 REPLIES 7
amitchandak
Super User
Super User

@TcT85 , can you explain the calculation?

Share with Power BI Enthusiasts: Full Power BI Video (20 Hours) YouTube
Microsoft Fabric Series 60+ Videos YouTube
Microsoft Fabric Hindi End to End YouTube

Hi,

 

I followed microsoft suggestion to first create this colum

DAX Date Previous1 =
CALCULATE (
MAX ( PD_PcbProductionData[SEL_LOD_LatestStartDateTime] ),
ALLEXCEPT ( PD_PcbProductionData, PD_PcbProductionData[PcbSerialNumber] ),
PD_PcbProductionData[SEL_LOD_LatestStartDateTime] < EARLIER ( 'PD_PcbProductionData'[SEL_LOD_LatestStartDateTime] ))
 
But here it says Calculate is not allowed in a DAX expression for Direct query model.
TcT85_0-1650606155780.png
 
If previous column worked i would try to follow up with this secondary dax formula.
 
Date Diff in Days =
IF (
ISBLANK ( 'View Name'[Date Previous] ),
1,
DATEDIFF (
'View Name'[Date Previous],
'View Name'[Date],
DAY
)
)

But I would replace DAY with Second

 

Not sure if this would work though

 

 

 

Icey
Community Support
Community Support

Hi @TcT85 ,

 

Please check if this is what you want:

DAX Date Previous1 =
VAR CurDate_ =
    MAX ( PD_PcbProductionData[Finished Date] )
VAR CurProduct_ =
    MAX ( PD_PcbProductionData[Product] )
RETURN
    CALCULATE (
        MAX ( PD_PcbProductionData[Finished Date] ),
        PD_PcbProductionData[Product] = CurProduct_,
        PD_PcbProductionData[Finished Date] < CurDate_,
        ALLSELECTED ( PD_PcbProductionData )
    )

Icey_0-1650940041945.png

 

EARLIER function is mostly used in the context of calculated columns. It is not supported in this scenario.

 

 

Best Regards,

Icey

 

If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

Hi Icey,

 

Thanks for the DAX formula.

 

This is the key for me to calculate the cycle time.

 

When i use Datediff to get the cycle time number, it says that function calculate is not allowed for a new column. in direct query model.

 

TcT85_0-1650954538451.png

 

 

Icey
Community Support
Community Support

Hi @TcT85 ,

 

Use MAX() / MIN() function like so:

Measure =
DATEDIFF (
    MAX ( PD_PcbProductionData[Finished Date] ),
    [DAX Date Previous1],
    SECOND
)

Icey_0-1650964376903.png

 

Best Regards,

Icey

 

If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

Yes, it works!!! Thanks Icey!

 

I added ABS to remove the minus value, not sure if this is the best solution.

Dax Measure = 
VAR Datedifferent= DATEDIFF (
    MAX( PD_PcbProductionData[SEL_LOD_LatestEndDateTime] ),
    [DAX Date Previous2],
    SECOND
)
Return
ABS(Datedifferent)

 

One more question.

 

How can i Sum all the measure values?

Dax Total Measure = CALCULATE(PD_PcbProductionData[Dax Measure])

I tried to sum the measure values, but it doesnt calculate correct.

For some reason it only takes the latest value that is 556 at the image below.

TcT85_0-1650973153186.png

 

 

Icey
Community Support
Community Support

Hi @TcT85 ,

 

Try something like this:

DAX Total Measure =
SUMX ( ALLSELECTED ( PD_PcbProductionData[PcbSerialNumber] ), [DAX Measure] )

 

 

Best Regards,

Icey

 

If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

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!

October Power BI Update Carousel

Power BI Monthly Update - October 2025

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

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.