1

I am trying to replicate this Excel formula or for loop that calculates the current row value based on the previous row value of the same column.
When value = 1, Result = Rank * 1 Else, Result = Rank * previous Result

| Value | Rank | Result |
| ----- | ---- | ------ |
|   1   |   3  |   3    |
|   2   |   2  |   6    |
|   3   |   1  |   6    |

I already tried generating a series table to get the Value and Rank columns but I am unable to refer to the existing Result column to update the same. Is there a way to get this done using DAX for dynamic number of rows?

4
  • DAX can't handle recursive calculations... The only realistic option here is to pre- calculate this field at the source and then import it. Commented Feb 11, 2021 at 9:03
  • @RADO, any suggestions for replicating this on SQL server then? like a stored proc? Commented Feb 11, 2021 at 12:11
  • Yes, you can use a stored procedure and write the recursion in SQL. I'd recommend to post it as a separate question with the appropriate tags. Commented Feb 11, 2021 at 12:26
  • @RADO that's true - it cannot handle recursion, but it's powerful enough to solve this problem - look at my answer. Commented Feb 11, 2021 at 12:44

1 Answer 1

1

It can be done, but you need to get your mindset out of recursion. Notice that I did [Rank] even for [Value] equal 1, as [Rank] * [Value] in that case is equal [Rank] (value equals 1, right?).

Result =
IF (
    Table[Value] = 1,
    [Rank],
    [Rank] *
    PRODUCTX (
        FILTER ( Table, Table[Value] < EARLIER ( Table[Value] ) ),
        [Rank]
    )
)

enter image description here

EDIT:

The previous one was unneccessarily complex:

Result =
CALCULATE (
    PRODUCT ( [Rank] ),
    FILTER ( Table, [Value] <= EARLIER ( [Value] ) )
)
Sign up to request clarification or add additional context in comments.

1 Comment

that does help actually! I was playing around with SUMX,MINX along with EARLIER but completely missed PRODUCTX which does the trick - so thank you!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.