Forum Discussion

NKTMS's avatar
NKTMS
Frequent Visitor
3 years ago
Solved

Consecutive sum

Hi, I've been stuck on this for a while.  I have this table in PowerBI with Columns Lkey,Mkey and Count. Nothings unique in that. I want to create a new column Sum which sums up the values of count...
  • v-yueyunzh-msft's avatar
    3 years ago

    Hi , NKTMS 

    Thanks for your sample data and end result first!

    Here are the steps you can refer to :
    (1)My test data is the same as yours.

    (2)We can add an index column in Power Query to judge the row .

    (3)Then we can apply the data to Power BI Desktop, and we can cretae a calculated column.

    I create two calculated columns and you can choose which the column you need to get :

     

    Column 1 = var _pre_blank =  FILTER('Table','Table'[Index]<EARLIER('Table'[Index]) && 'Table'[Count] =  BLANK())
    var _blank_index =IF(ISBLANK( MAXX(_pre_blank,[Index])), 0, MAXX(_pre_blank,[Index]))
    var _value = SUMX( FILTER('Table' , 'Table'[Index]>_blank_index && 'Table'[Index]<=EARLIER('Table'[Index])) , [Count])
    return
    _value

     

    Column 2 = var _pre_blank =  FILTER('Table','Table'[Index]<EARLIER('Table'[Index]) && 'Table'[Count] =  BLANK())
    var _blank_index =IF(ISBLANK( MAXX(_pre_blank,[Index])), 0, MAXX(_pre_blank,[Index]))
    var _value = SUMX( FILTER('Table' , 'Table'[Index]>_blank_index && 'Table'[Index]<=EARLIER('Table'[Index])) , [Count])
    var _next_blank =   FILTER('Table','Table'[Index]>=EARLIER('Table'[Index]) && 'Table'[Count] =  BLANK())
    var _blank_next=IF( ISBLANK( MINX(_next_blank,[Index]) ) , MAX('Table'[Index])+1 , MINX(_next_blank,[Index]))
    return
    IF(_blank_next -1 = [Index] , _value, BLANK())

     

    Then we can get the result as follows:

     

     

    Thank you for your time and sharing, and thank you for your support and understanding of PowerBI! 

     

    Best Regards,

    Aniya Zhang

    If this post helps, then please consider Accept it as the solution to help the other members find it more quickly