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
labuser1235
Helper IV
Helper IV

Lookup value by comparing two columns from two tables and show 3rd column as output

I would like to compare two columns from two different tables and get the price value in the calculated column. 

Let the two tables be Fruit Name Type and Price columns as Table 1 along with Date Fruit Name Type and Need price to look up from table 1 and add it in table 2. Relations between the tables are Furit Name (Active Relation)and Type. 

 

Can we achieve this by DAX as well ??? 

 

Fruit NameTypePrice
OrangeType-15
OrangeType-27
OrangeType-310
AppleType-13
AppleType-25
AppleType-38
GrapesType-12
GrapesType-24
GrapesType-36

 

DateFruit NameTypeLookvalue for price
1/31/2020OrangeType-2 
1/31/2020Apple Type-3 
1/31/2020GrapesType-1 
1/31/2020OrangeType-1 
1/31/2020OrangeType-3 
1/30/2020Apple Type-1 
1/30/2020GrapesType-2 
1/30/2020GrapesType-3 
1/30/2020OrangesType-1 
1/30/2020OrangesType-1 

 

Please help... I have tried multiple ways may be I was doing it worng....please help me with the DAX formula or Calulated column formula to get the result. 

 

1 ACCEPTED SOLUTION
amitchandak
Super User
Super User

Try

new column in table two = maxx(filter(table1, table1[type]=table2[type] && table1[Fruit Name]=table2[Fruit Name]),table1[price])

 

Appreciate your Kudos. In case, this is the solution you are looking for, mark it as the Solution. In case it does not help, please provide additional information and mark me with @
Thanks. My Recent Blog -
Winner-Topper-on-Map-How-to-Color-States-on-a-Map-with-Winners , HR-Analytics-Active-Employee-Hire-and-Termination-trend
Power-BI-Working-with-Non-Standard-Time-Periods And Comparing-Data-Across-Date-Ranges

Connect on Linkedin

View solution in original post

14 REPLIES 14
labuser1235
Helper IV
Helper IV

If I add another column which tells only the price of respective years then how should I match it with repective to years. 

Please help with these @amitchandak @TomMartens 

 

Fruit NameType2020 - Price2019 - Price
OrangeType-154
OrangeType-276
OrangeType-3108
AppleType-132
AppleType-254
AppleType-387
GrapesType-122
GrapesType-243
GrapesType-366

 

 

DateFruit NameTypeLookvalue for price
1/31/2020OrangeType-2 
1/31/2020Apple Type-3 
1/31/2020GrapesType-1 
1/31/2020OrangeType-1 
1/31/2020OrangeType-3 
1/30/2020Apple Type-1 
1/30/2020GrapesType-2 
1/30/2020GrapesType-3 
1/30/2020OrangesType-1 
1/30/2020OrangesType-1 
12/30/2019Apple Type-3 
12/30/2019GrapesType-1 
12/30/2019OrangeType-1 
12/30/2019OrangeType-3 
12/31/2019Apple Type-1 
12/31/2019GrapesType-2 
12/31/2019GrapesType-3 
12/31/2019OrangesType-1 
12/31/2019OrangeType-2 
12/31/2019Apple Type-3 

 

 

Earlier will not allow any function inside. So create year using dates

 

Year1 = year(Date)

 

now you can add clause like

Year1 = earlier(Year1) // This year

Year1 = earlier(Year1)-1 // This vs Last

@amitchandak  I have understood that adding year column would bring only the year. However, I am only confused because the price in table1 tells only for that year which is in column and the year in table2 is present in Rows. How should I match and get the exact price. Please help me I am really consfused.  

Can you share the raw data structure again with output structure? The one you shared last not making clear what is input and what is output

@amitchandak Please check the raw data again. I am expecting output as in Table2 last column which I have added.

 

Input Table1

Fruit NameType2020 - Price2019 - Price
OrangeType-154
OrangeType-276
OrangeType-3108
AppleType-132
AppleType-254
AppleType-387
GrapesType-122
GrapesType-243
GrapesType-366

 

Expected Output in Table2: Last Column -- Output- Lookup Price (Column Name)

DateYearFruit NameTypeOutput - Lookup Price
1/31/20202020OrangeType-27
1/31/20202020Apple Type-38
1/31/20202020GrapesType-12
1/31/20202020OrangeType-15
1/31/20202020OrangeType-310
1/30/20202020Apple Type-13
1/30/20202020GrapesType-24
1/30/20202020GrapesType-36
1/30/20202020OrangesType-15
1/30/20202020OrangesType-15
12/30/20192019Apple Type-37
12/30/20192019GrapesType-12
12/30/20192019OrangeType-14
12/30/20192019OrangeType-38
12/31/20192019Apple Type-12
12/31/20192019GrapesType-23
12/31/20192019GrapesType-36
12/31/20192019OrangesType-14
12/31/20192019OrangeType-26
12/31/20192019Apple Type-37

 

 

 

 

Check -https://www.dropbox.com/s/d4pmpfein2h3nuv/differentyearprice.pbix?dl=0

 

Not sure how you want to get different dates when you have data at the yearly level.

 

You can rename and change datatype in new table

@amitchandak You're a Genuis. Just the kind of data I was looking for.

 

PFB the screenshots for reference

 

Differentyearprice -1.PNGDifferentyearprice -2.PNG

I understood that I have to add another column deriving year from the date. 

 

My doubt is how will I lookup with the below DAX expression in this case as the column name gives only the price of that specific year in Table1 and the year is in rows in Table2. Please brief on this @amitchandak 

 

new column in table two = maxx(filter(table1, table1[type]=table2[type] && table1[Fruit Name]=table2[Fruit Name]),table1[price])

 

 

Table1

Fruit NameType2020 - Price2019 - Price
OrangeType-154
OrangeType-276
OrangeType-3108
AppleType-132
AppleType-254
AppleType-387
GrapesType-122
GrapesType-243
GrapesType-366

 

Table2

DateYearFruit NameTypeLookvalue for price
1/31/20202020OrangeType-2 
1/31/20202020Apple Type-3 
1/31/20202020GrapesType-1 
1/31/20202020OrangeType-1 
1/31/20202020OrangeType-3 
1/30/20202020Apple Type-1 
1/30/20202020GrapesType-2 
1/30/20202020GrapesType-3 
1/30/20202020OrangesType-1 
1/30/20202020OrangesType-1 
12/30/20192019Apple Type-3 
12/30/20192019GrapesType-1 
12/30/20192019OrangeType-1 
12/30/20192019OrangeType-3 
12/31/20192019Apple Type-1 
12/31/20192019GrapesType-2 
12/31/20192019GrapesType-3 
12/31/20192019OrangesType-1 
12/31/20192019OrangeType-2 
12/31/20192019Apple Type-3 

 

amitchandak
Super User
Super User

Try

new column in table two = maxx(filter(table1, table1[type]=table2[type] && table1[Fruit Name]=table2[Fruit Name]),table1[price])

 

Appreciate your Kudos. In case, this is the solution you are looking for, mark it as the Solution. In case it does not help, please provide additional information and mark me with @
Thanks. My Recent Blog -
Winner-Topper-on-Map-How-to-Color-States-on-a-Map-with-Winners , HR-Analytics-Active-Employee-Hire-and-Termination-trend
Power-BI-Working-with-Non-Standard-Time-Periods And Comparing-Data-Across-Date-Ranges

Connect on Linkedin

Dear @amitchandak, this is a brilliantl!!!   Could you help me modify slightly, to add a search function to the second column? Specifically 

> The first columns have exact matches

> The second columns, I need to search  to see whether Table 1 term is within Table 2 column

 

What would you suggest? 

This worked like charm Thanks alot @amitchandak . 

TomMartens
Super User
Super User

Hey @labuser1235 ,

 

you can use the DAX formula LOOKUPVALUE inside a calculated column, in my example, I call the table that contains the price column sourcetable and the other table targetable. The statement will look like this:

 

price =
lookupvalue(
'<sourcetable>'[Price],
'<sourcetable>'[Fruit Name], 'targettable>'[FruitName],
'<sourcetable>'[Type], 'targettable>'[Type]
)

 

Hopefully, this provides what you are looking for.

 

Regards,

Tom



Did I answer your question? Mark my post as a solution, this will help others!

Proud to be a Super User!
I accept Kudos 😉
Hamburg, Germany

Short and sweet!

It throws "A table of multiple values was supplied where a single value was expected error", when i actually work on larger records of data. 

 

My data contains 21K records. Please help @TomMartens 

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.