Forum Discussion

Anonymous's avatar
Anonymous
Not applicable
6 years ago
Solved

Lookupvalue on date period

Hi,

 

I have been searching for ways to lookup a value (for instance a costprice) from another table based on a time interval. 

 

Sales Table                                                                 PriceTable

Date                    Product                                           Product               StartDate              EndDate            Cost

2019-01-01               A                                                     A                   2019-01-01           2019-01-31         5

2019-01-01               B                                                     B                   2019-01-01            2019-01-31         8

2019-01-02               A                                                     A                   2019-02-01            2019-02-28        7

2019-02-01               A                                                     

 

 

Desired outcome

SalesTable

Date                    Product         Price                                  

2019-01-01               A                 5                           

2019-01-01               B                 8                               

2019-01-02               A                 5                               

2019-02-01               A                 7   

 

I have no problems getting the price for the first of every month as those dates correspond with each other. But what I would like is for the sale on the second to lookup the corresponding cost price based on the price in the interval 2019-01-01 - 2019-01-31. How does one best set this up? Some use of minx/maxx? TopN?

 

I managed to solve it by creating a helper table listing all dates between the intervall and adding the cost price on each date for each item. But this table grows quickly, and the sollution is not very neat. I am sure there are better and faster ways to do this.

 

I don't mind if the sollution is given as a calculated column or a measure. Any way that is better than the list of all possible dates/items/costs would work.

 

Thank you,

Kind regards,

Patrik

 

  • Hi Anonymous 

    try new measure in the Sales table

    Measure = calculate(MAX('Price Table'[Cost]);
    FILTER(ALL('Price Table');
    'Price Table'[Product]=Selectedvalue('Sales Table'[Product]) &&
    'Price Table'[StartDate]<=selectedvalue('Sales Table'[Date]) && 
    'Price Table'[EndDate]>=selectedvalue('Sales Table'[Date])
    )
    )

    do not hesitate to give a kudo to useful posts and mark solutions as solution
    Linkedin

2 Replies

  • az38's avatar
    az38
    Icon for Community Champion rankCommunity Champion

    Hi Anonymous 

    try new measure in the Sales table

    Measure = calculate(MAX('Price Table'[Cost]);
    FILTER(ALL('Price Table');
    'Price Table'[Product]=Selectedvalue('Sales Table'[Product]) &&
    'Price Table'[StartDate]<=selectedvalue('Sales Table'[Date]) && 
    'Price Table'[EndDate]>=selectedvalue('Sales Table'[Date])
    )
    )

    do not hesitate to give a kudo to useful posts and mark solutions as solution
    Linkedin

    • Anonymous's avatar
      Anonymous
      Not applicable

      Thank you very much az38 .

       

      Tried it out this morning, and so far it looks to be doing what I want it too, and could even add some more filtering I needed. Very clean sollution, big thanks!