Forum Discussion
MAX Prior Period based on LastDate return value
- Anonymous7 years ago
OK. Here's a solution:
You have to always know what you're slicing by. You always should slice by the attributes of dimensions, not those of the fact table. The above works because Year comes from DimDates, not your fact table. I'd suggest you hide all the attributes of the fact table save the measures.
Here are the measures you see above:
Darek - Most Recent Date = var __onlyOneTickerVisible = HASONEFILTER( DimBenchmark[Benchmark Ticker] ) var __result = MAX( Benchmarks_PowerBI[Date] ) return if( __onlyOneTickerVisible, __result )Darek - Most Recent Date Shifted = var __onlyOneTickerVisible = HASONEFILTER( DimBenchmark[Benchmark Ticker] ) var __oneShiftVisible = HASONEFILTER( 'Shift From Base Year'[Shift in Years] ) var __numOfYearsBack = SELECTEDVALUE( 'Shift From Base Year'[Shift] ) var __result = CALCULATE( [Darek - Most Recent Date], DATEADD( DimDate[Date], -__numOfYearsBack, YEAR ) ) return if( __onlyOneTickerVisible && __oneShiftVisible, __result )Darek - Most Recent Price = var __onlyOneTickerVisible = HASONEFILTER( DimBenchmark[Benchmark Ticker] )
-- In the current date context get the latest price visible var __result = LASTNONBLANK( Benchmarks_PowerBI[ Price ], Benchmarks_PowerBI[ Price ] ) return if( __onlyOneTickerVisible, __result )Darek - Most Recent Price Shifted = var __onlyOneTickerVisible = HASONEFILTER( DimBenchmark[Benchmark Ticker] ) var __oneShiftVisible = HASONEFILTER( 'Shift From Base Year'[Shift in Years] ) var __shiftInYears = SELECTEDVALUE( 'Shift From Base Year'[Shift] ) var __price = CALCULATE( [Darek - Most Recent Price], DATEADD( DimDate[Date], -__shiftInYears, YEAR ) ) return if( __onlyOneTickerVisible && __oneShiftVisible, __price )All the other measures can be deleted from your file.
Now, here's what you have to do:
- Mark your DimDate table as DATE TABLE.
- Create a table that will hold shifts.
1. Here's how you mark a table as date table. Go to Modeling > Calendars and while your DimTable is displayed in the table view mode, click on Mark as Date Table.
2. Create a table like this:
Hide Shift (it's for calculation only) and leave the other one to be used on a slicer. Do not connect the table to anything else. It's a parameter table.
Never, ever select dates from your fact table. Only from your Date dimension. Then everything will work.
By the way, if you put quarters (from your Date table) in the slicer for Years, it should also work. Basically, for any selection of attribute in the DimDate dimension, it should work. Of course, on condition that I've understood the logic correctly. But you've got a good start, so you might want to adjust it to your needs if it acts up :)
Best
Darek
Please let me know if this link does not work:
https://onedrive.live.com/?authkey=%21ANQYX5UCHJaHyuU&id=E1D90AB70FBCB9DF%21805&cid=E1D90AB70FBCB9DF
You'll be able to see several examples of how it does not work.
Respectfully,
Robert
OK. Here's a solution:
You have to always know what you're slicing by. You always should slice by the attributes of dimensions, not those of the fact table. The above works because Year comes from DimDates, not your fact table. I'd suggest you hide all the attributes of the fact table save the measures.
Here are the measures you see above:
Darek - Most Recent Date =
var __onlyOneTickerVisible = HASONEFILTER( DimBenchmark[Benchmark Ticker] )
var __result = MAX( Benchmarks_PowerBI[Date] )
return
if( __onlyOneTickerVisible, __result )Darek - Most Recent Date Shifted =
var __onlyOneTickerVisible = HASONEFILTER( DimBenchmark[Benchmark Ticker] )
var __oneShiftVisible = HASONEFILTER( 'Shift From Base Year'[Shift in Years] )
var __numOfYearsBack = SELECTEDVALUE( 'Shift From Base Year'[Shift] )
var __result =
CALCULATE(
[Darek - Most Recent Date],
DATEADD( DimDate[Date], -__numOfYearsBack, YEAR )
)
return
if( __onlyOneTickerVisible && __oneShiftVisible, __result )
Darek - Most Recent Price = var __onlyOneTickerVisible = HASONEFILTER( DimBenchmark[Benchmark Ticker] )
-- In the current date context get the latest price visible var __result = LASTNONBLANK( Benchmarks_PowerBI[ Price ], Benchmarks_PowerBI[ Price ] ) return if( __onlyOneTickerVisible, __result )
Darek - Most Recent Price Shifted =
var __onlyOneTickerVisible = HASONEFILTER( DimBenchmark[Benchmark Ticker] )
var __oneShiftVisible = HASONEFILTER( 'Shift From Base Year'[Shift in Years] )
var __shiftInYears = SELECTEDVALUE( 'Shift From Base Year'[Shift] )
var __price =
CALCULATE(
[Darek - Most Recent Price],
DATEADD( DimDate[Date], -__shiftInYears, YEAR )
)
return
if( __onlyOneTickerVisible && __oneShiftVisible, __price )All the other measures can be deleted from your file.
Now, here's what you have to do:
- Mark your DimDate table as DATE TABLE.
- Create a table that will hold shifts.
1. Here's how you mark a table as date table. Go to Modeling > Calendars and while your DimTable is displayed in the table view mode, click on Mark as Date Table.
2. Create a table like this:
Hide Shift (it's for calculation only) and leave the other one to be used on a slicer. Do not connect the table to anything else. It's a parameter table.
Never, ever select dates from your fact table. Only from your Date dimension. Then everything will work.
By the way, if you put quarters (from your Date table) in the slicer for Years, it should also work. Basically, for any selection of attribute in the DimDate dimension, it should work. Of course, on condition that I've understood the logic correctly. But you've got a good start, so you might want to adjust it to your needs if it acts up :)
Best
Darek
- Anonymous7 years agoNot applicable
Hi Darek,
I sincerely apprecaite your time and explanation, it will not only be helpful to myself but others whom are searching the boards for a solution as well.
Respectfully,
- Anonymous7 years agoNot applicable
There's a mistake in one of the formulas...
Please change it to this:
Darek - Most Recent Price = var __onlyOneTickerVisible = HASONEFILTER( DimBenchmark[Benchmark Ticker] ) var __mostRecentDate = [Darek - Most Recent Date] var __result = CALCULATE( VALUES( Benchmarks_PowerBI[ Price ] ), KEEPFILTERS( DimDate[Date] = __mostRecentDate ) ) return -- In the current date context get the latest price visible if( __onlyOneTickerVisible, __result )Sorry. We can't use LASTNONBLANK in this case.
Best
Darek