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
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.
| 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 |
- CahabaData10 years agoMemorable Member
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
- leroy77310 years agoHelper II
Thanks got everything working now, just need to fine tune the query