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
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.
- Anonymous10 years agoNot applicable
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 ago
Helper 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 ago
Helper 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 ago
Memorable 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 ago
Helper 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.