Forum Discussion
Get max value from related table
- Anonymous7 years ago
First of all, your statements are not clear. You say you want the amount on the latest month of the latest year.... and then you say something about list price. Where the heck do you have a list price column in your tables?
When using FILTER, you should always remember that if you use a measure in the filtering condition, the measure performs a context transition because each measure is wrapped in CALCULATE implicitly. You should understand how context transition works before you even start using (measures in) FILTERs.
Here's probably what you want:
[Latest Sale Year] = MAX( Sales[Year] ) [Latest Sale Month] = var __latestYear = [Latest Year] var __latestMonth = CALCULATE( MAX( Sales[Month] ), Sales[Year] = __latestYear ) return __latestMonth [Latest Amount] = var __latestMonth = [Latest Month] var __latestYear = [Latest Year] var __latestAmount = CALCULATE( MAX( Sales[Amount] ), Sales[Year] = __latestYear, Sales[Month] = __latestMonth ) return __latestAmount
If you have selected many items, you'll get the maximum amount for any of the items in the set that have the same latest year and latest month. If you want this measure(s) to work only when one item is visible, then you must guard your measures with HASONEVALUE or HASONEFILTER.
Best
Darek
First of all, your statements are not clear. You say you want the amount on the latest month of the latest year.... and then you say something about list price. Where the heck do you have a list price column in your tables?
When using FILTER, you should always remember that if you use a measure in the filtering condition, the measure performs a context transition because each measure is wrapped in CALCULATE implicitly. You should understand how context transition works before you even start using (measures in) FILTERs.
Here's probably what you want:
[Latest Sale Year] = MAX( Sales[Year] ) [Latest Sale Month] = var __latestYear = [Latest Year] var __latestMonth = CALCULATE( MAX( Sales[Month] ), Sales[Year] = __latestYear ) return __latestMonth [Latest Amount] = var __latestMonth = [Latest Month] var __latestYear = [Latest Year] var __latestAmount = CALCULATE( MAX( Sales[Amount] ), Sales[Year] = __latestYear, Sales[Month] = __latestMonth ) return __latestAmount
If you have selected many items, you'll get the maximum amount for any of the items in the set that have the same latest year and latest month. If you want this measure(s) to work only when one item is visible, then you must guard your measures with HASONEVALUE or HASONEFILTER.
Best
Darek
- Anonymous7 years agoNot applicable
Thank you so much, Darek. It works perfectly.
I'm noob in DAX.Best regards.