Forum Discussion

AndyC's avatar
AndyC
Frequent Visitor
1 year ago
Solved

Reset next row based on compounded total

Hello,

I've got most of the way with this but now I'm stuck.

Throughout, I've been using List.Generate within power query  and have linked a sample file below.

 

Scenario - For each row of gains, there is a signal which initiates a position.

A signal of 1 initiates a position of 1.

A further signal of 1 adds to the position. There is a maximum position of 2 allowed.

A signal of 0 means no change to position.

A signal of -1 takes current position to 0.

Return is then calculated from gain x position.

The returns are then grouped whilst there is a position and a compounded return for each group calculated.

This is as far as I've got.

What I'd like to achieve next is to reset any position to 0 when its compounded return reaches -1% or less.

This would also reset the grouping and thus the compounded return

So, in effect, any initiated positions would be reset to 0 either with a signal of -1 or a compounded return <= -1% and would not initiate again until the next signal of 1.

For example, in the linked file, on the 24/08/2023 the group comp return was -2.49%. On the 25/08/2023 I want the position to reset to 0 along with the grouping and group comp return. The position would then stay at 0 until the next positive signal on the 31/08/2023.

This is the first time I’ve used List.Generate and custom functions and I’m not sure whether I’m going down the correct route. It may be that DAX would provide a better way to do this but I have very little experience of using DAX so went with power query instead.

If anyone could help with this it would be much appreciated.

Sample file 

Andy

9 Replies

  • dufoq3's avatar
    dufoq3
    Community Champion

    Hi AndyC, could you add into your sample file also expected output please?

    • AndyC's avatar
      AndyC
      Frequent Visitor

      Hello dufoq3 ,

      I have now added a table for expected output.

      I have highlighted the cells that are different compared to the original table.

      Hope this makes sense.

      • dufoq3's avatar
        dufoq3
        Community Champion

        Hi, I've create same as you but in a simple query (file is attached).

         

        You can find 2 new versions (so you can delete all the others).
        Check [group comp return] column please, because in my opinion you want my v2, but let me know please.

         

         

        My question is:

        why ther is 0 here when signal is 1?