user28081423
user28081423

Reputation: 11

Calculative consecutive values with DAX calculated column where values restart based on condition

I have a Power BI calculated table set up like the following with customer, item, week and cut cases. I'm looking to add another column that sums the number of consecutive weeks the cut cases > 0. Most importantly I need this index to restart back at 1 after any week where cut cases = 0 even though the customer/item grouping stays the same (see GOAL column)

Some intricacies- the week begin dates sometimes skip weeks which is fine, I just need to know the consecutive cut weeks for whatever weeks show up in a row. I don't care about any weeks with no cut or what values show up in the rank for those.

Customer Item CustomerItemCut Week Cut Cases CurrentIndex (WRONG) GOAL
Customer1 A Customer1_A_Cut 7/29/24 1 1 1
Customer1 A Customer1_A_Cut 8/26/24 2 2 2
Customer1 A Customer1_A_NoCut 9/2/24 0 0 0
Customer1 A Customer1_A_NoCut 9/9/24 0 0 0
Customer1 A Customer1_A_NoCut 9/16/24 0 0 0
Customer1 A Customer1_A_Cut 10/7/24 3 3 1
Customer1 A Customer1_A_Cut 10/14/24 2 4 2
Customer1 A Customer1_A_Cut 10/21/24 6 5 3
Customer2 B Customer2_B_Cut 9/9/24 2 1 1
Customer2 B Customer2_B_NoCut 9/16/24 0 0 0
Customer2 B Customer2_B_Cut 9/23/24 2 2 1
Customer2 B Customer2_B_Cut 9/30/24 2 3 2
Customer2 B Customer2_B_Cut 10/7/24 3 4 3
Customer2 B Customer2_B_NoCut 10/14/24 0 0 0
Customer2 B Customer2_B_Cut 10/21/24 2 5 1

My current attempt at using Rankx isn't working because the values don't restart, which is because I can't figure out how to create a unique identifier for the restart condition.

IF(Table1[Cut Cases]<=0,0,Rankx(Filter(Table1,EARLIER(Table1[CustomerItemCut]) = Table1[CustomerItemCut]),Table1[Week Begin Date],,ASC,Dense))

I'm trying to understand how I can A. create a unique identifier that shows what "cut instance" the group is in (like below) so that rankx will work OR

CustomerItemCut
Customer1_A_Cut1
Customer1_A_Cut1
Customer1_A_NoCut
Customer1_A_Cut2
Customer1_A_Cut2
Customer1_A_Cut2
Customer1_A_NoCut

B. just figure out another method with what I have

Upvotes: 1

Views: 40

Answers (1)

Ryan
Ryan

Reputation: 2482

you can try to create a column

column=
VAR _last =
    MAXX (
        FILTER (
            'Table',
            'Table'[Customer] = EARLIER ( 'Table'[Customer] )
                && 'Table'[CustomerItemCut] <> EARLIER ( 'Table'[CustomerItemCut] )
                && 'Table'[Week] < EARLIER ( 'Table'[Week] )
        ),
        'Table'[Week]
    )
VAR _start =
    MINX (
        FILTER (
            'Table',
            'Table'[CustomerItemCut] = EARLIER ( 'Table'[CustomerItemCut] )
                && 'Table'[Week] > _last
        ),
        'Table'[Week]
    )
RETURN
    IF (
        CONTAINSSTRING ( 'Table'[CustomerItemCut], "No" ),
        0,
        CALCULATE (
            COUNTROWS ( 'Table' ),
            FILTER (
                'Table',
                'Table'[CustomerItemCut] = EARLIER ( 'Table'[CustomerItemCut] )
                    && 'Table'[Week] >= _start
                    && 'Table'[Week] <= EARLIER ( 'Table'[Week] )
            )
        )
    )

enter image description here

Upvotes: 0

Related Questions