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

Level up your Power BI skills this month - build one visual each week and tell better stories with data! Get started

Reply
amjwh99
New Member

Compare two strings for similarity

Hi,

 

Is there a way to compare two text strings for similarity? I'm looking for the return value to be a percentage, not true/false.  Example image.jpg

1 ACCEPTED SOLUTION
v-sihou-msft
Microsoft Employee
Microsoft Employee

@amjwh99

 

In DAX/Power Query, we can't check consequence of each character in comparison, like "John" and "Jonh". If you don't need to consider that kind of scenario, you just need to have each character in Name2 compare with Name1 and get the number of matched chrarcters for calculation percentage. 

 

1. Transfrom Name2 into a List. 

 

=Text.ToList([Name2])

123.PNG

 

2. Expand values into current table. 

 

3.PNG

 

3. Add a custom column to check if character is matched.

 

=if Text.Contains([Name1],[Name2 List]) then 1 else 0

44.PNG

 

4. Group on Name1 and Name2. 

 

55.PNG

 

5. Create a column to Name1 text length.

 

=Text.Length([Name1])

66.PNG

 

6. Create a column to calculate the similarity.

 

2.PNG

 

Then entire Power Query:

 

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45W8srPyFPSgVCxOtFKvpnZqUA+mALxvRLBfK/E5PwksIBPYlFxFVAERCvFxgIA", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type text) meta [Serialized.Text = true]) in type table [Name1 = _t, Name2 = _t]),
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"Name1", type text}, {"Name2", type text}}),
    #"Added Custom1" = Table.AddColumn(#"Changed Type", "Name2 List", each Text.ToList([Name2])),
    #"Expanded Name2 List" = Table.ExpandListColumn(#"Added Custom1", "Name2 List"),
    #"Added Custom" = Table.AddColumn(#"Expanded Name2 List", "Custom", each if Text.Contains([Name1],[Name2 List]) then 1 else 0),
    #"Grouped Rows" = Table.Group(#"Added Custom", {"Name1", "Name2"}, {{"Match", each List.Sum([Custom]), type number}}),
    #"Added Custom2" = Table.AddColumn(#"Grouped Rows", "NameLength", each Text.Length([Name1])),
    #"Added Custom3" = Table.AddColumn(#"Added Custom2", "Similarity", each [Match]/[NameLength]),
    #"Changed Type1" = Table.TransformColumnTypes(#"Added Custom3",{{"Similarity", Percentage.Type}})
in
    #"Changed Type1"

99.PNG

 

Regards,

View solution in original post

2 REPLIES 2
DevourFood
New Member

Hey thank you, its a blessing

v-sihou-msft
Microsoft Employee
Microsoft Employee

@amjwh99

 

In DAX/Power Query, we can't check consequence of each character in comparison, like "John" and "Jonh". If you don't need to consider that kind of scenario, you just need to have each character in Name2 compare with Name1 and get the number of matched chrarcters for calculation percentage. 

 

1. Transfrom Name2 into a List. 

 

=Text.ToList([Name2])

123.PNG

 

2. Expand values into current table. 

 

3.PNG

 

3. Add a custom column to check if character is matched.

 

=if Text.Contains([Name1],[Name2 List]) then 1 else 0

44.PNG

 

4. Group on Name1 and Name2. 

 

55.PNG

 

5. Create a column to Name1 text length.

 

=Text.Length([Name1])

66.PNG

 

6. Create a column to calculate the similarity.

 

2.PNG

 

Then entire Power Query:

 

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45W8srPyFPSgVCxOtFKvpnZqUA+mALxvRLBfK/E5PwksIBPYlFxFVAERCvFxgIA", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type text) meta [Serialized.Text = true]) in type table [Name1 = _t, Name2 = _t]),
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"Name1", type text}, {"Name2", type text}}),
    #"Added Custom1" = Table.AddColumn(#"Changed Type", "Name2 List", each Text.ToList([Name2])),
    #"Expanded Name2 List" = Table.ExpandListColumn(#"Added Custom1", "Name2 List"),
    #"Added Custom" = Table.AddColumn(#"Expanded Name2 List", "Custom", each if Text.Contains([Name1],[Name2 List]) then 1 else 0),
    #"Grouped Rows" = Table.Group(#"Added Custom", {"Name1", "Name2"}, {{"Match", each List.Sum([Custom]), type number}}),
    #"Added Custom2" = Table.AddColumn(#"Grouped Rows", "NameLength", each Text.Length([Name1])),
    #"Added Custom3" = Table.AddColumn(#"Added Custom2", "Similarity", each [Match]/[NameLength]),
    #"Changed Type1" = Table.TransformColumnTypes(#"Added Custom3",{{"Similarity", Percentage.Type}})
in
    #"Changed Type1"

99.PNG

 

Regards,

Helpful resources

Announcements
April Power BI Update Carousel

Power BI Monthly Update - April 2026

Check out the April 2026 Power BI update to learn about new features.

Fabric SQL PBI Data Days

Data Days 2026 coming soon!

Sign up to receive a private message when registration opens and key events begin.

New to Fabric survey Carousel

New to Fabric Survey

If you have recently started exploring Fabric, we'd love to hear how it's going. Your feedback can help with product improvements.

Power BI DataViz World Championships carousel

Power BI DataViz World Championships - June 2026

A new Power BI DataViz World Championship is coming this June! Don't miss out on submitting your entry.