Forum Discussion
Calculate duration based on dates in different rows.
- Anonymous10 years ago
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
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
- leroy77310 years agoHelper II
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.
- leroy77310 years agoHelper II
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.
- CahabaData10 years agoMemorable Member
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?
- leroy77310 years agoHelper II
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.
- leroy77310 years agoHelper II
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.
SN Edit Date Old New Index Duration 1
8/16/2016 13:14 Operational Status Fully Operational Non-Operational 0 69 1 6/8/2016 7:24 Operational Status Reduced Throughput Fully Operational 1 8 1 5/31/2016 12:54 Operational Status Fully Operational Reduced Throughput 2 8 1 5/23/2016 9:35 Operational Status Reduced Throughput Fully Operational 3 7 1 5/16/2016 16:02 Operational Status Fully Operational Reduced Throughput 4 53 1 3/24/2016 9:14 Operational Status Non-Operational Fully Operational 5 0