Forum Discussion
leorrn
1 year agoRegular Visitor
Calculate the difference between two dates in different rows
Hi, I'd like to calculate the difference between dates, splited in different rows, returing the difference in the format "Days Hours". Do you guys have any idea? The table is already orde...
- 1 year ago
Hi leorrn - Please find the attached pbix you can use DAX to calculate the time difference between consecutive rows within each "Planta" group
Datediff_Days_Hours =VAR PrevDatetime =CALCULATE(MAX('timdiff'[Datetime]),FILTER('timdiff','timdiff'[Planta] = EARLIER('timdiff'[Planta]) &&'timdiff'[Datetime] < EARLIER('timdiff'[Datetime])))
VAR DiffMinutes = DATEDIFF(PrevDatetime, 'timdiff'[Datetime], MINUTE)
VAR Days = QUOTIENT(DiffMinutes, 1440) -- 1440 minutes in a dayVAR Hours = QUOTIENT(MOD(DiffMinutes, 1440), 60) -- Remaining minutes converted to hours
RETURNIF(ISBLANK(PrevDatetime),BLANK(),FORMAT(Days, "0") & " Days " & FORMAT(Hours, "0") & " Hours")
Greg_Deckler
1 year agoCommunity Champion
leorrn 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 __PreviousDate = MAXX(FILTER('Table','Table'[Date] < EARLIER('Table'[Date])),[Date])
VAR __Previous = MAXX(FILTER('Table',[Date]=__PreviousDate),[Value])
RETURN
( __Current - __Previous ) * 1.