Forum Discussion
Get data from column from unrelated tables
Is there a way to get data from table B based on match between columns from A and B even thought tables are unrelated. Result is in table C. Is there are duplicates in table B ignore them and take value from "Letter" on the first encounter.
Hi,
Please check the below picture and the attached pbix file.
It is for creating a new table by DAX.
TABLE C = ADDCOLUMNS ( 'Table A', "@letter", MINX ( FILTER ( 'Table B', 'Table B'[id] = 'Table A'[id] && 'Table B'[code] = 'Table A'[code] ), 'Table B'[letter] ) )
8 Replies
- Jihwan_Kim
Super User
Hi,
Please check the below picture and the attached pbix file.
It is for creating a new table by DAX.
TABLE C = ADDCOLUMNS ( 'Table A', "@letter", MINX ( FILTER ( 'Table B', 'Table B'[id] = 'Table A'[id] && 'Table B'[code] = 'Table A'[code] ), 'Table B'[letter] ) ) - stribor45
Post Prodigy
Would you mind explaining what parts of this code executes first please. I am just trying to understand how this works. Does the table first gets filtered based on these conditions?
- Jihwan_Kim
Super User
Hi,
I tried to add comments into the code like below.
TABLE C = ADDCOLUMNS ( 'Table A', // Use the ADDCOLUMNS function to create a new calculated column within 'Table A' "@letter", // Name the calculated column as "@letter" // Define the expression for the "@letter" column MINX ( // Use the MINX function to return the minimum value of the expression specified // This ensures we get the smallest (alphabetically or numerically) 'letter' value FILTER ( // Use the FILTER function to create a subset of 'Table B' // The subset includes rows where both conditions below are true: 'Table B', 'Table B'[id] = 'Table A'[id] // Condition 1: Match the 'id' in 'Table B' with the 'id' in 'Table A' && 'Table B'[code] = 'Table A'[code] // Condition 2: Match the 'code' in 'Table B' with the 'code' in 'Table A' ), // The column from 'Table B' to evaluate: 'Table B'[letter] // This is the column from which we want to extract the minimum value 'Table B'[letter] ) )- stribor45
Post Prodigy
Thank you!!!!
If first table A had 3 more columns is there a way to somoehow only take first three (id, code and name) and then do same calculations as before?
- stribor45
Post Prodigy
I was more hoping to use DAX as I was aware on how to do this in PQ. Also I was hoping for the table to look like this as I want to take only some columns from table A. what if table had 4 or more columns (example below) and we wanted to use only first three columns in this calculation to get same output. Is there a way for this whole code to get wrapped up with selectcolumns?
id code name Day 111 222 John Monday 111 333 John Tuesday 333 222 Jim Friday 444 333 Elaine Monday
- v-hashadapu
Community Support
Hi stribor45 , Thank you for reaching out to the Microsoft Community Forum.
Please let us know if your issue is solved. If it is, consider marking the answers that helped 'Accept as Solution', so others with similar queries can find them easily. If not, please share the details.
Thank you.