<?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: calculation based on the previous value in DAX Commands and Tips</title>
    <link>https://community.fabric.microsoft.com/t5/DAX-Commands-and-Tips/calculation-based-on-the-previous-value/m-p/2886740#M93589</link>
    <description>&lt;P&gt;&lt;a href="javascript:void(0)" data-lia-user-mentions="" data-lia-user-uid="407473" data-lia-user-login="ybyb23" class="lia-mention lia-mention-user"&gt;ybyb23&lt;/a&gt; , I doubt this can be done. the recursive calculation is a little bit of a challenge in DAX. As long as we can achieve them using cumulative, that can be done &lt;/P&gt;</description>
    <pubDate>Sat, 05 Nov 2022 02:31:42 GMT</pubDate>
    <dc:creator>amitchandak</dc:creator>
    <dc:date>2022-11-05T02:31:42Z</dc:date>
    <item>
      <title>calculation based on the previous value</title>
      <link>https://community.fabric.microsoft.com/t5/DAX-Commands-and-Tips/calculation-based-on-the-previous-value/m-p/2886178#M93555</link>
      <description>&lt;P&gt;Hi all,&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I am stuck with creating a dax function, i really need help..&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Here is an example, the final in the table should be my result, I have a value for 2019 which is the starting point of the calculation, then its about adding up the previous value calculated multiplied by the factor.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;img /&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thanks in advance for your support.&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 04 Nov 2022 17:24:16 GMT</pubDate>
      <guid>https://community.fabric.microsoft.com/t5/DAX-Commands-and-Tips/calculation-based-on-the-previous-value/m-p/2886178#M93555</guid>
      <dc:creator>ybyb23</dc:creator>
      <dc:date>2022-11-04T17:24:16Z</dc:date>
    </item>
    <item>
      <title>Re: calculation based on the previous value</title>
      <link>https://community.fabric.microsoft.com/t5/DAX-Commands-and-Tips/calculation-based-on-the-previous-value/m-p/2886740#M93589</link>
      <description>&lt;P&gt;&lt;a href="javascript:void(0)" data-lia-user-mentions="" data-lia-user-uid="407473" data-lia-user-login="ybyb23" class="lia-mention lia-mention-user"&gt;ybyb23&lt;/a&gt; , I doubt this can be done. the recursive calculation is a little bit of a challenge in DAX. As long as we can achieve them using cumulative, that can be done &lt;/P&gt;</description>
      <pubDate>Sat, 05 Nov 2022 02:31:42 GMT</pubDate>
      <guid>https://community.fabric.microsoft.com/t5/DAX-Commands-and-Tips/calculation-based-on-the-previous-value/m-p/2886740#M93589</guid>
      <dc:creator>amitchandak</dc:creator>
      <dc:date>2022-11-05T02:31:42Z</dc:date>
    </item>
    <item>
      <title>Re: calculation based on the previous value</title>
      <link>https://community.fabric.microsoft.com/t5/DAX-Commands-and-Tips/calculation-based-on-the-previous-value/m-p/2886833#M93599</link>
      <description>&lt;P&gt;Hi&amp;nbsp;&lt;a href="javascript:void(0)" data-lia-user-mentions="" data-lia-user-uid="407473" data-lia-user-login="ybyb23" class="lia-mention lia-mention-user"&gt;ybyb23&lt;/a&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;What do you mean by "DAX function"? In DAX there are no functions. Either measures or calculated columns/tables. The above can be done in DAX as a measure because even though the problem in nature is recursive (and DAX does not support such constructs, bar the special kind called "side recursion"), a formula can be crafted that'll be fully iterative. If you need a table with a calculated column, please use Power Query for this.&lt;/P&gt;
&lt;P&gt;Here's how to do it in Power Query:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;LI-CODE lang="csharp"&gt;// T
let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WMjIwtFTSUTK0ABIGSrE6IBEjAyAHiPSMYAKGUAFjmIARVMAEJmAMFTCFCZhABcyUYmMB", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Year = _t, Kpi = _t, Factor = _t]),
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"Year", Int64.Type}, {"Kpi", Int64.Type}, {"Factor", type number}}),
    ReplicateKpiDown = Table.FillDown(#"Changed Type",{"Kpi"}),
    AddIndex = Table.AddIndexColumn(ReplicateKpiDown, "Index", 0, 1, Int64.Type),
    MoveIndexToFirstColumn = Table.ReorderColumns(AddIndex,{"Index", "Year", "Kpi", "Factor"}),
    AddCofactor = Table.AddColumn(MoveIndexToFirstColumn, "Cofactor", each 1 - [Factor]),
    MakeFinalCalculation = Table.AddColumn(AddCofactor, "Final", each [Kpi] * List.Product(List.FirstN(AddCofactor[Cofactor], [Index] + 1)))
in
    MakeFinalCalculation&lt;/LI-CODE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I'll do it in DAX as well and then paste it here. Bear with me...&lt;/P&gt;
&lt;P&gt;Here's the outcome of the calculation in PQ and DAX:&lt;/P&gt;
&lt;P&gt;&lt;img /&gt;&lt;/P&gt;
&lt;P&gt;The file where this is done has been attached...&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sat, 05 Nov 2022 08:42:13 GMT</pubDate>
      <guid>https://community.fabric.microsoft.com/t5/DAX-Commands-and-Tips/calculation-based-on-the-previous-value/m-p/2886833#M93599</guid>
      <dc:creator>daXtreme</dc:creator>
      <dc:date>2022-11-05T08:42:13Z</dc:date>
    </item>
    <item>
      <title>Re: calculation based on the previous value</title>
      <link>https://community.fabric.microsoft.com/t5/DAX-Commands-and-Tips/calculation-based-on-the-previous-value/m-p/2887320#M93626</link>
      <description>&lt;P&gt;DAX certainly has functions (e.g. MAX, PRODUCTX, CALCULATETABLE). It's just that the user cannot define functions, except in limited sorts of ways.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Also note that while DAX cannot do recursion, the M language can. This means that instead of an O(N^2) solution that's required in DAX, we can get much better performance for large tables with an O(N) solution that uses recursion. Using &lt;A href="https://learn.microsoft.com/en-us/powerquery-m/list-accumulate" target="_blank"&gt;List.Accumulate&lt;/A&gt; or&amp;nbsp;&lt;A href="https://learn.microsoft.com/en-us/powerquery-m/list-generate" target="_blank"&gt;List.Generate&lt;/A&gt;&amp;nbsp;is a common way to implement recursive logic. For example,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;LI-CODE lang="php"&gt;let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("NcjLCQAgDATRXvYcJB8VrSWk/zY0h70MzMuEq10I7PwoSlpc//QPpxglKE6ZlKAsyqRsVD0=", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text)) in type table [Year = _t, Kpi = _t, Factor = _t]),
    ChangeType = Table.TransformColumnTypes(Source, {{"Year", Int64.Type}, {"Kpi", Int64.Type}, {"Factor", type number}}),
    NewColumn = List.Generate(
        () =&amp;gt; [x = ChangeType[Kpi]{0}, f = List.Buffer(ChangeType[Factor])],
        each not List.IsEmpty([f]),
        each [
            x = [x] * (1 - List.First(f)),
            f = List.RemoveFirstN([f], 1)
        ],
        each [x]
    ),
    AddColToTable = Table.FromColumns(Table.ToColumns(ChangeType) &amp;amp; {NewColumn}, Table.ColumnNames(ChangeType) &amp;amp; {"Final"})
in
    AddColToTable&lt;/LI-CODE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sun, 06 Nov 2022 00:10:41 GMT</pubDate>
      <guid>https://community.fabric.microsoft.com/t5/DAX-Commands-and-Tips/calculation-based-on-the-previous-value/m-p/2887320#M93626</guid>
      <dc:creator>AlexisOlson</dc:creator>
      <dc:date>2022-11-06T00:10:41Z</dc:date>
    </item>
    <item>
      <title>Re: calculation based on the previous value</title>
      <link>https://community.fabric.microsoft.com/t5/DAX-Commands-and-Tips/calculation-based-on-the-previous-value/m-p/2887394#M93637</link>
      <description>&lt;P&gt;"&lt;SPAN&gt;&lt;EM&gt;DAX certainly has functions (e.g. MAX, PRODUCTX, CALCULATETABLE). It's just that the user cannot define functions, except in limited sorts of ways.&lt;/EM&gt;"&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;&lt;a href="javascript:void(0)" data-lia-user-mentions="" data-lia-user-uid="39298" data-lia-user-login="AlexisOlson" class="lia-mention lia-mention-user"&gt;AlexisOlson&lt;/a&gt;, when I said "functions," I meant &lt;STRONG&gt;user-defined functions&lt;/STRONG&gt;, obviously, because this is what&amp;nbsp;&lt;a href="javascript:void(0)" data-lia-user-mentions="" data-lia-user-uid="407473" data-lia-user-login="ybyb23" class="lia-mention lia-mention-user"&gt;ybyb23&lt;/a&gt;&amp;nbsp;asked for. They do not exist in DAX, not even "in limited sort of ways." Measures can't be considered functions, either, in any way because they can't take arguments, at least directly. But if you want to abuse terminology... well, yes, you can name any object you want anything you want. Nobody can prevent you from this.&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;EM&gt;"Also note that while DAX cannot do recursion, the M language can."&lt;/EM&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;&lt;a href="javascript:void(0)" data-lia-user-mentions="" data-lia-user-uid="39298" data-lia-user-login="AlexisOlson" class="lia-mention lia-mention-user"&gt;AlexisOlson&lt;/a&gt;, have you read what I wrote there above? I did write that even if in DAX there's only side-recursion allowed, &lt;STRONG&gt;full recursion exists in M&lt;/STRONG&gt;.&lt;/SPAN&gt;&lt;/P&gt;</description>
      <pubDate>Sun, 06 Nov 2022 08:23:37 GMT</pubDate>
      <guid>https://community.fabric.microsoft.com/t5/DAX-Commands-and-Tips/calculation-based-on-the-previous-value/m-p/2887394#M93637</guid>
      <dc:creator>daXtreme</dc:creator>
      <dc:date>2022-11-06T08:23:37Z</dc:date>
    </item>
    <item>
      <title>Re: calculation based on the previous value</title>
      <link>https://community.fabric.microsoft.com/t5/DAX-Commands-and-Tips/calculation-based-on-the-previous-value/m-p/2889870#M93770</link>
      <description>&lt;P&gt;Saying "&lt;SPAN&gt;In DAX there are no functions" is not accurate, so I clarified this meant user-defined functions. As you said, measures cannot take arguments directly. However, you can pass filters to a measure that behave like arguments.&amp;nbsp;&lt;/SPAN&gt;&lt;SPAN&gt;This is limited by the fact that the domain of the function you're recreating must already be defined in the data model (you can't have arbitrary inputs; it must come from values in an existing table).&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;As a simple example of a limited function, let T be a table with a column [x]. Define the measure [x^2] as&lt;/SPAN&gt;&lt;/P&gt;
&lt;LI-CODE lang="php"&gt;VAR _x = SELECTEDVALUE ( T[x] )
RETURN _x * _x&lt;/LI-CODE&gt;
&lt;P&gt;&lt;SPAN&gt;We can now use this measure as a function so long as the input exists in column T[x]. For example,&lt;/SPAN&gt;&lt;/P&gt;
&lt;LI-CODE lang="php"&gt;10^2 = [x^2](T[x] = 10)
     = CALCULATE ( [x^2], T[x] = 10 )&lt;/LI-CODE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;For Power Query, I'm not claiming you said anything wrong either. The M code you wrote works fine with the caveat that it's using what you call "side recursion" the same way that's necessary for DAX (which isn't as efficient for large tables). If you're going to use M for the custom column, then I think it makes sense to show a recursive solution too, which is what I provided.&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;I'm not trying to pick a fight. I kudoed your post and it was accepted as the answer, as appropriate. Maybe mine will help a future reader who needs a recursive solution for performance reasons.&lt;/SPAN&gt;&lt;/P&gt;</description>
      <pubDate>Mon, 07 Nov 2022 16:43:49 GMT</pubDate>
      <guid>https://community.fabric.microsoft.com/t5/DAX-Commands-and-Tips/calculation-based-on-the-previous-value/m-p/2889870#M93770</guid>
      <dc:creator>AlexisOlson</dc:creator>
      <dc:date>2022-11-07T16:43:49Z</dc:date>
    </item>
    <item>
      <title>Re: calculation based on the previous value</title>
      <link>https://community.fabric.microsoft.com/t5/DAX-Commands-and-Tips/calculation-based-on-the-previous-value/m-p/2889941#M93781</link>
      <description>&lt;P&gt;&lt;a href="javascript:void(0)" data-lia-user-mentions="" data-lia-user-uid="39298" data-lia-user-login="AlexisOlson" class="lia-mention lia-mention-user"&gt;AlexisOlson&lt;/a&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I get that. No hurt feelings &lt;span class="lia-unicode-emoji" title=":slightly_smiling_face:"&gt;🙂&lt;/span&gt; I appreciate your solution as well. Actually, I have saved this page to have something to return to when I'll need an efficient algorithm for the calculation of running totals. I did know about this way of writing M but was too lazy to implement it, so I went for the less efficient solution. Depending on how big the dataset is, it might do the trick. In case it's too slow, your code should do the trick. My code is probably easier to understand for the people new to Power BI or Power Query. Yours is more advanced. Much more.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Whether DAX has or not any kind of udf's is open to debate but it's not worth debating it. So, let's leave it at that.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Cheers!&lt;/P&gt;</description>
      <pubDate>Mon, 07 Nov 2022 17:11:27 GMT</pubDate>
      <guid>https://community.fabric.microsoft.com/t5/DAX-Commands-and-Tips/calculation-based-on-the-previous-value/m-p/2889941#M93781</guid>
      <dc:creator>daXtreme</dc:creator>
      <dc:date>2022-11-07T17:11:27Z</dc:date>
    </item>
    <item>
      <title>Re: calculation based on the previous value</title>
      <link>https://community.fabric.microsoft.com/t5/DAX-Commands-and-Tips/calculation-based-on-the-previous-value/m-p/2892786#M93941</link>
      <description>&lt;P&gt;Thanks for the solution! It worked perfectly.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I can understand it is important to define precisly the issue, however a function can be an expression, or an operation, as far as I know this is what is about in Dax. I think it's a valid point to highlight it in this forum community by creating a power bi glossery.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I have finally opted to use Python to create the&amp;nbsp;&lt;SPAN&gt;recursive logic.&amp;nbsp;&lt;/SPAN&gt;&lt;/P&gt;</description>
      <pubDate>Tue, 08 Nov 2022 15:46:26 GMT</pubDate>
      <guid>https://community.fabric.microsoft.com/t5/DAX-Commands-and-Tips/calculation-based-on-the-previous-value/m-p/2892786#M93941</guid>
      <dc:creator>ybyb23</dc:creator>
      <dc:date>2022-11-08T15:46:26Z</dc:date>
    </item>
  </channel>
</rss>

