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
HB13
Helper I
Helper I

Merging queries with a many to many relationship

Hello PBI People!


I am struggling with a merge task and I'm hoping for some clarity and assistance on the issue.

I have two tables, Policies and Risks
Policies records the policyId every single day that a policy remains active
Risks contains info about different risk vehicles linked to a policy (one policy can have many vehicles)

I have included a screenshot of an example of the two tables as well as the merged table I am trying to create. I basically want to map each riskvehicleid to every instance of the policyid in the policies table.
I am not too sure how to go about it Power BI, my last attempt only pulled one row into the merged table. 

HB13_0-1659601975826.png

 

 


Please advise!
Thank you in advance






1 ACCEPTED SOLUTION
Bubble4502
Resolver III
Resolver III

Hi @HB13 ,

 

You can achieve this goal in power query:

First, choos merge queries as new:

Bubble4502_0-1660124445172.png

Then select table and choose inner join:

Bubble4502_1-1660124508547.png

 

Expand risk and remove useless column:

Bubble4502_2-1660124568929.png

Final output:

Bubble4502_3-1660124602254.png

Here is the M code:

let
    Source = Table.NestedJoin(Policies, {"policyid"}, Risk, {"policyid"}, "Risk", JoinKind.Inner),
    #"Expanded Risk" = Table.ExpandTableColumn(Source, "Risk", {"policyid", "riskvehicleid", "vehiclemanufactureyear"}, {"Risk.policyid", "Risk.riskvehicleid", "Risk.vehiclemanufactureyear"}),
    #"Removed Columns" = Table.RemoveColumns(#"Expanded Risk",{"Risk.policyid"})
in
    #"Removed Columns"

 

Kind Regards,

Bubble

If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

View solution in original post

4 REPLIES 4
v-jianboli-msft
Community Support
Community Support

Hi @HB13 ,

 

Could you tell me if your problem has been solved? If it is, kindly Accept it as the solution. More people will benefit from it. Or if you are still confused about it, please provide me with more details about your table and your problem or share me with your pbix file after removing sensitive data.

 

Refer to:

How to provide sample data in the Power BI Forum

How to Get Your Question Answered Quickly

 

Best Regards,

Jianbo Li

If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

 

Bubble4502
Resolver III
Resolver III

Hi @HB13 ,

 

You can achieve this goal in power query:

First, choos merge queries as new:

Bubble4502_0-1660124445172.png

Then select table and choose inner join:

Bubble4502_1-1660124508547.png

 

Expand risk and remove useless column:

Bubble4502_2-1660124568929.png

Final output:

Bubble4502_3-1660124602254.png

Here is the M code:

let
    Source = Table.NestedJoin(Policies, {"policyid"}, Risk, {"policyid"}, "Risk", JoinKind.Inner),
    #"Expanded Risk" = Table.ExpandTableColumn(Source, "Risk", {"policyid", "riskvehicleid", "vehiclemanufactureyear"}, {"Risk.policyid", "Risk.riskvehicleid", "Risk.vehiclemanufactureyear"}),
    #"Removed Columns" = Table.RemoveColumns(#"Expanded Risk",{"Risk.policyid"})
in
    #"Removed Columns"

 

Kind Regards,

Bubble

If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

amitchandak
Super User
Super User

@HB13 , Based on what I got. Create a combine column or policy and date in both tables and use that in merge

Are you saying combine date and policyid in a column? There isn't a date column in the Risks table to do so

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.