Get record instance in Power Query, excluding unique ID

Jon von der Heyden

MrExcel MVP, Moderator
Joined
Apr 6, 2004
Messages
10,912
Office Version
  1. 365
Platform
  1. Windows
Hi All

I am battling to find a solution to this and perhaps not asking the right question in Google as I still cannot seem to find the answer.

I have a dataset with many columns. One column denotes a unique ID for each row. The other column values are shared with many other records. What I want to do is get the instance # of each record based on all columns except the unique ID (as in that case each row would constitute a unique instance).

I can get the instance based on the common columns, but then I don't know how to pair these back with the original unique ID's.

For example:


Unique IDAttrib1Attrib2Attrib3Instance
1​
AX
10​
1​
2​
AY
15​
1​
3​
BX
20​
1​
4​
BX
20​
2​
5​
CY
30​
1​

All instances are unique except ID 4, which shares the same attributes as ID 3 (Attrib1, Attrib2, Attrib3).

It's the instance column that I am having a tough time figuring out. I can get it if I discard the Unique ID, but that is a critical key that I need to retain. Any ideas?

Many thanks
Jon
 

Excel Facts

Select all contiguous cells
Pressing Ctrl+* (asterisk) will select the "current region" - all contiguous cells in all directions.
Thinking out loud here, without Excel for the moment makes it a bit too hard, but let's try with...
Merge query on it's own on the attribute columns.
Fetch the position of unique ID in those tables.
 
Upvote 0
I have a dataset with many columns. One column denotes a unique ID for each row. The other column values are shared with many other records. What I want to do is get the instance # of each record based on all columns except the unique ID (as in that case each row would constitute a unique instance).

I can get the instance based on the common columns, but then I don't know how to pair these back with the original unique ID's.

For example:


Unique IDAttrib1Attrib2Attrib3Instance
1​
AX
10​
1​
2​
AY
15​
1​
3​
BX
20​
1​
4​
BX
20​
2​
5​
CY
30​
1​

All instances are unique except ID 4, which shares the same attributes as ID 3 (Attrib1, Attrib2, Attrib3).
Hi Jon. What is the result table that you want to get according to this sample data? How do you want the instance numbers and IDs to be listed for each unique row? Delimited in a column like below? Or embedded as list values for instances and IDs? (It is probably me, but a result table will help me to understand)

1687386863375.png
 
Upvote 0
Power Query:
let
    Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],
    AttribList = {"Attrib1", "Attrib2", "Attrib3"},
    tbl = Table.Group(Source, AttribList, {{"All", each _}}),
    tbl1 = Table.TransformColumns(tbl, {"All", each Table.AddIndexColumn(_, "Instance",1)}),
    tbl2 = Table.ExpandTableColumn(tbl1, "All", {"Unique ID", "Instance"}),
    Result = Table.ReorderColumns(tbl2,{"Unique ID"} & AttribList & {"Instance"})

in
    Result

delme.xlsx
ABCDEFGHIJK
1Unique IDAttrib1Attrib2Attrib3Unique IDAttrib1Attrib2Attrib3Instance
21AX101AX101
32AY152AY151
43BX203BX201
54BX204BX202
65CY305CY301
7
Sheet2
 
Upvote 1
Solution
JGordon11 - yes you nailed it - this is giving me the result I anticipate, and much more elegant than my failed concoction.

Thanks Suat also, and sorry for my poor question-asking. :)
 
Upvote 0
With Excel now, adding what I had in mind for reference only.

Power Query:
let
    Source = Excel.CurrentWorkbook(){[Name="data"]}[Content],
    Set_types = Table.TransformColumnTypes(Source,{{"Unique ID", type text}, {"Attrib1", type text}, {"Attrib2", type text}, {"Attrib3", type text}}),
    Self_join = Table.NestedJoin(Set_types, {"Attrib1", "Attrib2", "Attrib3"}, Set_types, {"Attrib1", "Attrib2", "Attrib3"}, "Sub", JoinKind.LeftOuter),
    Add_col_counter = Table.AddColumn(Self_join, "counter", each List.PositionOf([Sub][Unique ID], [Unique ID])+1),
    Remove_col_sub = Table.RemoveColumns(Add_col_counter,{"Sub"})
in
    Remove_col_sub
 
Upvote 0

Forum statistics

Threads
1,224,862
Messages
6,181,457
Members
453,042
Latest member
AbdelrahmanExcel

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top