Forum Discussion
Create Custom Column based on filter in Related table
- 6 years ago
Hi larsm11 ,
The following DAX will create the column you are looking for. Just a warning: a calculated column is not evaluated when you apply new filters, only when you do a data refresh the calculated column is re-evaluated. If you want it to be dynamic, you will need to use measures.
daysBetween = VAR _curID = Table1[ID] VAR _highestAmount = MAXX(FILTER(Table2, Table2[id] = _curID), [amount]) VAR _lowestAmount = MINX(FILTER(Table2, Table2[id] = _curID), [amount]) VAR _highestDate = MAXX(FILTER(Table2, Table2[id] = _curID && Table2[amount] = _highestAmount), [date]) VAR _lowestDate = MAXX(FILTER(Table2, Table2[id] = _curID && Table2[amount] = _lowestAmount), [date]) RETURN DATEDIFF(_highestDate, _lowestDate, DAY)I used variables as much as possible to illustrate the logic 🙂 Let me know if this solves your issue!
Hi larsm11 ,
The following DAX will create the column you are looking for. Just a warning: a calculated column is not evaluated when you apply new filters, only when you do a data refresh the calculated column is re-evaluated. If you want it to be dynamic, you will need to use measures.
daysBetween =
VAR _curID = Table1[ID]
VAR _highestAmount = MAXX(FILTER(Table2, Table2[id] = _curID), [amount])
VAR _lowestAmount = MINX(FILTER(Table2, Table2[id] = _curID), [amount])
VAR _highestDate = MAXX(FILTER(Table2, Table2[id] = _curID && Table2[amount] = _highestAmount), [date])
VAR _lowestDate = MAXX(FILTER(Table2, Table2[id] = _curID && Table2[amount] = _lowestAmount), [date])
RETURN
DATEDIFF(_highestDate, _lowestDate, DAY)
I used variables as much as possible to illustrate the logic 🙂 Let me know if this solves your issue!
Excellent,
Thanks for bringing me a great and working solution in such a short timeframe, and for using variables to make it so readable for a newbie like myself.
/LarsM