Forum Discussion
Recursively calculate diff between dates for customer contracts
- 4 years ago
Hi Anonymous
Here is the sample file with solution https://www.dropbox.com/t/pbCv6QBPPQp0LhAy
The table looks like this
You need to create an order column:Order = VAR CurrentID = Contracts[Id] VAR CurrentStartDate = Contracts[StartDate] VAR CurrentIdTable = FILTER ( Contracts, Contracts[Id] = CurrentID ) VAR Result = RANKX ( CurrentIdTable, Contracts[StartDate], , ASC ) RETURN ResultThen the difference column
Gap (days) = VAR PreviuosEndDate = LOOKUPVALUE ( Contracts[EndDate], Contracts[Id], Contracts[Id], Contracts[Order], Contracts[Order] - 1 ) VAR Difference = DATEDIFF ( PreviuosEndDate, Contracts[StartDate], DAY ) VAR Result = IF ( ISBLANK ( PreviuosEndDate ), 0, Difference ) RETURN ResultPlease let me know if this satistfies your requirement. Thank you
It looks like duplicate start dates ARE possible for the same customer. So a customer can have more than 1 contract starting on the same date. Would that cause this issue?
Also, Not all contracts are 1 year contracts, some are 2 or 3 years long.
Below is an exmaple of a customer whose data looks a bit unusual and the Result it returns for Gap.
You are right. If you have duplicate start date then the value argument inside LOOKUPVALUE will no longer be a scalar value but a table of multiple values (depending on how many duplicate start dates per customer) and this will result in an error. But in this case the whole calculation does not make sense because we cannot tell what subtract from what!! For example can you tell which numbers to you want to achieve in the above example?