Forum Discussion
Previous Row Value with logical condition
hi lifesafari ,
It is better to do this in Power Query.
I am sharing Calculated Column.
Assumption : This logic works on assumption that there will always be a row with non blank value for "Codice Disegno" with startd date before the row(tipo 3/9/275/281/276/279/278/4) with blank or "0" for same "Codice Macchina" and Datetime Start.
Calculated Column
---------------------
- lifesafari2 years ago
Helper I
It's same to be work. Let me some time to analyse to confirm as solution.
During this time can i ask you wich is the logic that you have use to arrive at this DAX? Thanks- talespin2 years ago
Solution Sage
hi lifesafari
Please see the comments(Put it in a notepad so its easy to read). Hope this helps. Let me know if any question.
Calculated Column
---------------------
Column =
//Since this is a calculated column, which invokes row context, it will iterate table row by row. I am storing current row values for below two columns in a variable.
VAR _machinecode = TestTable5[Codice Macchina]
VAR _IMESStart = TestTable5[IMES_Start]//Since you mentioned that "Codice Disegno" is blank or 0 where TestTable5[Tipo] is {3,9,275,281,276,279,278,4}, so I have put a condition so that CALCULATE is only executed if TestTable5[Tipo] is {3,9,275,281,276,279,278,4}
//With CALCULATE, context transition is happening from row context to filter context(all the values of all columns in current row will translate to filters).
//So in CALCULATE first I am removing all filters from table and then again applying filter only on "Codice Macchina" and "IMES_Start".
//Since "IMES_Start" is datetime I am fetching only recods before current value of "IMES_Start".
//Then I am making sure only data from current date is retrieve by using FORMAT(TestTable5[IMES_Start], "YYYY-MM-DD") = FORMAT(_IMESStart, "YYYY-MM-DD")
//THen I am excluding all records where TestTable5[Tipo] is {3,9,275,281,276,279,278,4}
//Finally I get the MAX of "Codice Disegno"
return
IF( TestTable5[Tipo] IN {3,9,275,281,276,279,278,4},
CALCULATE(
MAX(TestTable5[Codice Disegno]),
REMOVEFILTERS(TestTable5),
TestTable5[Codice Macchina] = _machinecode,
TestTable5[IMES_Start] <= _IMESStart,
FORMAT(TestTable5[IMES_Start], "YYYY-MM-DD") = FORMAT(_IMESStart, "YYYY-MM-DD"),
NOT(TestTable5[Tipo] IN {3,9,275,281,276,279,278,4})
),
TestTable5[Codice Disegno]
)