Search code examples
powerbidaxdaxstudio

running sum/cumulative sum in direct query power bi


i have spent alot time on this with this, but not sure why this aint working.i have the dataset like:

part    loc    wk       demand  cumulative
123-1   1000   20_wk01   10      10
123-1   1000   20_wk02   15      25
123-1   1000   20_wk03   12      27
123-2   1020   20_wk01   13      13
123-2   1020   20_wk02   14      27
123-2   1020   20_wk03   15      42

all the columns are "text" and i have a part filter. "Demand" is a measure. Hence my code tried is:

RunningTotal = CALCULATE(demand, FILTER( ALLSELECTED( 'table'[part]),
 'table'[part]<=MAX('table'[part]) ) ,
FILTER(ALLSELECTED('table'[loc]),'table'[loc]<=MAX('table'[loc])
),FILTER(ALLSELECTED('table'[wk]),'table'[wk]<=MAX('table'[wk])))

please assist. I am struggling with part which is not working as desired.


Solution

  • You can do the same with a Measure as below-

    cumulative = 
    
    var cur_row_wk = min(table[wk])
    
    return
    CALCULATE(
        'table'[demand],
        FILTER(
            ALLEXCEPT('table',table[part], table[loc]),
            'table'[wk] <= cur_row_wk
        )
    )
    

    Output-

    enter image description here