Forum Discussion
calculation based on the previous value
Saying "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. 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).
As a simple example of a limited function, let T be a table with a column [x]. Define the measure [x^2] as
VAR _x = SELECTEDVALUE ( T[x] )
RETURN _x * _x
We can now use this measure as a function so long as the input exists in column T[x]. For example,
10^2 = [x^2](T[x] = 10)
= CALCULATE ( [x^2], T[x] = 10 )
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.
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.
I get that. No hurt feelings 🙂 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.
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.
Cheers!