Forum Discussion

witbi's avatar
witbi
Helper I
3 years ago
Solved

Create an intersection table from reference and transaction tables

Hello,


I have a reference table (a) with values that I would like to look up in a transaction table (b) across multiple columns "Location" and "Interests" and generate a new intersection table (c).

The reference table has text.startwith values like "cycl" so I can match "cycle" or "cycling". Duplicated relationships are ok as I can remove them later.

Any help would be most appreciated! Thank you and happy new year.

a) Reference table

ID_categoryValue
1UK
2Canada
3Australia
4cycl
5travel
6read
7theatre
8cook
9sport
10garden

 
b) transaction table

ID_personNameLocationInterests
1AlexLondon, UKTraveling, going to the theatre, gardening are my weekend pleasures
2BevToronto, CanadaI'm a world traveler. Most recently I cycled the UK on a 20 day food and cooking tour
3CathSydney, AusstraliaI'm an avid sports fan. I belong to a sports travel club that's visited Canada for ice hockey and Europe for cycling
4DeanGlasgow, UKI started a book club that reads 10 books a month. We're all avid readers! 
5EdMontreal, CanadaI get the most joy from cooking with the freshest ingredients direct from my garden


c) intersection table (new table)

ID_categoryID_personColumn
11Location
15Interests
110Interests
22Location
25Interests
21Interests
28Interests
33Location
39Interests
32Interests
34Interests
41Location
46Interests
52Location
58Interests
510Interests
  • = let a=Table.Buffer(Table.UnpivotOtherColumns(TransactionTable,{"ID_person","Name"},"Attributes","Value")) in Table.Sort(#table({"ID_person","ID_category","Custom"},List.TransformMany(Table.ToRows(Table.AddColumn(ReferenceTable,"n",(x)=>Table.SelectRows(a,each Text.Contains([Value],x[Value],Comparer.OrdinalIgnoreCase)))),each Table.ToRows(_{2}),(x,y)=>{y{0},x{0},y{2}})),"ID_person")

  • let me guess

    = let a=Table.Buffer(TransactionTable) in Table.Sort(#table({"ID_person","ID_category"},List.TransformMany(Table.ToRows(Table.AddColumn(ReferenceTable,"n",(x)=>Table.SelectRows(a,each not List.Contains(List.Skip(Record.ToList(_),2),x[Value],(m,n)=>Text.Contains(m,n,Comparer.OrdinalIgnoreCase))))),each Table.ToRows(_{2}),(x,y)=>{y{0},x{0}})),"ID_person")

5 Replies

  • wdx223_Daniel's avatar
    wdx223_Daniel
    Community Champion

    = let a=Table.Buffer(Table.UnpivotOtherColumns(TransactionTable,{"ID_person","Name"},"Attributes","Value")) in Table.Sort(#table({"ID_person","ID_category","Custom"},List.TransformMany(Table.ToRows(Table.AddColumn(ReferenceTable,"n",(x)=>Table.SelectRows(a,each Text.Contains([Value],x[Value],Comparer.OrdinalIgnoreCase)))),each Table.ToRows(_{2}),(x,y)=>{y{0},x{0},y{2}})),"ID_person")

    • witbi's avatar
      witbi
      Helper I

      wdx223_Daniel this was spot on! Thank you for the new knowledge.

    • witbi's avatar
      witbi
      Helper I

      Hello again wdx223_Daniel 

      I'm also interested in the opposite case where the transaction table row does not contain the search value. Below isn't giving the desired result. Any help most appreciated. Thank you!

      = let a=Table.Buffer(Table.UnpivotOtherColumns(TransactionTable,{"ID_person","Name"},"Attributes","Value")) in Table.Sort(#table({"ID_person","ID_category","Custom"},List.TransformMany(Table.ToRows(Table.AddColumn(ReferenceTable,"n",(x)=>Table.SelectRows(a,each not  Text.Contains([Value],x[Value],Comparer.OrdinalIgnoreCase)))),each Table.ToRows(_{2}),(x,y)=>{y{0},x{0},y{2}})),"ID_person")

      • wdx223_Daniel's avatar
        wdx223_Daniel
        Community Champion

        let me guess

        = let a=Table.Buffer(TransactionTable) in Table.Sort(#table({"ID_person","ID_category"},List.TransformMany(Table.ToRows(Table.AddColumn(ReferenceTable,"n",(x)=>Table.SelectRows(a,each not List.Contains(List.Skip(Record.ToList(_),2),x[Value],(m,n)=>Text.Contains(m,n,Comparer.OrdinalIgnoreCase))))),each Table.ToRows(_{2}),(x,y)=>{y{0},x{0}})),"ID_person")