Forum Discussion

Anonymous's avatar
Anonymous
Not applicable
6 years ago
Solved

Calculate Day Diff between 2 dates on row level

Hi there,

 

I hope you can help me!

 

I have an issue: I used the table below as an example

 

I am trying to calculate the day difference based on " Change Date " for each Product based on " Entry NO ". Normally if I had 2 date columns that would be easy, but in this case, I have only 1 date column with all change dates.

 

It's easy to solve this in excel, but how can I solve this in Power BI? it has to be dynamic.

 

Product Entry      NoPhase      Change Date

Bike1Start8/11/2020
Bike2Develop8/12/2020
Bike3Finish8/14/2020
Car1Start8/12/2020
Car2Develop8/15/2020
Motor1Start8/17/2020
Motor2Develop8/20/2020
Motor3Finish8/24/2020
Step1Start8/11/2020
Step2Develop8/20/2020
Step3Finish8/29/2020
  • Anonymous's avatar
    Anonymous
    6 years ago

    HI Anonymous 

     

    Please check the below code. This gives 1 day as per your need.

     

    CHANGE DATE = DATEDIFF([PHASE],MAXX(FILTER(TABLE,[PRODUCT] = EARLIER(PRODUCT) && [ENTRY] < EARLIER([ENTRY])),[PHASE] ),DAY)

     

7 Replies

  • Anonymous what you are expecting #2 - #1, #3 - #2 

    • Anonymous's avatar
      Anonymous
      Not applicable

      What is expect is the difference in days for each product based on entry no and change date.

       

      Example,

       

      Bike has 3 entry no, the difference in days = 

      Change date entry no 2 - Change date entry no 3 = (12-8-2020) - (11-8-2020) = 1 day.

       

      This should be dynamic where Power BI takes into consideration the Product, Entry No before calculating the diff date in day.

      • parry2k's avatar
        parry2k
        Super User

        Anonymous try this measure

         

        New Measure = 
        VAR __entry = MAX ( Table[Entry] ) - 1
        VAR __prevDate = CALCULATE ( MAX ( Table[ChangeDate] ), Table[Entry] = __entry )
        RETURN
        DATEDIFF ( __prevDate, MAX ( Table[ChangeDate] ), DAY )

         

        I would  Kudos if my solution helped. 👉 If you can spend time posting the question, you can also make efforts to give Kudos whoever helped to solve your problem. It is a token of appreciation!

        Visit us at https://perytus.com, your one-stop-shop for Power BI-related projects/training/consultancy.

  • Anonymous , Not very clear

    Try like

    datediff(maxx(filter(table,[Product] = earlier(Product ) && [Entry] < earlier([Entry])),[Change Date] ),[Change Date],day)

    • Anonymous's avatar
      Anonymous
      Not applicable

      What is expect is the difference in days for each product based on entry no and change date.

       

      Example,

       

      Bike has 3 entry no, the difference in days = 

      Change date entry no 2 - Change date entry no 3 = (12-8-2020) - (11-8-2020) = 1 day.

       

      This should be dynamic where Power BI takes into consideration the Product, Entry No before calculating the diff date in days.

  • Anonymous's avatar
    Anonymous
    Not applicable

    HI Anonymous 

     

    Please check the below code. This gives 1 day as per your need.

     

    CHANGE DATE = DATEDIFF([PHASE],MAXX(FILTER(TABLE,[PRODUCT] = EARLIER(PRODUCT) && [ENTRY] < EARLIER([ENTRY])),[PHASE] ),DAY)