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
cgkas
Helper V
Helper V

How to relate Id with column in 2 tables

Hello to all,

 

I have this table #1 with columns "Month" and corresponding "Id"

 

IdMonth
1Jan
2Feb
3Mar

 

And this table #2 with columns "Visitors", "Day" and "Month"

 

VisitorsDayMonth
1004Feb
1357Feb
40023Feb
9008Jan
12321Jan
34227Jan
78229Jan
32211Mar
13419Mar

 

I want to have a new column in Table #2 with the corresponding "Id" for each month and show sorted by "Id" and then by "Day" like below:

 

IdVisitorsDayMonth
19008Jan
112321Jan
134227Jan
178229Jan
21004Feb
21357Feb
240023Feb
332211Mar
313419Mar

 

May someone help me to do this please.

1 ACCEPTED SOLUTION
v-xuding-msft
Community Support
Community Support

Hi @cgkas ,

 

@HotChilli , His method is worked and convenient.  If you want to implement in Power Query. There is a way you can have a try.

 

1.Choose Home --> Merge Queries --> choose Left Outer

 

0.png

2.Append the table

1.jpg2.jpg

3.Reordered column Table1.Id and removed the column Table1.Month . Then you can get the table you want.

3.png

 

let
Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WMjQwUNJRMgFit9QkpVgdoIixKZBnjiRiAlZjZIwkZAkWsgBir8Q8iDawvJEhkpCxiRFIyBxJyNwCLGSJrMoIJGQI0uibWAR1AshBhpYwoVgA", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type text) meta [Serialized.Text = true]) in type table [Visitors = _t, Day = _t, Month = _t]),
#"Changed Type" = Table.TransformColumnTypes(Source,{{"Visitors", Int64.Type}, {"Day", Int64.Type}, {"Month", type text}}),
#"Merged Queries" = Table.NestedJoin(#"Changed Type", {"Month"}, Table1, {"Month"}, "Table1", JoinKind.LeftOuter),
#"Expanded Table1" = Table.ExpandTableColumn(#"Merged Queries", "Table1", {"Id", "Month"}, {"Table1.Id", "Table1.Month"}),
#"Reordered Columns" = Table.ReorderColumns(#"Expanded Table1",{"Table1.Id", "Visitors", "Day", "Month", "Table1.Month"}),
#"Sorted Rows" = Table.Sort(#"Reordered Columns",{{"Table1.Id", Order.Ascending}}),
#"Removed Columns" = Table.RemoveColumns(#"Sorted Rows",{"Table1.Month"})
in
#"Removed Columns"

 

Best Regards,

Xue Ding

 

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

 

Best Regards,
Xue Ding
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-xuding-msft
Community Support
Community Support

Hi @cgkas ,

 

@HotChilli , His method is worked and convenient.  If you want to implement in Power Query. There is a way you can have a try.

 

1.Choose Home --> Merge Queries --> choose Left Outer

 

0.png

2.Append the table

1.jpg2.jpg

3.Reordered column Table1.Id and removed the column Table1.Month . Then you can get the table you want.

3.png

 

let
Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WMjQwUNJRMgFit9QkpVgdoIixKZBnjiRiAlZjZIwkZAkWsgBir8Q8iDawvJEhkpCxiRFIyBxJyNwCLGSJrMoIJGQI0uibWAR1AshBhpYwoVgA", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type text) meta [Serialized.Text = true]) in type table [Visitors = _t, Day = _t, Month = _t]),
#"Changed Type" = Table.TransformColumnTypes(Source,{{"Visitors", Int64.Type}, {"Day", Int64.Type}, {"Month", type text}}),
#"Merged Queries" = Table.NestedJoin(#"Changed Type", {"Month"}, Table1, {"Month"}, "Table1", JoinKind.LeftOuter),
#"Expanded Table1" = Table.ExpandTableColumn(#"Merged Queries", "Table1", {"Id", "Month"}, {"Table1.Id", "Table1.Month"}),
#"Reordered Columns" = Table.ReorderColumns(#"Expanded Table1",{"Table1.Id", "Visitors", "Day", "Month", "Table1.Month"}),
#"Sorted Rows" = Table.Sort(#"Reordered Columns",{{"Table1.Id", Order.Ascending}}),
#"Removed Columns" = Table.RemoveColumns(#"Sorted Rows",{"Table1.Month"})
in
#"Removed Columns"

 

Best Regards,

Xue Ding

 

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

 

Best Regards,
Xue Ding
If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.
HotChilli
Super User
Super User

You need a relationship on Month (it should create automatically)

 

Then create the column in table2 as

Id = RELATED(Table1[Id]) 

Hi HotChili,

 

For Table1 I have this code:

let
    Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"Id", Int64.Type}, {"Month", type text}})
in
    #"Changed Type"

and for Table2 this code:

let
    Source = Excel.CurrentWorkbook(){[Name="Table2"]}[Content],
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"Visitors", Int64.Type}, {"Day", Int64.Type}, {"Month", type text}}),
    #"Added Custom" = Table.AddColumn(#"Changed Type", "Id", each RELATED(Table1[Id]))
in
    #"Added Custom"

I added the custim colum but I receive error in #"Added Custom" step.

Expression.Error: The name 'RELATED' wasn't recognized.  Make sure it's spelled correctly.

What is missing and what I´m doing wrong?

 

Thanks

You are in Power Query. 

I wrote you some DAX (which is used in powerbi)

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.