Forum Discussion
Calculate the Latest Price
| Row No. | Zone | Item Code | Country | Year | Price | Benchmark Price (BP) |
| 1 | West EUR | A | Belgium | 2015 | - | - |
| 2 | West EUR | A | Belgium | 2016 | 102 | - |
| 3 | West EUR | A | Belgium | 2017 | 104 | 102 |
| 4 | West EUR | A | Belgium | 2018 | 106 | 104 |
| 5 | West EUR | A | Belgium | 2020 | 110 | 106 |
| 6 | West EUR | A | Netherlands | 2019 | 120 | 106 |
| 7 | West EUR | A | Netherlands | 2020 | 122 | 120 |
| 8 | West EUR | A | Netherlands | 2020 | 124 | 120 |
| 9 | West EUR | A | Netherlands | 2021 | 124 | 124 |
Above table (from column "Row No." to column "Price") is a small subset of my dataset. I need help to calculate the "Benchmark Price" by using a Calculated Column (There are millions of different Item Codes in my dataset and a Measure really slows things down)
This is the logic for Benchmark Price (BP) -
BP is the "Latest Price within the last 3 years. This should be preferably from the same country but if not then at least from the same zone." If a purchase has been made within the last 3 years in the same country, then that price will be the BP. If a purchase has not been made within the last 3 years in the same country, then see if it has been purchased in at least the same zone. If yes, then that price will be BP (even though it might be from another country).
Explaining with examples-
Row No. 2 - There is no BP as item was not purchased in 2015 in Belgium and data before 2015 is not available. Item not purchased in 2015 in Netherlands (same zone) as well.
Row No. 6 - The item was not purchased in Netherlands before 2019. However, we have a purchase made in the same Zone i.e. in Belgium. So BP will be the 2018 Belgium Price. (If 2018 Belgium Price wasn't available, then 2017 Belgium Price would have been the BP. If 2017 Belgium Price was also not available then 2016 Belgium Price would have been the BP. Only if none of 2018, 2017, 2016 prices were available in the entire Zone, then there would have been no BP. Even if 2015 Prices were available they would not have worked as they would be beyond the 3 year time frame.)
Row No. 9 - We have 2 prices for 2020 in the same country i.e. Netherlands. In such cases choose the higher price. 124 > 122 so BP is 124.
I have also done some color-coding to make it easier to understand where the BP is coming from.
Please let me know if there are any questions.
Would appreciate all help. Thanks in advance!
Try this:
BP = VAR CurrYear = Prices[Year] VAR MinYear = CurrYear - 3 VAR LastZoneYear = CALCULATE ( MAX ( Prices[Year] ), ALLEXCEPT ( Prices, Prices[Item Code], Prices[Zone] ), Prices[Year] > MinYear, Prices[Year] < CurrYear ) VAR LastCountryYear = CALCULATE ( MAX ( Prices[Year] ), ALLEXCEPT ( Prices, Prices[Item Code], Prices[Country] ), Prices[Year] > MinYear, Prices[Year] < CurrYear ) VAR LastZonePrice = CALCULATE ( MAX ( Prices[Price] ), ALLEXCEPT ( Prices, Prices[Item Code], Prices[Zone] ), Prices[Year] = LastZoneYear ) VAR LastCountryPrice = CALCULATE ( MAX ( Prices[Price] ), ALLEXCEPT ( Prices, Prices[Item Code], Prices[Country] ), Prices[Year] = LastCountryYear ) RETURN IF ( ISBLANK ( LastCountryPrice ), LastZonePrice, LastCountryPrice )
2 Replies
- AlexisOlson
Super User
Try this:
BP = VAR CurrYear = Prices[Year] VAR MinYear = CurrYear - 3 VAR LastZoneYear = CALCULATE ( MAX ( Prices[Year] ), ALLEXCEPT ( Prices, Prices[Item Code], Prices[Zone] ), Prices[Year] > MinYear, Prices[Year] < CurrYear ) VAR LastCountryYear = CALCULATE ( MAX ( Prices[Year] ), ALLEXCEPT ( Prices, Prices[Item Code], Prices[Country] ), Prices[Year] > MinYear, Prices[Year] < CurrYear ) VAR LastZonePrice = CALCULATE ( MAX ( Prices[Price] ), ALLEXCEPT ( Prices, Prices[Item Code], Prices[Zone] ), Prices[Year] = LastZoneYear ) VAR LastCountryPrice = CALCULATE ( MAX ( Prices[Price] ), ALLEXCEPT ( Prices, Prices[Item Code], Prices[Country] ), Prices[Year] = LastCountryYear ) RETURN IF ( ISBLANK ( LastCountryPrice ), LastZonePrice, LastCountryPrice )- AnonymousNot applicable
Thanks a lot Alexis!