Forum Discussion
Dynamically referencing above rows in the same column
Hi Community,
How can we reference the row above using dax
| Material QTY | Rate | Date | X Closing Stock | Correct Closing stock | Index |
| 0 | 0.2 | 11-Apr-23 | 0 | 0 | 0 |
| 0 | 0.2 | 12-Apr-23 | 0 | 0 | 1 |
| 0 | 0.2 | 13-Apr-23 | 0 | 0 | 2 |
| 0 | 0.2 | 14-Apr-23 | 0 | 0 | 3 |
| 0 | 0.2 | 15-Apr-23 | 0 | 0 | 4 |
| 0 | 0.2 | 16-Apr-23 | 0 | 0 | 5 |
| 150000 | 0.2 | 17-Apr-23 | 150000 | 150000 | 6 |
| 1000 | 0.2 | 18-Apr-23 | 121000 | 121000 | 7 |
| 0 | 0.2 | 19-Apr-23 | 96800 | 96800 | 8 |
| 100000 | 0.15 | 20-Apr-23 | 177440 | 177440 | 9 |
| 0 | 0.15 | 21-Apr-23 | 146952 | 150824 | 10 |
| 0 | 0.15 | 22-Apr-23 | 121811.6 | 128200.4 | 11 |
| 0 | 0.15 | 23-Apr-23 | 101061.78 | 108970.34 | 12 |
| 0 | 0.15 | 24-Apr-23 | 83920.049 | 92624.789 | 13 |
need to calculate the data in correct closing stock (excel formula used is: =E2*(1-B3)+A3 in the E3)
with the help tamerj1 and making minor changes to the solution given by tamerj1 to a similar problem I could come up with
NaveenMD
Here you goClosing Stock 2 = VAR T1 = FILTER ( 'Table', 'Table'[Index] <= EARLIER ( 'Table'[Index] ) ) RETURN SUMX ( T1, VAR Qty = 'Table'[Material QTY] VAR T2 = FILTER ( T1, 'Table'[Index] >= EARLIER ( 'Table'[Index] ) ) VAR Rate = PRODUCTX ( T2, IF ( 'Table'[Index] = EARLIEST ( 'Table'[Index] ), 1, ( 1 - 'Table'[Rate] ) ) ) RETURN Qty * Rate )
14 Replies
- Greg_DecklerCommunity Champion
NaveenMD See my article on Mean Time Between Failure (MTBF) which uses EARLIER: http://community.powerbi.com/t5/Community-Blog/Mean-Time-Between-Failure-MTBF-and-Power-BI/ba-p/339586.
The basic pattern is:
Column =
VAR __Current = [Value]
VAR __PreviousDate = MAXX(FILTER('Table','Table'[Date] < EARLIER('Table'[Date])),[Date])
VAR __Previous = MAXX(FILTER('Table',[Date]=__PreviousDate),[Value])
RETURN
__Current - __Previous- tamerj1Community Champion
Greg_Deckler
This is a nice example of pseudo-recursive calculation that might be helpful to you for your new book. - tamerj1Community Champion
NaveenMD
There is no fixed pattern for what I call "Semi-Recursive" calculations.Referring to the previous row in the same column that is under evalution is not possible in Power Bi as the whole column is evaluated as set not cell by cell as the case in excel. However, we can trace back the the calculation of the previous cell to notice that it is actually evaluated the existing values of other (existing) columns and the cell previous to it and so on until we reach to the very first cell which is being totally evaluated from other existing column(s).
This would require taking some advantage of mathmics. Assuming quantity column "Q" and (1 - [Ratio]) is R then we can write
Row1: = Q1 Row2: = Q1*R2 + Q2 Row3: = (Row2)*R3 + Q3 = (Q1*R2 + Q2)*R3 + Q3 = Q1*R2*R3 + Q2*R3 + Q4 Similarly Row4 would be: = Q1*R2*R3*R4 + Q2*R3*R4 + Q3*R4 + Q4 And so on...If we look closely to Row4
Now it is a matter to find the tables that need to be iterated inside each of these two iterators. The rest is just fine tuning some details.
Closing Stock 2 = VAR MinIndex = MINX ( FILTER ( 'Table', 'Table'[Material QTY] > 0 ), 'Table'[Index] ) VAR T1 = FILTER ( 'Table', 'Table'[Index] <= EARLIER ( 'Table'[Index] ) ) RETURN SUMX ( T1, VAR Qty = 'Table'[Material QTY] VAR T2 = FILTER ( T1, 'Table'[Index] > EARLIER ( 'Table'[Index] ) ) VAR Rate = COALESCE( PRODUCTX ( T2, 1 - 'Table'[Rate] ), 1 ) RETURN Qty * Rate )- NaveenMDHelper I
tamerj1 this is where I am facing an issue by using the DAX
column 3 is the required rate% and column 2 is the actual rate % that has been calculated using the DAX.
I am hypothesising that somewhere the rate values are getting averaged out thus we are facing this problem.
Material QTY Rate Date Correct Closing stock Index 0 0.2 11-Apr-23 0 0 0 0.2 12-Apr-23 0 1 0 0.2 13-Apr-23 0 2 0 0.2 14-Apr-23 0 3 0 0.2 15-Apr-23 0 4 0 0.2 16-Apr-23 0 5 150000 0.2 17-Apr-23 150000 6 1000 0.2 18-Apr-23 121000 7 0 0.25 19-Apr-23 90750 8 100000 0.15 20-Apr-23 168062.5 9 0 0.12 21-Apr-23 147895 10 0 0.3 22-Apr-23 103526.5 11 0 0.15 23-Apr-23 87997.525 12 0 0.15 24-Apr-23 74797.89625 13 this a new data with much more variablity in the rate.
It would be great relief if you can help me with this.
- NaveenMDHelper I
Sorry for the concern tamerj1 !
Excel Formula : =D2*(1-B2)+A3
Material QTY Rate Date Correct Closing stock 0 0.2 11-Apr-23 0 0 0.2 12-Apr-23 0 0 0.2 13-Apr-23 0 0 0.2 14-Apr-23 0 0 0.2 15-Apr-23 0 0 0.2 16-Apr-23 0 150000 0.2 17-Apr-23 150000 1000 0.2 18-Apr-23 121000 0 0.2 19-Apr-23 96800 100000 0.15 20-Apr-23 177440 0 0.15 21-Apr-23 150824 0 0.15 22-Apr-23 128200.4 0 0.15 23-Apr-23 108970.34 0 0.15 24-Apr-23 92624.789 You can use the link for accessing the file : https://docs.google.com/spreadsheets/d/1E8obh8vJHHI6OX8FSEzTQ4_iM4OSGdQY/edit?usp=sharing&ouid=103277306777121863015&rtpof=true&sd=true
- tamerj1Community Champion
NaveenMD
Here you goClosing Stock 2 = VAR T1 = FILTER ( 'Table', 'Table'[Index] <= EARLIER ( 'Table'[Index] ) ) RETURN SUMX ( T1, VAR Qty = 'Table'[Material QTY] VAR T2 = FILTER ( T1, 'Table'[Index] >= EARLIER ( 'Table'[Index] ) ) VAR Rate = PRODUCTX ( T2, IF ( 'Table'[Index] = EARLIEST ( 'Table'[Index] ), 1, ( 1 - 'Table'[Rate] ) ) ) RETURN Qty * Rate )- JoaoSimasNew Member
Hi tamerj1 - thank you for this very good solution. I managed to solve a very similar problem using the solution from Closing Stock 1, which fits the best for my need. I'm struggling now to make it interactive by filters, meaning that I would like to make it a measure instead of a calculated column.
In my case it's like a have a measure for Material QTY and Rate, then I would like to create te Closing Stock 1 as a measure. Do you think this would be possible?
Thank you!