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

Get Fabric Certified for FREE during Fabric Data Days. Don't miss your chance! Request now

Reply
Cthies1965
Frequent Visitor

Identifying contracts with a large over lap of line items

Hello,

I have the following challenge:

I have a list of contract line items:

Contact IDLine item IDDescription
1000110Item A
1000120

Item B

1000130Item C
1000210Item D
1000220Item A
1000310Item A
1000320Item B
1000330Item D
1000340Item E
1000410Item F

 

I would like to identify for each Contract ID which other contract have an overlap of line item Description of more than 49%.

So the expected out come should be the following:

Base Contract IDComparison Contract IDPercent Overlap
100011000366.6
100021000150.0
1000210003100.0
100031000150.0
100031000250.0

 

I hope that something like this is possible with M code and would appreciate any advice.

Thank You.

1 ACCEPTED SOLUTION
AlienSx
Super User
Super User

let
    Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],
    group = Table.Group(
        Source, 
        "Contact ID", 
        {{"items", each [Line item ID]}, {"count", Table.RowCount}}
    ), 
    others = Table.AddColumn(group, "others", each group),
    xp = Table.ExpandTableColumn(
        others, 
        "others", 
        {"Contact ID", "items"}, 
        {"Comparison Contract ID", "comp_items"}
    ),
    overlap = Table.AddColumn(
        xp, 
        "overlap", 
        (x) => List.Count(List.Intersect({x[items], x[comp_items]})) / x[count], 
        Percentage.Type
    )[[Contact ID], [Comparison Contract ID], [overlap]],
    filter = Table.SelectRows(overlap, each [Contact ID] <> [Comparison Contract ID] and [overlap] > .49)
in
    filter

View solution in original post

2 REPLIES 2
AlienSx
Super User
Super User

let
    Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],
    group = Table.Group(
        Source, 
        "Contact ID", 
        {{"items", each [Line item ID]}, {"count", Table.RowCount}}
    ), 
    others = Table.AddColumn(group, "others", each group),
    xp = Table.ExpandTableColumn(
        others, 
        "others", 
        {"Contact ID", "items"}, 
        {"Comparison Contract ID", "comp_items"}
    ),
    overlap = Table.AddColumn(
        xp, 
        "overlap", 
        (x) => List.Count(List.Intersect({x[items], x[comp_items]})) / x[count], 
        Percentage.Type
    )[[Contact ID], [Comparison Contract ID], [overlap]],
    filter = Table.SelectRows(overlap, each [Contact ID] <> [Comparison Contract ID] and [overlap] > .49)
in
    filter

Fantastic, works like a charm. I think the key function here is the List.Intersect. Will have to study your solution a bit longer. Thanks a lot.

Helpful resources

Announcements
Fabric Data Days Carousel

Fabric Data Days

Advance your Data & AI career with 50 days of live learning, contests, hands-on challenges, study groups & certifications and more!

October Power BI Update Carousel

Power BI Monthly Update - October 2025

Check out the October 2025 Power BI update to learn about new features.

FabCon Atlanta 2026 carousel

FabCon Atlanta 2026

Join us at FabCon Atlanta, March 16-20, for the ultimate Fabric, Power BI, AI and SQL community-led event. Save $200 with code FABCOMM.