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

Register now to learn Fabric in free live sessions led by the best Microsoft experts. From Apr 16 to May 9, in English and Spanish.

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?

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
Microsoft Fabric Learn Together

Microsoft Fabric Learn Together

Covering the world! 9:00-10:30 AM Sydney, 4:00-5:30 PM CET (Paris/Berlin), 7:00-8:30 PM Mexico City

PBI_APRIL_CAROUSEL1

Power BI Monthly Update - April 2024

Check out the April 2024 Power BI update to learn about new features.

April Fabric Community Update

Fabric Community Update - April 2024

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

Top Solution Authors