Forum Discussion

Anonymous's avatar
Anonymous
Not applicable
4 years ago
Solved

Recursively calculate diff between dates for customer contracts

Here is a dataset of customer contracts where a customer can have more than one contract, typically yearly. For example, Bruno Lage has 2 one year contracts, one starting on 05/02/20 and the other on...
  • tamerj1's avatar
    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
        Result 

    Then 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
        Result

    Please let me know if this satistfies your requirement. Thank you