Forum Discussion

Anonymous's avatar
Anonymous
Not applicable
7 years ago
Solved

MAX Prior Period based on LastDate return value

Hi,   I have two measures which return the price based on the max date and both are working correctly.  What I am looking to do is similar to DateAdd but I don't believe DateAdd can be combined wit...
  • Anonymous's avatar
    Anonymous
    7 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:

    1. Mark your DimDate table as DATE TABLE.
    2. 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