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

Register now to learn Fabric in free live sessions led by the best Microsoft experts. From Apr 16 to May 9, in English and Spanish.

Reply
leroy773
Helper II
Helper II

Calculate duration based on dates in different rows.

All,

 

I am attempting to see if Power BI is a good fit.  And trying to get started on reporting.  I have data from salesforce I reported in the query.  I am looking for an easy way to show the duration between dates and report the value associated with it.  In excel it is pretty starightforward, but looking for a more automated way in BI.  Since Ihave to update the file and subtract from the current date for the first row.  Any help will be greatly appreaciated.  In excel I added the duration and just copied the Status column

 

Edit DateOld ValueNew ValueSerialdurationStatus
10/19/15 8:06 AMDown for MaintenanceFully Operational1300.66Fully Operational
10/14/15 9:11 AMFully OperationalDown for Maintenance14.954861111Down for Maintenance
7/21/15 8:06 AMNon-OperationalFully Operational185.04513889Fully Operational
7/15/15 12:35 PMFully OperationalNon-Operational15.813194444Non-Operational
7/7/15 2:10 PMNon-OperationalFully Operational17.934027778Fully Operational
7/6/15 5:09 PMFully OperationalNon-Operational10.875694444Non-Operational
5/20/15 7:13 AMNon-OperationalFully Operational147.41388889Fully Operational
5/18/15 8:42 AMFully OperationalNon-Operational11.938194444Non-Operational
4/15/15 6:40 AMCustomer SituationFully Operational133.08472222Fully Operational
4/14/15 4:36 PMNon-OperationalCustomer Situation10.586111111Customer Situation
4/14/15 6:12 AMFully OperationalNon-Operational10.433333333Non-Operational
3/26/15 7:36 PMNon-OperationalFully Operational118.44166667Fully Operational
3/25/15 1:37 PMFully OperationalNon-Operational11.249305556Non-Operational
7/20/16 8:42 PMReduced ThroughputFully Operational225.14Fully Operational
7/15/16 12:11 PMNon-OperationalReduced Throughput25.354861111Reduced Throughput
7/15/16 7:54 AMFully OperationalNon-Operational20.178472222Non-Operational
6/18/16 4:31 AMNon-OperationalFully Operational227.14097222Fully Operational
6/17/16 5:42 AMFully OperationalNon-Operational20.950694444Non-Operational
6/14/16 2:07 PMNon-OperationalFully Operational22.649305556Fully Operational
6/10/16 7:27 AMFully OperationalNon-Operational24.277777778Non-Operational
6/8/16 4:01 PMNon-OperationalFully Operational21.643055556Fully Operational
6/7/16 1:05 PMFully OperationalNon-Operational21.122222222Non-Operational
5/31/16 11:58 AMNon-OperationalFully Operational27.046527778Fully Operational
5/25/16 6:57 AMFully OperationalNon-Operational26.209027778Non-Operational
2/26/16 5:51 AMReduced ThroughputFully Operational289.04583333Fully Operational
2/25/16 5:50 AMNon-OperationalReduced Throughput21.000694444Reduced Throughput
1 ACCEPTED SOLUTION

Hi @leroy773,


Based on your description, you want to compare your current row with previous row, right? If that is the case, firstly, go to query editor of Power BI Desktop and add an index column in your current table.

Secondly, add a new column and write DAX formula to compare date values of the rows.

Duration = DATEDIFF(Table5[Column1],IF(Table5[Index]=0,Table5[Column1],LOOKUPVALUE(Table5[Column1],Table5[Index],Table5[Index]-1)),HOUR)/24


For more details, you can review the example in the attached PBIX file. 



Thanks,
Lydia Zhang

Community Support Team _ Lydia Zhang
If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

View solution in original post

11 REPLIES 11
ankitpatira
Community Champion
Community Champion

@leroy773 in power bi desktop easiest way to do is go to query editor, under Add Column tab -> Add Custom Coumn which will give you dialog box to enter power query. you can simply drag and drop your start and end date columns and subtract them. this will create a step in power bi desktop which will be applied each time you refresh the query. If your column type of start and end date is date/time then resulting column will also be date time.

 

 

Capture.PNG

Thanks Ankit...forgot that Power BI is not similar to Excel in Formula ease.

Thanks for the feedback, but unfortunately I only have one date in each row. So need to be able to subtract from previous row. But that tip is useful for other items.

Hi @leroy773,


Based on your description, you want to compare your current row with previous row, right? If that is the case, firstly, go to query editor of Power BI Desktop and add an index column in your current table.

Secondly, add a new column and write DAX formula to compare date values of the rows.

Duration = DATEDIFF(Table5[Column1],IF(Table5[Index]=0,Table5[Column1],LOOKUPVALUE(Table5[Column1],Table5[Index],Table5[Index]-1)),HOUR)/24


For more details, you can review the example in the attached PBIX file. 



Thanks,
Lydia Zhang

Community Support Team _ Lydia Zhang
If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

I would like to ask a question about the advice to add an index.

 

Is there an ordinal property of that; by which I mean - is the newly invented index definitely going to go from low (first row seen at top) to high (last row seen at bottom) ?

 

I ask because in the post - the date field is in reverse chronological order.  So that the next date is Index +1 versus Index -1. 

 

Also I ask because from the database perspective the definition of an index is that the value is guaranteed to be unique - but not necessarily sequential.  Particularly if records get deleted - in a database that key/index value is never re-used.  So that to tie one row to another row logically one doesn't use the index (key) value but rather a relevant value in one of the fields.

 

So in Power BI - if one of the records is deleted - does the index above it reset so that the +1 or -1 is always valid?

www.CahabaData.com

Will try my best to answer, for this report.  I pull data from salesforce where the records will not be deleted.  I am looking at history of status.  The report in the query is sorted by serial number than by edit date.  Each row has a single date field.  Below is the latest iteration of the formula.  I have updated the formula to calculate the difference between the dates in each row, based on the fact if the serial number in the next row matches the serial number in the current row.

 

Duration = if(LOOKUPVALUE('Instrument Status History'[Installed Product: Installed Product ID],'Instrument Status History'[Index],'Instrument Status History'[Index]+1)='Instrument Status History'[Installed Product: Installed Product ID],DATEDIFF(LOOKUPVALUE('Instrument Status History'[Edit Date],'Instrument Status History'[Index],'Instrument Status History'[Index]+1),'Instrument Status History'[Edit Date],DAY))

 

I am not trying to figure out a way to calculate today's date from the first row associated with serial number.  Such that if the the last edit date was May 22nd, I would like to calculate today-may22nd and have it report.  Working on that currently.

 

Then will need to work on a way of calculating percentage for each status for said time period to show in the graphical interface.

Is there a way in power BI to offset the calucation to the previous row.  Currently duration shows up in the row, but would like to offset the calculation to show the duration in the previous row.  Currently 69 is in the first row, but would like that to show up in the second and row.

SNEdit Date OldNewIndexDuration

1

8/16/2016 13:14Operational StatusFully OperationalNon-Operational069
16/8/2016 7:24Operational StatusReduced ThroughputFully Operational18
15/31/2016 12:54Operational StatusFully OperationalReduced Throughput28
15/23/2016 9:35Operational StatusReduced ThroughputFully Operational37
15/16/2016 16:02Operational StatusFully OperationalReduced Throughput453
13/24/2016 9:14Operational StatusNon-OperationalFully Operational50

I don't follow what you mean by 'calculate' .....  but Lydia gave method in post on how to join another row's value using LookupValue and (Index-1) 

 

not sure if the right solution is -1 or +1 but in any case it is the concept to use

www.CahabaData.com

Thanks got everything working now, just need to fine tune the query

 

Thanks for the recommendation currently working with your proposal.  Unfortunately datediff is not recognized.  Checking the format of my dates, but continues to state un recognized.

Found my mistake for the datediff, was using it in power query instead of the BI.  The datediff function is recognized but now fails for the start date cannot be greater than the end date.  Still investigating.

Helpful resources

Announcements
Microsoft Fabric Learn Together

Microsoft Fabric Learn Together

Covering the world! 9:00-10:30 AM Sydney, 4:00-5:30 PM CET (Paris/Berlin), 7:00-8:30 PM Mexico City

PBI_APRIL_CAROUSEL1

Power BI Monthly Update - April 2024

Check out the April 2024 Power BI update to learn about new features.

April Fabric Community Update

Fabric Community Update - April 2024

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