Forum Discussion

elouizi's avatar
elouizi
Helper II
6 years ago
Solved

Previous duration record

Hi,

 

I am new to Power BI. I want to calculate the previous duration record of a project. I have being trying with Earlier function. But that did not work for me. Please dont use previousYear-fucntion because the dates vary alot. this is just a simple example


Can anyone please help me?

 

Regards,

 

Elouizi

 

  • Hi,

    This calculated column works fine

    =LOOKUPVALUE('Duration'[Duration],'Duration'[Date],CALCULATE(MAX('Duration'[Date]),FILTER('Duration','Duration'[Brand]=EARLIER('Duration'[Brand])&&'Duration'[Date]<EARLIER('Duration'[Date]))),'Duration'[Brand],'Duration'[Brand])

    Hope this helps.

10 Replies

  • mahoneypat's avatar
    mahoneypat
    Microsoft Employee

    Are you trying to create a new column or measure?  Here is a measure expression that should work.  Replace "Table" with your actual table name and try it in a table visual with your Date, Brand, and Duration columns.

     

    LastDuration =
    VAR thisdate =
        MIN ( Table[Date] )
    RETURN
        CALCULATE (
            LASTNONBLANKVALUE ( Table[Date], MAX ( [Table[Duration] ) ),
            ALLEXCEPT ( Table, Table[Brand] ),
            Table[Date] < thisdate
        )
    

     

    If this works for you, please mark it as the solution.  Kudos are appreciated too.  Please let me know if not.

    Regards,

    Pat

    • elouizi's avatar
      elouizi
      Helper II

      Hi,

       

      Thank you so much for you quick response.

       

      I tried the formula. But it showed me the previous dates and not the previous duration.

      I changed the formula a little bit to get the duration out of it. But it did not give me the expected results.

       

      Could you please explain the formula? I'm a newbie that wants to learn new things.

       

      Thanks!

       

       

       

       

  • Greg_Deckler's avatar
    Greg_Deckler
    Community Champion

    elouizi - If you want to create a column, you can use EARLIER, see my article on Mean Time Between Failure (MTBF) which uses EARLIER: http://community.powerbi.com/t5/Community-Blog/Mean-Time-Between-Failure-MTBF-and-Power-BI/ba-p/339586.
    The basic pattern is:
    Column = 
      VAR __Current = [Value]
      VAR __Previous = MAXX(FILTER('Table','Table'[Date] < EARLIER('Table'[Date])),[Value])
    RETURN
      __Current - __Previous

     

    If you want a measure then mahoneypat 's formula should work.

  • Hi,

    This calculated column formula works

    =LOOKUPVALUE(Data[DurationinMinutes],Data[Date],CALCULATE(MAX(Data[Date]),FILTER(Data,Data[Brand]=EARLIER(Data[Brand])&&Data[Date]<EARLIER(Data[Date]))),Data[Brand],Data[Brand])

    Hope this helps.

    • elouizi's avatar
      elouizi
      Helper II

      Looks amazing. Is there a way I can use it as a messaure? Because I need to do other calculations, otherwise I need to create a lot of columns (which will impact the performance)

      • Ashish_Mathur's avatar
        Ashish_Mathur
        Super User

        Whom are you replying to?  Did my calculated column formula work?

    • elouizi's avatar
      elouizi
      Helper II

      Hi Ashish_Mathur,

       

      My replay was to you. :).

      I dont know why, but it says that it cannot find the column "brand" and the "date" columns.
      Is there another way I can solve this without creating a colum? Otherwise I may need to create lots of other columns

       

      Thanks!