简体   繁体   中英

How to reference the correct value in a many to many relationship in Power BI?

I have two tables connected by Serial Number.

SAMPLES

SERIAL#  SMU  SAMPLE
001      52   GREEN
002      25   GREEN
001      124  YELLOW
003      41   RED
001      266  GREEN
001      280  GREEN

WARRANTY

SERIALl#   SMUSTART SMUEND  LIFE
001       1        100     1
002       5        105     1
003       1        100     1
001       101      200     2
001       201      300     3

I am trying to be able to create a slicer on LIFE that will show me only the SAMPLES where the SMU is within the SMUSTART and SMUEND range. I've tried pulling the LIFE column into the SAMPLES table, concatenating SERIAL# and LIFE and then connecting the tables on my new concatenated columns. But I was never able to get LIFE successfully brought over to the SAMPLES table and didn't know if I was even heading down the right path. Any and all help is appreciated. Thank you. Let me know if I need to clarify anything.

This is possible if you transform your tables.

Add a new column to the samples table that is a combination of the serial no and the SMU.

SAMPLES

SERIAL#  SMU  SAMPLE    SER_SMU
001      52   GREEN     001_52
002      25   GREEN     002_25
001      124  YELLOW    001_124
003      41   RED       003_41
001      266  GREEN     001_266
001      280  GREEN     001_280

The warrantly table you first have to "expand". Create a row for every possible SMU. And then add again a new column that is the combination of Serial no and SMU.

Like this:

SERIALl#    SMU LIFE    SER_SMU
001         1   1       001_1
001         2   1       001_2
...
001         100 1       001_100

This way you can join the tables on the SER_SMU.

What is your source of the data? Can you transform the tables when importing them, otherwise you have to do it with power query, which might be a bit challenging.

EDIT: example of SQL query:

SELECT s.serial, s.SMU, s.SAMPLE,w.LIFE
FROM samples s
LEFT JOIN warranty w
    ON s.serial = w.serial
    AND s.SMU BETWEEN w.SMUSTART AND w.SMUEND

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM