Forum Discussion

François's avatar
François
Icon for Helper I rankHelper I
6 years ago
Solved

Most recent values from another table

I have a table of products, and a table of prices. Prices table contains the product ID, as well as the date, and the type of product (purchase or sales)   I want to display a list of  Product ID...
  • Mariusz's avatar
    6 years ago

    Hi François 

     

    Try this.

    Latest purchase price of current year = 
    VAR __lastDate =   
    CALCULATE(
        MAX( 'Table'[Transaction Date] ),
        ALL( 'Table' ),
        VALUES( 'Table'[Product ID] )
    )
    RETURN 
    CALCULATE(
        SUM( 'Table'[Purchase Price] ),
        TREATAS( { __lastDate }, 'Calendar'[Date] )
    )

     

    Latest purchase price of last year = 
    VAR __year = YEAR( MAX( 'Table'[Transaction Date] ) )
    VAR __lastDate =   
    CALCULATE(
        MAX( 'Table'[Transaction Date] ),
        ALL( 'Table' ),
        VALUES( 'Table'[Product ID] ),
        'Calendar'[Year] < __year
    )
    RETURN 
    CALCULATE(
        SUM( 'Table'[Purchase Price] ),
        TREATAS( { __lastDate }, 'Calendar'[Date] )
    )

     

    Best Regards,
    Mariusz

    If this post helps, then please consider Accepting it as the solution.

    Please feel free to connect with me.
    LinkedIn

     

  • Mariusz's avatar
    Mariusz
    6 years ago

    Hi François 

     

    First I'm calculating the last date the product was purchased, and later using this date to get a price, I used SUM ( as you need to use an aggregation ) but you can use MAX or SELECTEDVALUE.

     

    With last year the same just excluding the current year.

     

    And yes you can replace the 'Calendar'[Date] with date in your table.

     

    To create the sales price you can just copy the code and create another Measure replacing Purchase column with Sales. 

     

    Please see the attached file with a solution.

     

    Best Regards,
    Mariusz

    If this post helps, then please consider Accepting it as the solution.

    Please feel free to connect with me.
    LinkedIn

     

    Best Regards,
    Mariusz

    If this post helps, then please consider Accepting it as the solution.

    Please feel free to connect with me.
    LinkedIn