Skip to main content
cancel
Showing results for 
Search instead for 
Did you mean: 

Grow your Fabric skills and prepare for the DP-600 certification exam by completing the latest Microsoft Fabric challenge.

Reply
eschultz
Regular Visitor

Creating Relationship Within the Same Table? Power Query

Hello,

 

I know there must be a formula or something I can use to create this relationship but not sure how. I have two tables that I have a relationship between. (Goal ID is the relationship key). When I filter on Goal 23, Animal ID 46 does not come through because it does not have a Goal ID. However, you can see that the parent ID is 45. So through this hierarchy , the Goal ID should be 23. Can someone help me create this relationship? Is it a formula or some type of table I need to create? Thank you!

 

Goal IDGoal
23Increase Speed

 

Animal ID

Animal

Animal TierGoal IDParent Animal  

45

Dog

Adult23   
46PuppyBaby 45  
2 ACCEPTED SOLUTIONS
serpiva64
Super User
Super User

Hi, you can obtain this result

serpiva64_0-1677316388514.png

by merging on itself you table

serpiva64_1-1677316446462.png

and then expanding 

serpiva64_2-1677316480284.png

and sorting

serpiva64_3-1677316515726.png

here is the example

let
Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WMjFV0lFyyU8Hko4ppTklQNrIGEgoxeoAJc2ArIDSgoJKIO2UmASigAioJzYWAA==", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [AnimalID = _t, Animal = _t, AT = _t, GoalId = _t, #"Parent Animal" = _t]),
#"Merged Queries" = Table.NestedJoin(Source, {"Parent Animal"}, Source, {"AnimalID"}, "Source", JoinKind.LeftOuter),
#"Expanded Source" = Table.ExpandTableColumn(#"Merged Queries", "Source", {"GoalId"}, {"GoalId.1"}),
#"Sorted Rows" = Table.Sort(#"Expanded Source",{{"AnimalID", Order.Ascending}})
in
#"Sorted Rows"

 

If this post is useful to help you to solve your issue, consider giving the post a thumbs up and accepting it as a solution!

 

 

 

 

View solution in original post

Thank you!! So helpful

View solution in original post

2 REPLIES 2
serpiva64
Super User
Super User

Hi, you can obtain this result

serpiva64_0-1677316388514.png

by merging on itself you table

serpiva64_1-1677316446462.png

and then expanding 

serpiva64_2-1677316480284.png

and sorting

serpiva64_3-1677316515726.png

here is the example

let
Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WMjFV0lFyyU8Hko4ppTklQNrIGEgoxeoAJc2ArIDSgoJKIO2UmASigAioJzYWAA==", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [AnimalID = _t, Animal = _t, AT = _t, GoalId = _t, #"Parent Animal" = _t]),
#"Merged Queries" = Table.NestedJoin(Source, {"Parent Animal"}, Source, {"AnimalID"}, "Source", JoinKind.LeftOuter),
#"Expanded Source" = Table.ExpandTableColumn(#"Merged Queries", "Source", {"GoalId"}, {"GoalId.1"}),
#"Sorted Rows" = Table.Sort(#"Expanded Source",{{"AnimalID", Order.Ascending}})
in
#"Sorted Rows"

 

If this post is useful to help you to solve your issue, consider giving the post a thumbs up and accepting it as a solution!

 

 

 

 

Thank you!! So helpful

Helpful resources

Announcements
Europe Fabric Conference

Europe’s largest Microsoft Fabric Community Conference

Join the community in Stockholm for expert Microsoft Fabric learning including a very exciting keynote from Arun Ulag, Corporate Vice President, Azure Data.

RTI Forums Carousel3

New forum boards available in Real-Time Intelligence.

Ask questions in Eventhouse and KQL, Eventstream, and Reflex.

Top Solution Authors
Top Kudoed Authors