Skip to main content
cancel
Showing results for 
Search instead for 
Did you mean: 

Register now to learn Fabric in free live sessions led by the best Microsoft experts. From Apr 16 to May 9, in English and Spanish.

Reply
Cary_Casey
Frequent Visitor

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.
1 ACCEPTED SOLUTION

@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))

 

image.png

 

Mark it as a solution if it helps.

Kudos are nice too.

 

Connect on LinkedIn

View solution in original post

7 REPLIES 7
VasTg
Memorable Member
Memorable 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.

 

image.png

 

If it helps, mark it as a solution

Kudos are nice too

 

Connect on LinkedIn

@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]))

 

@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))

 

image.png

 

Mark it as a solution if it helps.

Kudos are nice too.

 

Connect on LinkedIn

@VasTg 

 

Thank you very much. This helped immensely in my learning of DAX! This will help me with several other projects to isolate the correct data.

@Cary_Casey 

 

Glad it helped.

Connect on LinkedIn

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.

Best Regards,

Pragati Jain


MVP logo


LinkedIn | Twitter | Blog YouTube 

Did I answer your question? Mark my post as a solution! This will help others on the forum!

Appreciate your Kudos!!

Proud to be a Super User!!

@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!

Helpful resources

Announcements
Microsoft Fabric Learn Together

Microsoft Fabric Learn Together

Covering the world! 9:00-10:30 AM Sydney, 4:00-5:30 PM CET (Paris/Berlin), 7:00-8:30 PM Mexico City

PBI_APRIL_CAROUSEL1

Power BI Monthly Update - April 2024

Check out the April 2024 Power BI update to learn about new features.

April Fabric Community Update

Fabric Community Update - April 2024

Find out what's new and trending in the Fabric Community.