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

Join us for an expert-led overview of the tools and concepts you'll need to become a Certified Power BI Data Analyst and pass exam PL-300. Register now.

Reply
johnny_green
New Member

Time Difference in between rows with condition

Hello,

 

I have some raw data csv file that is logging the access time on a kiosk. Is there any formula can do below logic?

 

- Time difference between row. (eg. Row 2 - Row 3 = Result display on Column4)

- If the difference let's say bigger than 12 hours. Output "Null" on Column4

 

Thank you all for the great help.

 

Pic2.png

 

 

 

Johnny

2 ACCEPTED SOLUTIONS
dearwatson
Continued Contributor
Continued Contributor

This can get tricky.

 

To resolve this I normally do a "self join" here's the steps:

 

1 .Merge the 2 columns with a space seperator and change type into a DATETIME column.. also sort it Ascending.. (Oldest at the top) u will need this later.

 

Capture.PNG

2. Create a duplicate of the query... in fact make two... usually I will reference the original query TWICE then disable load on the original query - this means I can go back and change the original easily if I want without breaking stuff..

 

Capture.PNG

 

3. create an index column on the new querys.. the first index must start with 0 and then the second one starts with 1. you now have 2 queries with an index OFFSET by 1... see screenshot:

 

Index starting at 0

Capture.PNG

 

Index starting at 1:

Capture2.PNG

 

 

4. Now select the query with the index that starts with 0 - this is your main query now... you use this to "Merge Queries" to the other one by the index field...

 

Capture.PNG

 

5. Expand the datetime from the merged query and this will give you the yesterdays value for all records except the first one.

 

 

6. Then do the simple equation to get the difference...

Capture2.PNG

 

I'm sure there's a cooler way... I think you can do some fancy DAX with EARLIER etc... to do this too but I prefer this method as it gives me better data lineage... as in I can work out issues more easily.

 

Cheers

Greg

 

 

View solution in original post

v-huizhn-msft
Microsoft Employee
Microsoft Employee

Hi @johnny_green,

Apart from the solution @dearwatson posted, you also can use the following solution.

1. Merge the column2 and column3 in Power Query.  Add a index column in Power Query according to @dearwatson posted.

2. Create a calculated column using the following formula.

Column4 = IF(DATEDIFF(LOOKUPVALUE(Test[Merged],Test[Index],Test[Index]-1),Test[Merged],MINUTE)>720,BLANK(),DATEDIFF(LOOKUPVALUE(Test[Merged],Test[Index],Test[Index]-1),Test[Merged],MINUTE))


Please see the expected result.

2.PNG

Best Regards,
Angelia

View solution in original post

2 REPLIES 2
v-huizhn-msft
Microsoft Employee
Microsoft Employee

Hi @johnny_green,

Apart from the solution @dearwatson posted, you also can use the following solution.

1. Merge the column2 and column3 in Power Query.  Add a index column in Power Query according to @dearwatson posted.

2. Create a calculated column using the following formula.

Column4 = IF(DATEDIFF(LOOKUPVALUE(Test[Merged],Test[Index],Test[Index]-1),Test[Merged],MINUTE)>720,BLANK(),DATEDIFF(LOOKUPVALUE(Test[Merged],Test[Index],Test[Index]-1),Test[Merged],MINUTE))


Please see the expected result.

2.PNG

Best Regards,
Angelia

dearwatson
Continued Contributor
Continued Contributor

This can get tricky.

 

To resolve this I normally do a "self join" here's the steps:

 

1 .Merge the 2 columns with a space seperator and change type into a DATETIME column.. also sort it Ascending.. (Oldest at the top) u will need this later.

 

Capture.PNG

2. Create a duplicate of the query... in fact make two... usually I will reference the original query TWICE then disable load on the original query - this means I can go back and change the original easily if I want without breaking stuff..

 

Capture.PNG

 

3. create an index column on the new querys.. the first index must start with 0 and then the second one starts with 1. you now have 2 queries with an index OFFSET by 1... see screenshot:

 

Index starting at 0

Capture.PNG

 

Index starting at 1:

Capture2.PNG

 

 

4. Now select the query with the index that starts with 0 - this is your main query now... you use this to "Merge Queries" to the other one by the index field...

 

Capture.PNG

 

5. Expand the datetime from the merged query and this will give you the yesterdays value for all records except the first one.

 

 

6. Then do the simple equation to get the difference...

Capture2.PNG

 

I'm sure there's a cooler way... I think you can do some fancy DAX with EARLIER etc... to do this too but I prefer this method as it gives me better data lineage... as in I can work out issues more easily.

 

Cheers

Greg

 

 

Helpful resources

Announcements
Join our Fabric User Panel

Join our Fabric User Panel

This is your chance to engage directly with the engineering team behind Fabric and Power BI. Share your experiences and shape the future.

June 2025 Power BI Update Carousel

Power BI Monthly Update - June 2025

Check out the June 2025 Power BI update to learn about new features.

June 2025 community update carousel

Fabric Community Update - June 2025

Find out what's new and trending in the Fabric community.