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

Learn from the best! Meet the four finalists headed to the FINALS of the Power BI Dataviz World Championships! Register now

Reply
Tapemeasure
Regular Visitor

Date Difference between Rows in a Measure

Hi,

 

I'm trying to work out how to use a measure to caclualte the datedifference between rows

 

I've got an dataset that looks like (Date formatting is DD/MM/YYYY)

 

IDStartDate
113/08/2017
213/08/2017
312/08/2017
424/08/2017

 

So far, I've added the following Measure using RANKX 

 

RankMeasure = RANKX(
ALLSELECTED(TABLE),
CALCULATE(
FIRSTDATE(TABLE[StartDate])),,ASC,Dense)

 

When i add this to a table visualisation i get the follow

 

IDStartDateRankMeasure
113/08/2017     2
213/08/2017     2
312/08/2017     1
424/08/2017     3

 

So far so good, Now i'm trying to calculate the DateDifference (in days) between the rows so the result should look like

 

IDStartDateRankMeasureDateDifference (days)
113/08/2017     21
213/08/2017     21
312/08/2017     10
424/08/2017     311

 

Any help would be great.

 

Thanks

1 ACCEPTED SOLUTION
Tapemeasure
Regular Visitor

Hey everyone, thanks for your help, 

 

I managed to do it using the following measure (turns out, I don't need the rank column)

 

DaysBetweenFaults = 

var currentDate = SELECTEDVALUE(Table[StartDate])

var previousDate = CALCULATE(

Max(Table[StartDate]),

ALLSELECTED(Table),

Table[StartDate] < currentDate)

return

if(not ISBLANK(DATEDIFF(previousDate,currentDate,DAY)),DATEDIFF(previousDate,currentDate,DAY),0)

View solution in original post

3 REPLIES 3
Tapemeasure
Regular Visitor

Hey everyone, thanks for your help, 

 

I managed to do it using the following measure (turns out, I don't need the rank column)

 

DaysBetweenFaults = 

var currentDate = SELECTEDVALUE(Table[StartDate])

var previousDate = CALCULATE(

Max(Table[StartDate]),

ALLSELECTED(Table),

Table[StartDate] < currentDate)

return

if(not ISBLANK(DATEDIFF(previousDate,currentDate,DAY)),DATEDIFF(previousDate,currentDate,DAY),0)
Anonymous
Not applicable

Hi @Tapemeasure 

I create a Calculated Column to rank StartDate:

Rank = RANKX('Table','Table'[StartDate],,ASC,Dense)

And I build a Day column:

Day = DAY('Table'[StartDate])

Then I use a measure to achieve your goal:

DateDifference (days) =

IF(MAX('Table'[Rank])=1,

0,

MAX('Table'[Day])-CALCULATE(VALUES('Table'[Day]),FILTER(ALL('Table'),'Table'[Rank]=MAX('Table'[Rank])-1)))

Result:

1.png

You can download the pbix file form this link:

https://qiuyunus-my.sharepoint.com/:u:/g/personal/tongzhou_qiuyunus_onmicrosoft_com/EbLGEcfcu9tEsC0r...

 

Best Regards,

Rico Zhou

 

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

amitchandak
Super User
Super User

@Tapemeasure , If IS/Rank are continous

new column = datediff(Table[Date], maxx(filter(Table,Table[ID] =earlier(Table[ID])-1) ,Table[Date]),day)

// non-continous 
new column 2 =
var _ min = maxx(filter(Table,Table[ID] < earlier(Table[ID])-1) ,Table[ID])
return
datediff(Table[Date], minx(filter(Table,Table[ID] =_min) ,Table[Date]),day)

 

Both Measure and column methods is disuccsed here too

https://medium.com/@amitchandak.1978/power-bi-day-intelligence-questions-time-intelligence-5-5-5c324...

Share with Power BI Enthusiasts: Full Power BI Video (20 Hours) YouTube
Microsoft Fabric Series 60+ Videos YouTube
Microsoft Fabric Hindi End to End YouTube

Helpful resources

Announcements
New to Fabric survey Carousel

New to Fabric Survey

If you have recently started exploring Fabric, we'd love to hear how it's going. Your feedback can help with product improvements.

Power BI DataViz World Championships carousel

Power BI DataViz World Championships - June 2026

A new Power BI DataViz World Championship is coming this June! Don't miss out on submitting your entry.

Join our Fabric User Panel

Join our Fabric User Panel

Share feedback directly with Fabric product managers, participate in targeted research studies and influence the Fabric roadmap.

March Power BI Update Carousel

Power BI Community Update - March 2026

Check out the March 2026 Power BI update to learn about new features.