<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Re: Recursively calculate diff between dates for customer contracts in DAX Commands and Tips</title>
    <link>https://community.fabric.microsoft.com/t5/DAX-Commands-and-Tips/Recursively-calculate-diff-between-dates-for-customer-contracts/m-p/2352306#M59766</link>
    <description>&lt;P&gt;Hi&amp;nbsp;Anonymous&lt;/LI-USER&gt;&amp;nbsp;&lt;BR /&gt;Here is the sample file with solution&amp;nbsp;&lt;A href="https://www.dropbox.com/t/pbCv6QBPPQp0LhAy" target="_blank"&gt;https://www.dropbox.com/t/pbCv6QBPPQp0LhAy&lt;/A&gt;&lt;BR /&gt;The table looks like this&lt;BR /&gt;&lt;img /&gt;&lt;BR /&gt;You need to create an order column:&lt;/P&gt;&lt;LI-CODE lang="javascript"&gt;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 &lt;/LI-CODE&gt;&lt;P&gt;Then the difference column&lt;/P&gt;&lt;LI-CODE lang="javascript"&gt;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&lt;/LI-CODE&gt;&lt;P&gt;Please let me know if this satistfies your requirement. Thank you&lt;/P&gt;</description>
    <pubDate>Tue, 22 Feb 2022 04:22:03 GMT</pubDate>
    <dc:creator>tamerj1</dc:creator>
    <dc:date>2022-02-22T04:22:03Z</dc:date>
    <item>
      <title>Recursively calculate diff between dates for customer contracts</title>
      <link>https://community.fabric.microsoft.com/t5/DAX-Commands-and-Tips/Recursively-calculate-diff-between-dates-for-customer-contracts/m-p/2351805#M59731</link>
      <description>&lt;P&gt;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 07/10/2021.&amp;nbsp;&lt;/P&gt;&lt;P&gt;I'm trying to calculate the field named 'Gap' (Gap = Start Date - last Enddate). This field should identifiy the time between contracts for each customer in days, so if its a new contract then it should be 0 or if a customer has immediately renwed their contract then it should also be 0 (e.g their contracts ends on 22/02/2022 and they have a new one starting on the same date).&lt;/P&gt;&lt;P&gt;Basically, I think we need to order the data according to start date and then recursively find the difference between dates (Start Date - last Enddate).&lt;/P&gt;&lt;P&gt;How can I calculate this coloum/measure with DAX?&lt;BR /&gt;&lt;BR /&gt;&lt;/P&gt;&lt;P&gt;&lt;img /&gt;&lt;BR /&gt;Any help is much appreciated!&lt;/P&gt;</description>
      <pubDate>Mon, 21 Feb 2022 18:53:26 GMT</pubDate>
      <guid>https://community.fabric.microsoft.com/t5/DAX-Commands-and-Tips/Recursively-calculate-diff-between-dates-for-customer-contracts/m-p/2351805#M59731</guid>
      <dc:creator>Anonymous</dc:creator>
      <dc:date>2022-02-21T18:53:26Z</dc:date>
    </item>
    <item>
      <title>Re: Recursively calculate diff between dates for customer contracts</title>
      <link>https://community.fabric.microsoft.com/t5/DAX-Commands-and-Tips/Recursively-calculate-diff-between-dates-for-customer-contracts/m-p/2351928#M59739</link>
      <description>&lt;P&gt;Hello:&lt;/P&gt;&lt;P&gt;I'm not sure I totally understand but I beleive this will get you close -you can change the IF Statement:&lt;/P&gt;&lt;P&gt;Three Calc Columns. Please see below. I hope this gets you going in the right direction!&lt;/P&gt;&lt;DIV&gt;&lt;DIV&gt;&lt;SPAN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&gt;'Table'[ID]&lt;/SPAN&gt;&lt;SPAN&gt;)), &lt;/SPAN&gt;&lt;/DIV&gt;&lt;DIV&gt;&lt;SPAN&gt;'Table'[Start Date]&lt;/SPAN&gt;&lt;SPAN&gt;)&lt;/SPAN&gt;&lt;/DIV&gt;&lt;DIV&gt;&amp;nbsp;&lt;/DIV&gt;&lt;DIV&gt;&lt;DIV&gt;&lt;DIV&gt;&lt;SPAN&gt;Answer = &lt;/SPAN&gt;&lt;SPAN&gt;IF&lt;/SPAN&gt;&lt;SPAN&gt;(&lt;/SPAN&gt;&lt;SPAN&gt;'Table'[END Date]&lt;/SPAN&gt;&lt;SPAN&gt; &amp;lt; &lt;/SPAN&gt;&lt;SPAN&gt;TODAY&lt;/SPAN&gt;&lt;SPAN&gt;() -&lt;/SPAN&gt;&lt;SPAN&gt;365&lt;/SPAN&gt;&lt;SPAN&gt;,&lt;/SPAN&gt;&lt;SPAN&gt;0&lt;/SPAN&gt;&lt;SPAN&gt;,&lt;/SPAN&gt;&lt;/DIV&gt;&lt;DIV&gt;&lt;SPAN&gt;INT&lt;/SPAN&gt;&lt;SPAN&gt;(&lt;/SPAN&gt;&lt;SPAN&gt;'Table'[Max EDate]&lt;/SPAN&gt;&lt;SPAN&gt; - &lt;/SPAN&gt;&lt;SPAN&gt;'Table'[Min Date]&lt;/SPAN&gt;&lt;SPAN&gt; ))&lt;/SPAN&gt;&lt;/DIV&gt;&lt;/DIV&gt;&lt;/DIV&gt;&lt;/DIV&gt;&lt;/DIV&gt;&lt;/DIV&gt;</description>
      <pubDate>Mon, 21 Feb 2022 21:38:38 GMT</pubDate>
      <guid>https://community.fabric.microsoft.com/t5/DAX-Commands-and-Tips/Recursively-calculate-diff-between-dates-for-customer-contracts/m-p/2351928#M59739</guid>
      <dc:creator>Whitewater100</dc:creator>
      <dc:date>2022-02-21T21:38:38Z</dc:date>
    </item>
    <item>
      <title>Re: Recursively calculate diff between dates for customer contracts</title>
      <link>https://community.fabric.microsoft.com/t5/DAX-Commands-and-Tips/Recursively-calculate-diff-between-dates-for-customer-contracts/m-p/2352306#M59766</link>
      <description>&lt;P&gt;Hi&amp;nbsp;Anonymous&lt;/LI-USER&gt;&amp;nbsp;&lt;BR /&gt;Here is the sample file with solution&amp;nbsp;&lt;A href="https://www.dropbox.com/t/pbCv6QBPPQp0LhAy" target="_blank"&gt;https://www.dropbox.com/t/pbCv6QBPPQp0LhAy&lt;/A&gt;&lt;BR /&gt;The table looks like this&lt;BR /&gt;&lt;img /&gt;&lt;BR /&gt;You need to create an order column:&lt;/P&gt;&lt;LI-CODE lang="javascript"&gt;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 &lt;/LI-CODE&gt;&lt;P&gt;Then the difference column&lt;/P&gt;&lt;LI-CODE lang="javascript"&gt;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&lt;/LI-CODE&gt;&lt;P&gt;Please let me know if this satistfies your requirement. Thank you&lt;/P&gt;</description>
      <pubDate>Tue, 22 Feb 2022 04:22:03 GMT</pubDate>
      <guid>https://community.fabric.microsoft.com/t5/DAX-Commands-and-Tips/Recursively-calculate-diff-between-dates-for-customer-contracts/m-p/2352306#M59766</guid>
      <dc:creator>tamerj1</dc:creator>
      <dc:date>2022-02-22T04:22:03Z</dc:date>
    </item>
    <item>
      <title>Re: Recursively calculate diff between dates for customer contracts</title>
      <link>https://community.fabric.microsoft.com/t5/DAX-Commands-and-Tips/Recursively-calculate-diff-between-dates-for-customer-contracts/m-p/2353442#M59829</link>
      <description>&lt;P&gt;95% of this is exactly what i was looking for, thank you, much appreciated!&amp;nbsp; &amp;nbsp;&lt;BR /&gt;&lt;BR /&gt;The only error i got was from the final ISBLANK() check, it returned the following =&amp;nbsp; "A table of multiple values was supplied where a single value was expected."&lt;/P&gt;</description>
      <pubDate>Tue, 22 Feb 2022 13:04:53 GMT</pubDate>
      <guid>https://community.fabric.microsoft.com/t5/DAX-Commands-and-Tips/Recursively-calculate-diff-between-dates-for-customer-contracts/m-p/2353442#M59829</guid>
      <dc:creator>Anonymous</dc:creator>
      <dc:date>2022-02-22T13:04:53Z</dc:date>
    </item>
    <item>
      <title>Re: Recursively calculate diff between dates for customer contracts</title>
      <link>https://community.fabric.microsoft.com/t5/DAX-Commands-and-Tips/Recursively-calculate-diff-between-dates-for-customer-contracts/m-p/2353470#M59832</link>
      <description>&lt;P&gt;Technically, it should not be possible.&lt;BR /&gt;LOOKUPVALUE retruns a value not a table. And if it recieves multiple values it returns a blank. There is not a single table in the whole formula! Can you please explain further. I guess you have blanks in the start date? As duplicate start dates shall not be possible under the same name?&lt;/P&gt;</description>
      <pubDate>Tue, 22 Feb 2022 13:18:03 GMT</pubDate>
      <guid>https://community.fabric.microsoft.com/t5/DAX-Commands-and-Tips/Recursively-calculate-diff-between-dates-for-customer-contracts/m-p/2353470#M59832</guid>
      <dc:creator>tamerj1</dc:creator>
      <dc:date>2022-02-22T13:18:03Z</dc:date>
    </item>
    <item>
      <title>Re: Recursively calculate diff between dates for customer contracts</title>
      <link>https://community.fabric.microsoft.com/t5/DAX-Commands-and-Tips/Recursively-calculate-diff-between-dates-for-customer-contracts/m-p/2354117#M59871</link>
      <description>&lt;P&gt;It looks like &lt;SPAN&gt;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?&amp;nbsp;&amp;nbsp;&lt;BR /&gt;Also, Not all contracts are 1 year contracts, some are 2 or 3 years long.&lt;BR /&gt;Below is an exmaple of a customer whose data looks a bit unusual and the Result it returns for Gap.&lt;BR /&gt;&lt;img /&gt;&lt;/SPAN&gt;&lt;/P&gt;</description>
      <pubDate>Tue, 22 Feb 2022 18:15:48 GMT</pubDate>
      <guid>https://community.fabric.microsoft.com/t5/DAX-Commands-and-Tips/Recursively-calculate-diff-between-dates-for-customer-contracts/m-p/2354117#M59871</guid>
      <dc:creator>Anonymous</dc:creator>
      <dc:date>2022-02-22T18:15:48Z</dc:date>
    </item>
    <item>
      <title>Re: Recursively calculate diff between dates for customer contracts</title>
      <link>https://community.fabric.microsoft.com/t5/DAX-Commands-and-Tips/Recursively-calculate-diff-between-dates-for-customer-contracts/m-p/2354856#M59917</link>
      <description>&lt;P&gt;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&amp;nbsp;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&amp;nbsp;in the above example?&lt;/P&gt;</description>
      <pubDate>Wed, 23 Feb 2022 05:23:41 GMT</pubDate>
      <guid>https://community.fabric.microsoft.com/t5/DAX-Commands-and-Tips/Recursively-calculate-diff-between-dates-for-customer-contracts/m-p/2354856#M59917</guid>
      <dc:creator>tamerj1</dc:creator>
      <dc:date>2022-02-23T05:23:41Z</dc:date>
    </item>
  </channel>
</rss>

