Forum Discussion

TcT85's avatar
TcT85
Helper III
4 years ago
Solved

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
  • 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 )
        )
    

     

    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 TcT85 ,

     

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

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

     

    Best Regards,

    Icey

     

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

7 Replies

    • TcT85's avatar
      TcT85
      Helper III

      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.
       
      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's avatar
        Icey
        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 )
            )
        

         

        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.