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

Earn a 50% discount on the DP-600 certification exam by completing the Fabric 30 Days to Learn It challenge.

Reply

Create a many to many relationship with wildcard

Hi Everyone,

 

I have two working tables (A & B) and the ideal transformed result table (C) below in the attached Excel/PBI format. 

 

Ideally I would create a relationship (it would need to be many to many) between table A & B to allow the two datasets to be used in the same visualisation/have filters apply etc.  Wildcards cannot be used within the PK/FK in a relationship which means all possible permutations of the wildcards would need to be generated prior to the relationship being generated. 

 

I’m unsure how to generate these wildcard permutations in an efficient manner as there are 2k rows, many rows have multiple wildcard and each wildcard could be A-Z or 1-9 so 35 possible permutations per wildcard which means there are likely multiple millions of wildcard permutations when fully extrapolated.  

 

If there is a simpler and/or easier solution then I would greatly appreciate it. I'm open to any suggestions on how to get around the issue.

 

Sample Data:

Transformed Data Excel 

Transformed Data Power BI 

 

Thanks

2 ACCEPTED SOLUTIONS

just delete the second condition of the FILTER function's second parameter.

like "&&len()=len()

View solution in original post

Smauro
Solution Sage
Solution Sage

Hi @campbellmurphy,

 

A possible PQ solution:

1) Create a new table, to go through the actual codes generated with the wildcards. This is going to be used as an intermediary to link TABLE A and TABLE B. One note, as seen with your data, in case of overlap both KPIs are linked (ABCD will get linked to AB?D and A??D)

let
    AcceptableCharacters = {"A".."Z"} & {"1".."9"},

    Codes = #"TABLE A: KPIs"[[#"Code (PK)"]],
    lenghtList = { "1"..Text.From(List.Max(List.Transform(Codes[#"Code (PK)"], Text.Length) ) ) },

    #"Added CodeList" = Table.AddColumn(Codes, "Codes"each Record.FromList(Text.ToList([#"Code (PK)"]), lenghtList ) ),
    #"Expanded Codes" = Table.ExpandRecordColumn(#"Added CodeList", "Codes", lenghtList),

    #"Fix Wildcards" = Table.TransformColumns(#"Expanded Codes", List.Transform(lenghtList, (t) => 
        {t,
        (x) => if x = "?" then AcceptableCharacters else {x} })),
    #"Fix list Items" = List.Accumulate(lenghtList, #"Fix Wildcards", (tbl, col) => Table.ExpandListColumn(tbl, col) ),
    #"Merge to text" = Table.CombineColumns(#"Fix list Items", lenghtList, (x) as text => Text.Combine(x, ""), "Code")
in
    #"Merge to text"

 

 

2) Add a column in TABLE B, to get the max Length of the code based on TABLE A:

    maxKPICodeLength = List.Max(List.Transform(#"TABLE A: KPIs"[#"Code (PK)"], Text.Length) ),
    #"Add Code" = Table.AddColumn(#"Changed Type", "Code"each Text.Start([#"Code (FK)"], maxKPICodeLength), type text)

 

 

 

3) Create the relationships of  #"TABLE A: KPIs"[#"Code (PK)"]<>>#"newtable"[#"Code (PK)"] and #"TABLE B: Balances"[#"Code"]<<>#"newtable"[Code"]

 

 




Feel free to connect with me:
LinkedIn

View solution in original post

6 REPLIES 6
Smauro
Solution Sage
Solution Sage

Hi @campbellmurphy,

 

A possible PQ solution:

1) Create a new table, to go through the actual codes generated with the wildcards. This is going to be used as an intermediary to link TABLE A and TABLE B. One note, as seen with your data, in case of overlap both KPIs are linked (ABCD will get linked to AB?D and A??D)

let
    AcceptableCharacters = {"A".."Z"} & {"1".."9"},

    Codes = #"TABLE A: KPIs"[[#"Code (PK)"]],
    lenghtList = { "1"..Text.From(List.Max(List.Transform(Codes[#"Code (PK)"], Text.Length) ) ) },

    #"Added CodeList" = Table.AddColumn(Codes, "Codes"each Record.FromList(Text.ToList([#"Code (PK)"]), lenghtList ) ),
    #"Expanded Codes" = Table.ExpandRecordColumn(#"Added CodeList", "Codes", lenghtList),

    #"Fix Wildcards" = Table.TransformColumns(#"Expanded Codes", List.Transform(lenghtList, (t) => 
        {t,
        (x) => if x = "?" then AcceptableCharacters else {x} })),
    #"Fix list Items" = List.Accumulate(lenghtList, #"Fix Wildcards", (tbl, col) => Table.ExpandListColumn(tbl, col) ),
    #"Merge to text" = Table.CombineColumns(#"Fix list Items", lenghtList, (x) as text => Text.Combine(x, ""), "Code")
in
    #"Merge to text"

 

 

2) Add a column in TABLE B, to get the max Length of the code based on TABLE A:

    maxKPICodeLength = List.Max(List.Transform(#"TABLE A: KPIs"[#"Code (PK)"], Text.Length) ),
    #"Add Code" = Table.AddColumn(#"Changed Type", "Code"each Text.Start([#"Code (FK)"], maxKPICodeLength), type text)

 

 

 

3) Create the relationships of  #"TABLE A: KPIs"[#"Code (PK)"]<>>#"newtable"[#"Code (PK)"] and #"TABLE B: Balances"[#"Code"]<<>#"newtable"[Code"]

 

 




Feel free to connect with me:
LinkedIn

Thanks Smauro this was also a good solution.  In many ways it works even better.  I really appreciate you taking the time

wdx223_Daniel
Super User
Super User

try this code to generate a new table

wdx223_Daniel_0-1611650416780.png

 

Hi @wdx223_Daniel,

 

I really appreciate the code you’ve sent through.  When I brought it back into my actual dataset I found an additional complication which I hadn't accounted for (see below). 

Transformed Data.png

The Code (FK) in Table B often has additional alphanumeric values at the end of the string relative to the values in Code (PK) so the length won’t necessarily match.  I’ve attempted to resolve this issue by using the CONTAINSSTRING function instead of LEN but haven’t had success.  Any suggestions would be greatly appreciated.


Sample Data (Updated):
Transformed Data Excel 
Transformed Data Power BI 

Thanks

just delete the second condition of the FILTER function's second parameter.

like "&&len()=len()

Greg_Deckler
Super User
Super User

@campbellmurphy Should be able to use GENERATE


@ me in replies or I'll lose your thread!!!
Instead of a Kudo, please vote for this idea
Become an expert!: Enterprise DNA
External Tools: MSHGQM
YouTube Channel!: Microsoft Hates Greg
Latest book!:
The Definitive Guide to Power Query (M)

DAX is easy, CALCULATE makes DAX hard...

Helpful resources

Announcements
LearnSurvey

Fabric certifications survey

Certification feedback opportunity for the community.

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.

Top Solution Authors
Top Kudoed Authors