Forum Discussion

naitx23's avatar
naitx23
Regular Visitor
5 years ago
Solved

Calculated field based on previous row

Hello,

 

I’m trying to calculate the below field(Results) on PBI( query editor ). Calculation should be grouped by CustomerCode, ITEM, TYPE and the first row of the group should be calculated as Z1+X1-X2 then 2nd up to the last row of the group should be previous result+X1-X2. Let me know if you have any solutions in mind. Thanks in advance

 

1st row = Z1+ X1– X2 ;    15-0-4 = 11

2nd row = prevresult(1st row) + X1 – X2  ;   11+0-1 = 10

 

WeekStartCustomerCodeITEMDepartmentTypeZ1X1X2Result Formula
9/28/2020AAAAppleFINA15 411 Z1 - X1 + X215 + 0 - 4 = 11
10/5/2020AAAAppleFINA15 110 PrevResult + X1 - X211 + 0 - 1 = 10
11/2/2020AAAAppleFINA153 13 PrevResult + X1 - X210 + 3 - 0 =13
10/12/2020AAAAppleOPSB10 19 Z1 - X1 + X210 + 0 - 1 = 9
10/19/2020AAAAppleOPSB10 27 PrevResult + X1 - X29 + 0 - 2 = 7
11/2/2020AAAAppleOPSB10 16 PrevResult + X1 - X27 + 0 - 1 = 6
9/21/2020AAABananaOPSA131 14 Z1 - X1 + X213 + 1 - 0 = 14
10/5/2020AAABananaOPSA13 113 PrevResult + X1 - X214 + 0 - 1 = 13
10/12/2020AAABananaOPSA131311 PrevResult + X1 - X213 + 1 - 3 = 11

 

  • naitx23 

    You can use DAX to create a column (not in query editor)

    Column = 
    VAR _start=CALCULATE(min('Table'[WeekStart]),ALLEXCEPT('Table','Table'[CustomerCode],'Table'[ITEM],'Table'[Type]))
    VAR Z1= MAXX(FILTER('Table','Table'[WeekStart]=_start&&'Table'[CustomerCode]=EARLIER('Table'[CustomerCode])&&'Table'[ITEM]=EARLIER('Table'[ITEM])&&'Table'[Type]=EARLIER('Table'[Type])),'Table'[Z1])
    VAR X1=SUMX(FILTER('Table','Table'[CustomerCode]=EARLIER('Table'[CustomerCode])&&'Table'[ITEM]=EARLIER('Table'[ITEM])&&'Table'[Type]=EARLIER('Table'[Type])&&'Table'[WeekStart]<=EARLIER('Table'[WeekStart])),'Table'[X1])
    VAR X2=SUMX(FILTER('Table','Table'[CustomerCode]=EARLIER('Table'[CustomerCode])&&'Table'[ITEM]=EARLIER('Table'[ITEM])&&'Table'[Type]=EARLIER('Table'[Type])&&'Table'[WeekStart]<=EARLIER('Table'[WeekStart])),'Table'[X2])
    RETURN Z1+X1-X2

5 Replies

  • Let me preface this that I am still somewhat new to PBI but I think I know how to do what you need.

    To do this you'll first need to add an index column (I prefer to start from 0).

    Once you have your index column, you can create a new calculated column use a DAX expression as follows:

    Column = LOOKUPVALUE('*TableName*'[Result], '*TableName*'[Index], '*TableName*'[Index]-1)+0

    You may need to change some of the syntax to get it to work. And obviously for the first row, since you can't pull data from a previous row (since there isn't one), you may need to put the expression inside an IF expression.

    There may be a better way to do it but I think that should at least work.

    • naitx23's avatar
      naitx23
      Regular Visitor

      thank you for answering, but if you will notice every start of the group the formula is Z1 + X1 -X2.

       

      on the first row the formula is Z1 + X1 -X2 

      succeeding row until the end of the group is previousresult(1st row) + X1 - X2

       

       

      WeekStartCustomerCodeITEMDepartmentTypeZ1X1X2Result Formula
      9/28/2020AAAAppleFINA15 411 Z1 - X1 + X215 + 0 - 4 = 11
      10/5/2020AAAAppleFINA15 110 PrevResult + X1 - X211 + 0 - 1 = 10
      11/2/2020AAAAppleFINA153 13 PrevResult + X1 - X210 + 3 - 0 =13
      10/12/2020AAAAppleOPSB10 19 Z1 - X1 + X210 + 0 - 1 = 9
      10/19/2020AAAAppleOPSB10 27 PrevResult + X1 - X29 + 0 - 2 = 7
      11/2/2020AAAAppleOPSB10 16 PrevResult + X1 - X27 + 0 - 1 = 6
  • naitx23 

    You can use DAX to create a column (not in query editor)

    Column = 
    VAR _start=CALCULATE(min('Table'[WeekStart]),ALLEXCEPT('Table','Table'[CustomerCode],'Table'[ITEM],'Table'[Type]))
    VAR Z1= MAXX(FILTER('Table','Table'[WeekStart]=_start&&'Table'[CustomerCode]=EARLIER('Table'[CustomerCode])&&'Table'[ITEM]=EARLIER('Table'[ITEM])&&'Table'[Type]=EARLIER('Table'[Type])),'Table'[Z1])
    VAR X1=SUMX(FILTER('Table','Table'[CustomerCode]=EARLIER('Table'[CustomerCode])&&'Table'[ITEM]=EARLIER('Table'[ITEM])&&'Table'[Type]=EARLIER('Table'[Type])&&'Table'[WeekStart]<=EARLIER('Table'[WeekStart])),'Table'[X1])
    VAR X2=SUMX(FILTER('Table','Table'[CustomerCode]=EARLIER('Table'[CustomerCode])&&'Table'[ITEM]=EARLIER('Table'[ITEM])&&'Table'[Type]=EARLIER('Table'[Type])&&'Table'[WeekStart]<=EARLIER('Table'[WeekStart])),'Table'[X2])
    RETURN Z1+X1-X2