Forum Discussion

ArvindJha's avatar
ArvindJha
Icon for Helper III rankHelper III
1 year ago

Create a single table many to many relationship using DAX

Hi all,

 

I want to create single table out of two tables with many to many relationship using DAX , LOOKUP and RELATED does not work.

For time being i have created relationship between them but it does not solve all my problems

PERSONVISITWEIGHT
AVisit1100
AVisit290
AVisit380
BVisit180
BVisit270
BVisit360
   
PERSONHEIGHT
A140
A139
B142

 

Thanks,

Arvind

12 Replies

  • olgad's avatar
    olgad
    Icon for Resident Rockstar rankResident Rockstar

    Cant you just merge them in the Power Query? full outer join? 

    Or alternatevely,

    CombinedTable =
    SELECTCOLUMNS(
    FILTER(
    CROSSJOIN('Visits', 'Height'),
    'Visits'[PERSON] = 'Height'[PERSON]
    ),
    "PERSON", 'Visits'[PERSON],
    "VISIT", 'Visits'[VISIT],
    "WEIGHT", 'Visits'[WEIGHT],
    "HEIGHT", 'Height'[HEIGHT]
    )

     

     

    • ArvindJha's avatar
      ArvindJha
      Icon for Helper III rankHelper III

      Hello Olgad , it works i needed one input , here i just shared sample data with only 4 columns but in reality i have 20+ columns , so do i need to write 20 lines (one for each column) or is there a way it takes all columns automatically using * or something that would be helphul in such scenarios

      • ArvindJha's avatar
        ArvindJha
        Icon for Helper III rankHelper III

        Hello Olgad , thanks for the response also to your question on join why i am not taking join is that there are some transformations which is making m-query very slow so i doing them in DAX which is fast and then unable to move back to m-query as those newly created tables using DAX are not visible in m-query

  • ArvindJha 

    Create a New Table

    CombinedTable =
    SUMMARIZE(
    'VISIT_TABLE',
    'VISIT_TABLE'[PERSON],
    'VISIT_TABLE'[VISIT],
    'VISIT_TABLE'[WEIGHT],
    "HEIGHT",
    CALCULATE(
    MAX('HEIGHT_TABLE'[HEIGHT]),
    TREATAS(
    VALUES('VISIT_TABLE'[PERSON]),
    'HEIGHT_TABLE'[PERSON]
    )
    )
    )

    💌 If this helped, a Kudos 👍 or Solution mark would be great! 🎉
    Cheers,
    Kedar
    Connect on LinkedIn

    • ArvindJha's avatar
      ArvindJha
      Icon for Helper III rankHelper III

      Thanks for the response Kedar , output is as below , it takes only the max height 140 and ignores 139 for Person A which is not the expected outcome

       

  • olgad's avatar
    olgad
    Icon for Resident Rockstar rankResident Rockstar

    Try this, but this is inner join

     

    CombinedTable =
    NATURALINNERJOIN('Visits', 'Height')

    • ArvindJha's avatar
      ArvindJha
      Icon for Helper III rankHelper III

      Hi Olgad , thanks for response , it doesn't work 

       

      • olgad's avatar
        olgad
        Icon for Resident Rockstar rankResident Rockstar

        Person shall be the common column, check if it is the same datatype sometimes when one is string/text and the other is 'any' or different spelling person Person or there is a space in the name of one then it doesnt work

  • olgad's avatar
    olgad
    Icon for Resident Rockstar rankResident Rockstar

    ArvindJha 

    FILTER(
    CROSSJOIN('Visits', 'Height'),
    'Visits'[PERSON] = 'Height'[PERSON]
    )

    • ArvindJha's avatar
      ArvindJha
      Icon for Helper III rankHelper III

      Hi Olgad , thanks for the response , it gives the below message

      The Column with the name of 'PERSON' already exists in the 'Table' Table.

      It is because PERSON is present in both tables?

      • olgad's avatar
        olgad
        Icon for Resident Rockstar rankResident Rockstar

        Yes, please rename it first and check if it works, are there any other columns with the same name in both tables? In this case they will give you the same problem.

        When we do selectcolumns, we specifically say which columns to take, but when not, there cannot be the columns with the same name. In Power Query such columns will automatically get Person.1 name