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

Power BI is turning 10! Let’s celebrate together with dataviz contests, interactive sessions, and giveaways. Register now.

Reply
axk180022
Helper II
Helper II

Calculate Difference between 2 rows based on another column filter

axk180022_1-1630609733555.png

 

I would want to find the difference in the GenHours for each serialid. Currently this is how my table looks on power BI. I have filtered the latest and 2nd latest date for each serialid.

 

Now i would want to find the difference between the GenHours that is being diplayed on Power BI.

 

Can someone please help.

 

1 ACCEPTED SOLUTION

Hi,

This calculated column formula works

=if(CALCULATE(MAX(Data[Dates]),FILTER(Data,Data[serialid]=EARLIER(Data[serialid])))=Data[Dates],ABS(Data[GenHours]-LOOKUPVALUE(Data[GenHours],Data[Dates],CALCULATE(max(Data[Dates]),FILTER(Data,Data[serialid]=EARLIER(Data[serialid])&&Data[Dates]<EARLIER(Data[Dates]))),Data[serialid],Data[serialid])),BLANK())

Hope this helps.

Untitled.png


Regards,
Ashish Mathur
http://www.ashishmathur.com
https://www.linkedin.com/in/excelenthusiasts/

View solution in original post

25 REPLIES 25
Syndicate_Admin
Administrator
Administrator

This sent me to my analysis.

Thank you!

Syndicate_Admin
Administrator
Administrator

This sent me to my analysis.

Thank you!

richbenmintz
Resident Rockstar
Resident Rockstar

Hi @axk180022,

 

Are you able to provide the data as a table as opposed to an image and the expected outcome. Not sure I understand the requirement based on your post.

 

Thanks,



I hope this helps,
Richard

Did I answer your question? Mark my post as a solution! Kudos Appreciated!

Proud to be a Super User!


The one highlighted is the output I require, Which is the difference of GenHours between 2 latest dates. 

 

@richbenmintz 

Hi @axk180022 ,

 

could you provide the data?



I hope this helps,
Richard

Did I answer your question? Mark my post as a solution! Kudos Appreciated!

Proud to be a Super User!


Since Im a new user im unable to upload the above data. is there a link you can provide me?

@richbenmintz 

Hi @axk180022 ,

 

You can paste the data in a table, or share your pbix through google drive or onedrive



I hope this helps,
Richard

Did I answer your question? Mark my post as a solution! Kudos Appreciated!

Proud to be a Super User!


serialidGenHoursDates
cvs-001 30012/1/2019
cvs-001 2001/2/2020
cvs-001 2002/2/2020
cvs-002 8004/3/2020
cvs-002 3005/3/2020
cvs-002 4006/3/2020
cvs-003 2505/5/2020
cvs-004 8205/2/2020
cvs-004 3506/6/2020
cvs-004 1506/7/2020

 

Here is the table @richbenmintz 

Hi @axk180022 

 

If you create a calc column that get the prior date value like

Prior Date Value = 
var _serialid = [serialid]
var _date = [Dates]
return
CALCULATE(MAX('Table'[GenHours]), TOPN(1, FILTER('Table', [serialid] = _serialid && [Dates]<_date), 'Table'[Dates], DESC))

you can create another column that derives the difference

diff = [GenHours] - [Prior Date Value] 

hope this helps

 



I hope this helps,
Richard

Did I answer your question? Mark my post as a solution! Kudos Appreciated!

Proud to be a Super User!


Hi @axk180022,

 

See Formula Below, combined logic into single column and set value to blank when not the max date per serial number

 

Difference = 
var _serialid = [serialid]
var _date = [Dates]
return

if('Table'[Dates] = CALCULATE(MAX('Table'[Dates]), filter(ALL('Table'),'Table'[serialid] = _serialid)),
abs('Table'[GenHours] - 
    CALCULATE(MAX('Table'[GenHours]), 
        TOPN(1, FILTER('Table', [serialid] = _serialid && [Dates]<_date), 'Table'[Dates], DESC))
    )
    , BLANK())

 

richbenmintz_0-1630676003884.png

 



I hope this helps,
Richard

Did I answer your question? Mark my post as a solution! Kudos Appreciated!

Proud to be a Super User!


Thank you so much, this solution worked, but is it possible to make the other values to be left blank?   like below

serialidGenHoursDatesDifference
cvs-00130012/1/2019 
cvs-0012001/2/2020 
cvs-0012002/2/20200
cvs-0028004/3/2020 
cvs-0023005/3/2020 
cvs-0024006/3/2020100
cvs-0032505/5/2020250
cvs-0048205/2/2020 
cvs-0043506/6/2020 
cvs-0041506/7/2020200

 

@richbenmintz 

Hi,

This calculated column formula works

=if(CALCULATE(MAX(Data[Dates]),FILTER(Data,Data[serialid]=EARLIER(Data[serialid])))=Data[Dates],ABS(Data[GenHours]-LOOKUPVALUE(Data[GenHours],Data[Dates],CALCULATE(max(Data[Dates]),FILTER(Data,Data[serialid]=EARLIER(Data[serialid])&&Data[Dates]<EARLIER(Data[Dates]))),Data[serialid],Data[serialid])),BLANK())

Hope this helps.

Untitled.png


Regards,
Ashish Mathur
http://www.ashishmathur.com
https://www.linkedin.com/in/excelenthusiasts/

I am trying to do something similar, but different.  

 

For a given call (inci_id) AND a given unit (unitcode), I am trying to calculate the time difference between dispatch events (transtype).  The problem is that these values are contained within different rows and not consecutive.  I have attached a sample of my dataset.

closecodeconsoledescriptinci_idradorevtimeinsecstimestamptranstypeunitcodeuserid
 CONSOLE2Dispatched2019365004R159812/31/2019 0:26DR97BMCGARY
 CONSOLE4En-Route2019365004R166612/31/2019 0:27ER97ASABATINO
 CONSOLE2Dispatched2019365007R205012/31/2019 0:34DLD97BMCGARY
 CONSOLE2Arrived2019365004R209212/31/2019 0:34AR97BMCGARY
 CONSOLE2En-Route2019365007R215912/31/2019 0:35ELD97BMCGARY
 CONSOLE2Arrived2019365007R237812/31/2019 0:39ALD97BMCGARY
 CONSOLE2Transport2019365004R287712/31/2019 0:47TR97BMCGARY
FIRCONSOLE2Cleared2019365007R302512/31/2019 0:50CLD97BMCGARY
 CONSOLE2At Hospital2019365004R355012/31/2019 0:59HR97BMCGARY
 CONSOLE2Dispatched2019365017R387012/31/2019 1:04DR99BMCGARY
 CONSOLE2En-Route2019365017R394012/31/2019 1:05ER99BMCGARY
 CONSOLE2Arrived2019365017R433612/31/2019 1:12AR99BMCGARY
FIRCONSOLE2Cleared2019365004R461112/31/2019 1:16CR97BMCGARY

 

Here is the file: https://drive.google.com/file/d/1KNGslhWB4YsFuRbjGOesiLmfegxLOqEE/view?usp=sharing

For example - for inci_id 2019365004 and unit R97 - I want to know the time difference between Dispatched and Arrived.

 

Can you suggest a solution?

 

Thanks in advance

Darrel Donatto

Fire Chief, Palm Beach Fire Rescue

Hi,

Show the expected result very clearly.


Regards,
Ashish Mathur
http://www.ashishmathur.com
https://www.linkedin.com/in/excelenthusiasts/

When I paste in a table it does not post well.

 

DarrelDonatto_0-1656592754546.png

 

Data    
descriptinci_idtimestampunitcode 
Dispatched201936500412/31/2019 00:26:00R97 
En-Route201936500412/31/2019 00:27:00R97 
Dispatched201936500712/31/2019 00:34:00LD97 
Arrived201936500412/31/2019 00:34:00R97 
En-Route201936500712/31/2019 00:35:00LD97 
Arrived201936500712/31/2019 00:39:00LD97 
Transport201936500412/31/2019 00:47:00R97 
Cleared201936500712/31/2019 00:50:00LD97 
At Hospital201936500412/31/2019 00:59:00R97 
Dispatched201936501712/31/2019 01:04:00R99 
En-Route201936501712/31/2019 01:05:00R99 
Arrived201936501712/31/2019 01:12:00R99 
Cleared201936500412/31/2019 01:16:00R97 
     
Expected Result    
inci_idunitcodeDispatchedArrivedResponse Time
2019365004R9712/31/2019 00:26:0012/31/2019 00:34:0008:00
    Arrived Time - Dispatch Time

Hi,

Write these measures

D = CALCULATE(MIN(Data[timestamp]),Data[descript]="Dispatched")
A = CALCULATE(MIN(Data[timestamp]),Data[descript]="Arrived")
Response time = [A]-[D]

Hope this helps.

Untitled.png


Regards,
Ashish Mathur
http://www.ashishmathur.com
https://www.linkedin.com/in/excelenthusiasts/

Thank you so much...  This works well when used in a Matrix that groups by incident id and unit.  I modified it slightly to get the format I needed:

Response time = If([A]=0," ", FORMAT([A]-[D],"h:mm:ss"))
Is there a way to get the Maximum time to show in the incident id part of the matrix instead of the minimum?
DarrelDonatto_0-1656682157372.png

 

You are welcome.  I do not understand your requirement.  Please show the expected result very clearly.


Regards,
Ashish Mathur
http://www.ashishmathur.com
https://www.linkedin.com/in/excelenthusiasts/

I am looking to get the maximum unit response time to appear on the line for the incident ID.  Currently, I am getting the minimum time for all incident ids.  For this case, instead of the 6:50 it should show 13:57.

 

DarrelDonatto_0-1657030140257.png

 

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.