Forum Discussion

zaren1987's avatar
zaren1987
New Member
6 years ago

Cycle Count

 

Hi All.


I have my data in Column A and B with state 1 & 0 below.

I would like to have result of Column C & D (highlighted in red) based on Column A and B state.

The result in Column C & D gives me idea on how many cycle count it have in Column A and B, as shown in Column F & G. 

Other rule, the cycle count is only incremented when both Column A & B have state "0" value and the next row of either Column A or B indicates a state "1" value .

Please I will appreciate your help for any idea since I would like to apply this formula in a larger data.


Thank you in advance,

 

1 Reply

  • kentyler's avatar
    kentyler
    Solution Sage

    Thanks for posting an interesting problem
    I created a small table like yours. I added a calculated column that added up the left and right columns. You'll see why when we get to the measure.


    Since we need to access "the next row" and DAX has no built in concept of "next" or "previous" I also

    added an index column in the query editor 

     



     

     

     

     

     

     

     

     

    Now we need to write the measure that calcuates whether a given row should be counted as a cycle

    Is Cycle =
    VAR cur_index =
        SELECTEDVALUE ( Cycle[Index] )
    VAR cur_both =
        SELECTEDVALUE ( Cycle[leftPlusRight] )
    VAR next_row_is_one =
        CALCULATE (
            COUNTROWS ( cycle ),
            ALL ( Cycle ),
            Cycle[Index] = cur_index + 1,
            Cycle[leftPlusRight] > 0
        )
    VAR count_as_cycle = cur_both = 0
        && next_row_is_one = 1
    RETURN
        count_as_cycle

    .We store the index value from the current filter context and the "left plus right" value as local variables.  Then we use CALCULATE to count the rows where the next row  has an index one greater (the next row) and a leftplusright is greater than zero (either 1 or 2)
    then we return the combination of the next row being at least one and the current row being zero to say whether or not it should be counted as a cycle.

    We can put this in a table graphic to check our results 

    This code does not calculate whether the 1 in the "next" row is in the left or right column, it could be changed to do so.