Forum Discussion

Anonymous's avatar
Anonymous
Not applicable
4 years ago
Solved

Help required-Value from another table on the bases effective date

Hi,

 

I have 2 tables, they are both join with product( many to many relationship).

Order 
Order NoOrderdateProduct 
101/01/2021A
202/01/2021B
301/02/2021A
402/02/2021B
502/02/2021A
603/02/2021B
725/02/2021A
826/02/2021A
907/02/2021B
1008/03/2021A
   

 

Cost  
Cost %EffectiveDateProduct
1.101/01/2021A
1.401/01/2021B
1.501/02/2021A
1.815/02/2021A
1.901/03/2021A
202/02/2021B
   

Need the result like this 

Order   
Order NoOrderdateProduct Cost%
101/01/2021A1.1
202/01/2021B1.4
301/02/2021A1.1
402/02/2021B2
502/02/2021A1.5
603/02/2021B2
725/02/2021A1.8
826/02/2021A1.8
907/02/2021B1.5
1008/03/2021A1.9

 

If write custom colum to get cost% in order table this its give error of multiple values

VCost% =LOOKUPVALUE('Product'[Cost%],'Product'[Product],'Order'[Product])
 
Or 
no result
Vcost% =CALCULATE(SELECTEDVALUE(Product[Cost%]),Orderdate>=MINX(Product,Product[EfffectiveDate])&& OrderDate <=MAXX(Product,Product[EffectiveDate]))
 OR
 I have tried this also but no result

Cost1% =
Var MinEffDate = CALCULATE(MINX('Cost','Cost'[EffectiveDate]),FILTER('Cost','Cost'[Product]=('Order'[Product])))
Var MaxEffDate = CALCULATE(MAXX('Cost','Cost'[EffectiveDate]),FILTER('Cost','Cost'[Product]=('Order'[Product])))
Return
CALCULATE(SELECTEDVALUE('Cost'[Cost%]),'Order'[Orderdate]>=MinEffDate &&'Order'[Orderdate] <=MaxEffDate,FILTER('Cost','Cost'[Product]=('Order'[Product])))
 
Can you please how can I achieve this result.
Thanks
 
 
  • Anonymous's avatar
    Anonymous
    4 years ago

    Hi Anonymous ,

     

    Try this code to create a calcualted column.

    Cost % = 
    VAR _MAXDATE_BEFORE =
        CALCULATE (
            MAX ( Cost[EffectiveDate] ),
            FILTER (
                Cost,
                AND (
                    Cost[Product] = EARLIER ( 'Order'[Product ] ),
                    Cost[EffectiveDate] <= EARLIER ( 'Order'[Orderdate] )
                )
            )
        )
    VAR _COST =
        CALCULATE (
            SUM ( Cost[Cost %] ),
            FILTER (
                Cost,
                AND (
                    Cost[Product] = EARLIER ( 'Order'[Product ] ),
                    Cost[EffectiveDate] = _MAXDATE_BEFORE
                )
            )
        )
    RETURN
        _COST

    Result is as below.

    You can download my sample file as below to learn more details.

     

    Best Regards,
    Rico Zhou

     

    If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

4 Replies

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

    Anonymous So my normal go to for an alternative to LOOKUPVALUE is simply MAXX(FILTER(...),...)

    • Anonymous's avatar
      Anonymous
      Not applicable

      If I write following code then its gives circular dependency error 
      Vcost% =CALCULATE(SELECTEDVALUE(Product[Cost%]),Orderdate>=MINX(Product,Product[EfffectiveDate])&& OrderDate <=MAXX(Product,Product[EffectiveDate]))

      • Anonymous's avatar
        Anonymous
        Not applicable

        Hi Anonymous ,

         

        Try this code to create a calcualted column.

        Cost % = 
        VAR _MAXDATE_BEFORE =
            CALCULATE (
                MAX ( Cost[EffectiveDate] ),
                FILTER (
                    Cost,
                    AND (
                        Cost[Product] = EARLIER ( 'Order'[Product ] ),
                        Cost[EffectiveDate] <= EARLIER ( 'Order'[Orderdate] )
                    )
                )
            )
        VAR _COST =
            CALCULATE (
                SUM ( Cost[Cost %] ),
                FILTER (
                    Cost,
                    AND (
                        Cost[Product] = EARLIER ( 'Order'[Product ] ),
                        Cost[EffectiveDate] = _MAXDATE_BEFORE
                    )
                )
            )
        RETURN
            _COST

        Result is as below.

        You can download my sample file as below to learn more details.

         

        Best Regards,
        Rico Zhou

         

        If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.