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

Join the Fabric FabCon Global Hackathon—running virtually through Nov 3. Open to all skill levels. $10,000 in prizes! Register now.

Reply
Anonymous
Not applicable

return uncommon String after Comparisons

I want to compare two colums. Both of them contain strings.

I need to get the un common string in thrid column.

 

Col1

abc|def|ijk|bcd|plk

 

Col2

abc|plk|ijk

Col3 [Resulting Column ]
def|bcd

 

1 ACCEPTED SOLUTION
OwenAuger
Super User
Super User

Hi @Anonymous 

Off the top of my head, whether using M or DAX, I would:

  1. Split each of Col1 & Col2 into lists List1 & List2
  2. Take the union of List1 & List2, and remove the intersection of List1 & List2 (and remove duplicates & sort if desired)
  3. Concatenate the result

Attached PBIX has examples in M & DAX.

The step to add such a column in M in my example is:

= Table.AddColumn(#"Changed Type", "Col3 M",
        each let List1 = Text.Split([Col1],"|"),
                 List2 = Text.Split([Col2],"|"),
                 Union = List.Union({List1,List2}),
                 Intersection = List.Intersect({List1,List2}),
                 Uncommon = List.Sort(List.Distinct(List.Difference(Union,Intersection)))
             in
                 Text.Combine(Uncommon,"|"), type text)

The DAX calculated column version is:

Col3 DAX = 
VAR String1 = Test[Col1]
VAR String2 = Test[Col2]
VAR Length1 = PATHLENGTH ( String1 )
VAR Length2 = PATHLENGTH ( String2 )
VAR List1 =
    SELECTCOLUMNS (
        GENERATESERIES ( 1, Length1 ),
        "Value", PATHITEM ( String1, [Value] )
    )
VAR List2 =
    SELECTCOLUMNS (
        GENERATESERIES ( 1, Length2 ),
        "Value", PATHITEM ( String2, [Value] )
    )
VAR Uncommon = 
    DISTINCT (
        EXCEPT ( 
            UNION ( List1, List2 ),
            INTERSECT ( List1, List2 )
        )
    )
RETURN
    CONCATENATEX (
        Uncommon,
        [Value],
        "|",
        [Value]
    )

There could be some optimization, but that's what comes to mind 🙂

Hope that helps.

 

Regards,

Owen

 


Owen Auger
Did I answer your question? Mark my post as a solution!
Blog
LinkedIn

View solution in original post

1 REPLY 1
OwenAuger
Super User
Super User

Hi @Anonymous 

Off the top of my head, whether using M or DAX, I would:

  1. Split each of Col1 & Col2 into lists List1 & List2
  2. Take the union of List1 & List2, and remove the intersection of List1 & List2 (and remove duplicates & sort if desired)
  3. Concatenate the result

Attached PBIX has examples in M & DAX.

The step to add such a column in M in my example is:

= Table.AddColumn(#"Changed Type", "Col3 M",
        each let List1 = Text.Split([Col1],"|"),
                 List2 = Text.Split([Col2],"|"),
                 Union = List.Union({List1,List2}),
                 Intersection = List.Intersect({List1,List2}),
                 Uncommon = List.Sort(List.Distinct(List.Difference(Union,Intersection)))
             in
                 Text.Combine(Uncommon,"|"), type text)

The DAX calculated column version is:

Col3 DAX = 
VAR String1 = Test[Col1]
VAR String2 = Test[Col2]
VAR Length1 = PATHLENGTH ( String1 )
VAR Length2 = PATHLENGTH ( String2 )
VAR List1 =
    SELECTCOLUMNS (
        GENERATESERIES ( 1, Length1 ),
        "Value", PATHITEM ( String1, [Value] )
    )
VAR List2 =
    SELECTCOLUMNS (
        GENERATESERIES ( 1, Length2 ),
        "Value", PATHITEM ( String2, [Value] )
    )
VAR Uncommon = 
    DISTINCT (
        EXCEPT ( 
            UNION ( List1, List2 ),
            INTERSECT ( List1, List2 )
        )
    )
RETURN
    CONCATENATEX (
        Uncommon,
        [Value],
        "|",
        [Value]
    )

There could be some optimization, but that's what comes to mind 🙂

Hope that helps.

 

Regards,

Owen

 


Owen Auger
Did I answer your question? Mark my post as a solution!
Blog
LinkedIn

Helpful resources

Announcements
September Power BI Update Carousel

Power BI Monthly Update - September 2025

Check out the September 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.

Top Solution Authors
Top Kudoed Authors