Forum Discussion

Cary_Casey's avatar
Cary_Casey
Frequent Visitor
6 years ago
Solved

Extracing Column from foreign table with conditions

Hello,

 

There are two tables that do not have a direct relationship Result and supply. I am trying to extract column supply from table supply into result based on certain conditions.

 

Result

locationdateResultProduct
one11/15/2019Y1
one11/15/2019N2
two11/17/2019Y1
two11/17/2019Y2
three12/3/2019N1
three12/3/2019Y2
one1/5/2020Y1
one1/5/2020Y2
three1/5/2020Y1
three1/5/2020Y2

 

Supply

locationdatesupplyproduct
one10/11/2019A1
one10/10/2019A1
one10/5/2019D2
one10/6/2019D2
two11/15/2019C1
two11/12/2019C1
two11/10/2019C2
three12/1/2019A1
three11/30/2019A1
three11/29/2019A2
one12/30/2019B1
one12/31/2019B1
one1/1/2020B2

 

Desired outcome Result with supply column from supply

locationdateResultProductSupply
one11/15/2019Y1A
one11/15/2019N2D
two11/17/2019Y1C
two11/17/2019Y2C
three12/3/2019N1A
three12/3/2019Y2A
one1/5/2020Y1B
one1/5/2020Y2B
three1/5/2020Y1A
three1/5/2020Y2A

 

Establishing a many to many relationship on location ends up duplicating the rows in results table if there is more than one result in the supply column from table supply. The next attempt was the following DAX equation :

Supply = IF(Result[date] > Supply[date] && Result[location] = Supply[location], CALCULATE(MAX(Supply[supply]),FILTER(Supply,MAX(Supply[date]))), 0)
 
This does not work as Power BI can't find a single entry for each of the supply columns defined in the logical test(s) of the IF statement.
 
This goal is to get the most recent supply before the date of the Result table for a result for a location.
  • VasTg's avatar
    VasTg
    6 years ago

    Cary_Casey 

     

    I overlooked the output. Try this.

     

    Column = 
    VAR SUP_DATE = CALCULATE(MAX(Supply[date]),FILTER(ALL(Supply),Result[location]=Supply[location] && Result[Product]=Supply[product] && Supply[date]<=Result[date]))
    RETURN CALCULATE(MAX(Supply[supply]),FILTER(ALL(Supply),Result[location]=Supply[location] && Result[Product]=Supply[product] && Supply[date]=SUP_DATE))

     

     

    Mark it as a solution if it helps.

    Kudos are nice too.

     

7 Replies

  • VasTg's avatar
    VasTg
    Icon for Memorable Member rankMemorable Member

    Cary_Casey 

     

    Try this as a new column in Result table..

    Column = CALCULATE(MAX(Supply[supply]),FILTER(ALL(Supply),Result[location]=Supply[location] && Result[Product]=Supply[product] && Supply[date]<=Result[date]))

    Feel free to modify the conditions.

     

     

    If it helps, mark it as a solution

    Kudos are nice too

     

    • Pragati11's avatar
      Pragati11
      Icon for Super User rankSuper User

      Hi,

       

      One way to achieve this is create a table with column "LOCATION" having distinct values against this column. Then create a relationship between RESULT & LOCATION tables and SUPPLY and LOCATION tables.

      Further you can use DAX function "RELATED" to get the calculation done.

       

      Thanks.

      • Cary_Casey's avatar
        Cary_Casey
        Frequent Visitor

        Pragati11 

        I tried this, but it did not work as expected. The DAX RELATED functionality actually did not let me use the supply table as a related table. Thank you for your response though!

    • Cary_Casey's avatar
      Cary_Casey
      Frequent Visitor

      VasTg 

       

      After doing some experiments with this calculated column, the suggested DAX formula does not give the solution I was seeking.

       

      If you will notice in your screenshot, the fourth occurence of location one in the table should have supply point 'B' ,but the formula given results in 'D'. It takes the Max value of the filtered table. It is not taking into account that we want the Supply[supply] with the Supply[date] closest and before the date in result[date]. The current formula just gives the max supply point before the date which could easilly be 'D' or any other higher variable instead of the most recent supply point (the one we want ) 'B'. Do you know how we could take this context into account with our DAX formula?

       

      Column = CALCULATE(MAX(Supply[supply]),FILTER(ALL(Supply),Result[location]=Supply[location] && Result[Product]=Supply[product] 
      && Supply[date]<=Result[date]))

       

      • VasTg's avatar
        VasTg
        Icon for Memorable Member rankMemorable Member

        Cary_Casey 

         

        I overlooked the output. Try this.

         

        Column = 
        VAR SUP_DATE = CALCULATE(MAX(Supply[date]),FILTER(ALL(Supply),Result[location]=Supply[location] && Result[Product]=Supply[product] && Supply[date]<=Result[date]))
        RETURN CALCULATE(MAX(Supply[supply]),FILTER(ALL(Supply),Result[location]=Supply[location] && Result[Product]=Supply[product] && Supply[date]=SUP_DATE))

         

         

        Mark it as a solution if it helps.

        Kudos are nice too.