cancel
Showing results for 
Search instead for 
Did you mean: 
Reply
Highlighted
cgkas Regular Visitor
Regular Visitor

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

Accepted Solutions

Re: How to relate Id with column in 2 tables

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.

 

4 REPLIES 4
HotChilli Senior Member
Senior Member

Re: How to relate Id with column in 2 tables

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

 

Then create the column in table2 as

Id = RELATED(Table1[Id]) 
cgkas Regular Visitor
Regular Visitor

Re: How to relate Id with column in 2 tables

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

HotChilli Senior Member
Senior Member

Re: How to relate Id with column in 2 tables

You are in Power Query. 

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

Re: How to relate Id with column in 2 tables

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.