<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Re: Calculate Jaccard Similarity between two text strings in DAX Commands and Tips</title>
    <link>https://community.fabric.microsoft.com/t5/DAX-Commands-and-Tips/Calculate-Jaccard-Similarity-between-two-text-strings/m-p/3542617#M136181</link>
    <description>&lt;P&gt;Hi&amp;nbsp;&lt;/P&gt;&lt;P&gt;Good evening.&amp;nbsp;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Your solution was awesome.&amp;nbsp; Thanks a lot.&amp;nbsp; This formula works like magic.&amp;nbsp; It has saved me lot of time and effort.&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thanks.&amp;nbsp;&lt;/P&gt;&lt;P&gt;Karthik&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Mon, 20 Nov 2023 14:23:20 GMT</pubDate>
    <dc:creator>Karthik_s3</dc:creator>
    <dc:date>2023-11-20T14:23:20Z</dc:date>
    <item>
      <title>Calculate Jaccard Similarity between two text strings</title>
      <link>https://community.fabric.microsoft.com/t5/DAX-Commands-and-Tips/Calculate-Jaccard-Similarity-between-two-text-strings/m-p/3355021#M126042</link>
      <description>&lt;P&gt;Hi,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I have a table with two columns containing a text string like this:&lt;/P&gt;&lt;TABLE border="1" cellspacing="0" cellpadding="0"&gt;&lt;TBODY&gt;&lt;TR&gt;&lt;TD&gt;Name 1&lt;/TD&gt;&lt;TD&gt;Name 2&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;Savoy Suites&lt;/TD&gt;&lt;TD&gt;Savoy Suites Hotel Apartments&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;NH Prague City&lt;/TD&gt;&lt;TD&gt;NH Prague City&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;Badhotel Scheveningen&lt;/TD&gt;&lt;TD&gt;Badhotel Scheveningen&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;Terme Tuhelj en Camping&lt;/TD&gt;&lt;TD&gt;Terme Tuhelj&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;Les Lauriers Roses&lt;/TD&gt;&lt;TD&gt;Belambra Club Les Lauriers Roses&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;Nuovo Natural Village&lt;/TD&gt;&lt;TD&gt;Natural Village Resort&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;Torfhaus Harzresort&lt;/TD&gt;&lt;TD&gt;Torfhaus Harzresort&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;Muenster Kongresscenter Affiliated by Melia&lt;/TD&gt;&lt;TD&gt;Hotel Münster Kongresscenter Affiliated by Meliá&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;Royal Azur Thalasso Golf&lt;/TD&gt;&lt;TD&gt;Royal Azur Thalasso Golf&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;Eperland&lt;/TD&gt;&lt;TD&gt;Hotel Eperland&lt;/TD&gt;&lt;/TR&gt;&lt;/TBODY&gt;&lt;/TABLE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Is it possible creating a new column (or using a measure) to calculate the Jaccard Index for these two strings?&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thanks in advance!&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Best,&lt;/P&gt;&lt;P&gt;Jori&lt;/P&gt;&lt;P&gt;&lt;BR /&gt;&lt;a href="javascript:void(0)" data-lia-user-mentions="" data-lia-user-uid="313" data-lia-user-login="Greg_Deckler" class="lia-mention lia-mention-user"&gt;Greg_Deckler&lt;/a&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 28 Jul 2023 08:57:03 GMT</pubDate>
      <guid>https://community.fabric.microsoft.com/t5/DAX-Commands-and-Tips/Calculate-Jaccard-Similarity-between-two-text-strings/m-p/3355021#M126042</guid>
      <dc:creator>jppv20</dc:creator>
      <dc:date>2023-07-28T08:57:03Z</dc:date>
    </item>
    <item>
      <title>Re: Calculate Jaccard Similarity between two text strings</title>
      <link>https://community.fabric.microsoft.com/t5/DAX-Commands-and-Tips/Calculate-Jaccard-Similarity-between-two-text-strings/m-p/3355265#M126069</link>
      <description>&lt;P&gt;i thin you could refer to this discussion&amp;nbsp;&lt;BR /&gt;&lt;BR /&gt;&lt;A href="https://community.fabric.microsoft.com/t5/Desktop/Jaccard-Index-similarity-metric-calculation-in-Power-Query/td-p/2019747#:~:text=The%20Jaccard%20Index%20is%20calculated,%2C%202%263%2C%202%264%20and%203%264" target="_blank"&gt;https://community.fabric.microsoft.com/t5/Desktop/Jaccard-Index-similarity-metric-calculation-in-Power-Query/td-p/2019747#:~:text=The%20Jaccard%20Index%20is%20calculated,%2C%202%263%2C%202%264%20and%203%264&lt;/A&gt;.&lt;/P&gt;</description>
      <pubDate>Fri, 28 Jul 2023 11:30:14 GMT</pubDate>
      <guid>https://community.fabric.microsoft.com/t5/DAX-Commands-and-Tips/Calculate-Jaccard-Similarity-between-two-text-strings/m-p/3355265#M126069</guid>
      <dc:creator>eliasayyy</dc:creator>
      <dc:date>2023-07-28T11:30:14Z</dc:date>
    </item>
    <item>
      <title>Re: Calculate Jaccard Similarity between two text strings</title>
      <link>https://community.fabric.microsoft.com/t5/DAX-Commands-and-Tips/Calculate-Jaccard-Similarity-between-two-text-strings/m-p/3355272#M126070</link>
      <description>&lt;P&gt;Hi&amp;nbsp;&lt;a href="javascript:void(0)" data-lia-user-mentions="" data-lia-user-uid="262173" data-lia-user-login="jppv20" class="lia-mention lia-mention-user"&gt;jppv20&lt;/a&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;If the pairs of strings are known in advance, I would say this task more suited to Power Query. You could create a custom function for Jaccard Similarity.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;However, it is interesting to look at doing this with DAX &lt;span class="lia-unicode-emoji" title=":slightly_smiling_face:"&gt;🙂&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I have attached an example with a Jaccard Similarity calculated column (which could be adapted to a measure).&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I assumed that strings would be tokenized as words, delimited by spaces.&amp;nbsp; Does this match your methodolgy? If not, I'm sure it can be modified to suit.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Here is the code for the calculated column:&lt;/P&gt;
&lt;LI-CODE lang="markup"&gt;Jaccard Similarity = 
VAR String1 = Data[Name 1]
VAR String2 = Data[Name 2]
-- Tokenize strings using space
VAR Delimiter = " " 
-- Convert to vertical bar delimiter to allow path functions
VAR Bar = "|" 
VAR String1_BarDelimited =
    SUBSTITUTE ( String1, Delimiter, Bar )
VAR String1_TokenCount =
     PATHLENGTH ( String1_BarDelimited )
VAR String2_BarDelimited =
    SUBSTITUTE ( String2, Delimiter, Bar )
VAR String2_TokenCount =
     PATHLENGTH ( String2_BarDelimited )
VAR String1_Tokens =
    SELECTCOLUMNS (
        GENERATESERIES ( 1, String1_TokenCount ),
        "Token", PATHITEM ( String1_BarDelimited, [Value] )
    )
VAR String2_Tokens =
    SELECTCOLUMNS (
        GENERATESERIES ( 1, String2_TokenCount ),
        "Token", PATHITEM ( String2_BarDelimited, [Value] )
    )
-- DAX INTERSECT function preserves duplicates so is suitable to find intersection
VAR Tokens_Intersection =
    INTERSECT ( String1_Tokens, String2_Tokens )
-- DAX UNION function not suitable as it is equivalent to SQL UNION ALL
-- First get distinct union of tokens from both strings
VAR Tokens_Union_Distinct =
    DISTINCT ( UNION ( String1_Tokens, String2_Tokens ) )
-- Then repeat each token the max number of times it appears in each string
VAR Tokens_Union =
    SELECTCOLUMNS (
        GENERATE (
            Tokens_Union_Distinct,
            VAR CurrentToken = [Token]
            VAR String1_Count =
                COUNTROWS ( FILTER ( String1_Tokens, [Token] = CurrentToken ) )
            VAR String2_Count =
                COUNTROWS ( FILTER ( String2_Tokens, [Token] = CurrentToken ) )
            VAR Max_Count =
                MAX ( String1_Count, String2_Count )
            RETURN
                -- create required number of rows
                GENERATESERIES ( 1, Max_Count ) 
        ),
        "Token", [Token]
    )     
VAR Jaccard =
    DIVIDE (
        COUNTROWS ( Tokens_Intersection ),
        COUNTROWS ( Tokens_Union )
    )
RETURN
    Jaccard&lt;/LI-CODE&gt;
&lt;P&gt;One thing to note is that the DAX UNION function is similar to UNION ALL in SQL, so we need to modify its result to ensure each token is repeated the correct number of times (the max occurrences in any one of the strings).&lt;/P&gt;
&lt;P&gt;&lt;img /&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Regards&lt;/P&gt;</description>
      <pubDate>Fri, 28 Jul 2023 11:32:20 GMT</pubDate>
      <guid>https://community.fabric.microsoft.com/t5/DAX-Commands-and-Tips/Calculate-Jaccard-Similarity-between-two-text-strings/m-p/3355272#M126070</guid>
      <dc:creator>OwenAuger</dc:creator>
      <dc:date>2023-07-28T11:32:20Z</dc:date>
    </item>
    <item>
      <title>Re: Calculate Jaccard Similarity between two text strings</title>
      <link>https://community.fabric.microsoft.com/t5/DAX-Commands-and-Tips/Calculate-Jaccard-Similarity-between-two-text-strings/m-p/3355295#M126072</link>
      <description>&lt;P&gt;&lt;a href="javascript:void(0)" data-lia-user-mentions="" data-lia-user-uid="6799" data-lia-user-login="OwenAuger" class="lia-mention lia-mention-user"&gt;OwenAuger&lt;/a&gt;&amp;nbsp;can you please sahre how this might be done in power query&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 28 Jul 2023 11:54:22 GMT</pubDate>
      <guid>https://community.fabric.microsoft.com/t5/DAX-Commands-and-Tips/Calculate-Jaccard-Similarity-between-two-text-strings/m-p/3355295#M126072</guid>
      <dc:creator>eliasayyy</dc:creator>
      <dc:date>2023-07-28T11:54:22Z</dc:date>
    </item>
    <item>
      <title>Re: Calculate Jaccard Similarity between two text strings</title>
      <link>https://community.fabric.microsoft.com/t5/DAX-Commands-and-Tips/Calculate-Jaccard-Similarity-between-two-text-strings/m-p/3356277#M126132</link>
      <description>&lt;P&gt;Hi&amp;nbsp;&lt;a href="javascript:void(0)" data-lia-user-mentions="" data-lia-user-uid="434309" data-lia-user-login="eliasayyy" class="lia-mention lia-mention-user"&gt;eliasayyy&lt;/a&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;No problem, updated PBIX attached.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;OL&gt;
&lt;LI&gt;The measure is quite similar to the calculated column. However, to provide input to the measure, I created two disconnected tables containing strings ('Name 1' and 'Name 2').&lt;/LI&gt;
&lt;LI&gt;For Power Query I created a function. The code is significantly shorter than the DAX version, since Power Query has more convenient text-splitting functions, and the List.Union function gives us the result we need directly.&lt;/LI&gt;
&lt;/OL&gt;
&lt;P&gt;There may be some special cases to handle in both methods (empty strings etc) but this is at least a start &lt;span class="lia-unicode-emoji" title=":slightly_smiling_face:"&gt;🙂&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Code below.&lt;/P&gt;
&lt;P&gt;Regards,&lt;/P&gt;
&lt;P&gt;Owen&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;STRONG&gt;Measure: Jaccard Similarity Measure&lt;/STRONG&gt;&lt;/P&gt;
&lt;LI-CODE lang="markup"&gt;VAR String1 = SELECTEDVALUE ( 'Name 1'[Name 1] )
VAR String2 = SELECTEDVALUE ( 'Name 2'[Name 2] )
RETURN
IF (
    NOT ISBLANK ( String1 ) &amp;amp;&amp;amp; NOT ISBLANK ( String2 ),
    -- Tokenize strings using space
    VAR Delimiter = " " 
    -- Convert to vertical bar delimiter to allow path functions
    VAR Bar = "|" 
    VAR String1_BarDelimited =
        SUBSTITUTE ( String1, Delimiter, Bar )
    VAR String1_TokenCount =
        PATHLENGTH ( String1_BarDelimited )
    VAR String2_BarDelimited =
        SUBSTITUTE ( String2, Delimiter, Bar )
    VAR String2_TokenCount =
        PATHLENGTH ( String2_BarDelimited )
    VAR String1_Tokens =
        SELECTCOLUMNS (
            GENERATESERIES ( 1, String1_TokenCount ),
            "Token", PATHITEM ( String1_BarDelimited, [Value] )
        )
    VAR String2_Tokens =
        SELECTCOLUMNS (
            GENERATESERIES ( 1, String2_TokenCount ),
            "Token", PATHITEM ( String2_BarDelimited, [Value] )
        )
    -- DAX INTERSECT function preserves duplicates so is suitable to find intersection
    VAR Tokens_Intersection =
        INTERSECT ( String1_Tokens, String2_Tokens )
    -- DAX UNION function not suitable as it is equivalent to SQL UNION ALL
    -- First get distinct union of tokens from both strings
    VAR Tokens_Union_Distinct =
        DISTINCT ( UNION ( String1_Tokens, String2_Tokens ) )
    -- Then repeat each token the max number of times it appears in each string
    VAR Tokens_Union =
        SELECTCOLUMNS (
            GENERATE (
                Tokens_Union_Distinct,
                VAR CurrentToken = [Token]
                VAR String1_Count =
                    COUNTROWS ( FILTER ( String1_Tokens, [Token] = CurrentToken ) )
                VAR String2_Count =
                    COUNTROWS ( FILTER ( String2_Tokens, [Token] = CurrentToken ) )
                VAR Max_Count =
                    MAX ( String1_Count, String2_Count )
                RETURN
                    -- create required number of rows
                    GENERATESERIES ( 1, Max_Count ) 
            ),
            "Token", [Token]
        )
                
    VAR Jaccard =
        DIVIDE (
            COALESCE ( COUNTROWS ( Tokens_Intersection ), 0 ),
            COUNTROWS ( Tokens_Union )
        )
    RETURN
        Jaccard
)&lt;/LI-CODE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;STRONG&gt;Power Query function: fn_JaccardSimilarity&lt;/STRONG&gt;&lt;/P&gt;
&lt;LI-CODE lang="markup"&gt;( String1 as text, String2 as text ) as number =&amp;gt;
let
    String1_Tokens = Text.Split(String1, " "),
    String2_Tokens = Text.Split(String2, " "),
    Intersection = List.Intersect({String1_Tokens,String2_Tokens}),
    Union = List.Union({String1_Tokens,String2_Tokens}),
    Intersection_Count = List.Count(Intersection),
    Union_Count = List.Count(Union),
    Jaccard_Similarity = Intersection_Count / Union_Count
in Jaccard_Similarity&lt;/LI-CODE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sat, 29 Jul 2023 07:51:22 GMT</pubDate>
      <guid>https://community.fabric.microsoft.com/t5/DAX-Commands-and-Tips/Calculate-Jaccard-Similarity-between-two-text-strings/m-p/3356277#M126132</guid>
      <dc:creator>OwenAuger</dc:creator>
      <dc:date>2023-07-29T07:51:22Z</dc:date>
    </item>
    <item>
      <title>Re: Calculate Jaccard Similarity between two text strings</title>
      <link>https://community.fabric.microsoft.com/t5/DAX-Commands-and-Tips/Calculate-Jaccard-Similarity-between-two-text-strings/m-p/3356338#M126135</link>
      <description>&lt;P&gt;&lt;a href="javascript:void(0)" data-lia-user-mentions="" data-lia-user-uid="6799" data-lia-user-login="OwenAuger" class="lia-mention lia-mention-user"&gt;OwenAuger&lt;/a&gt;&amp;nbsp;thank you very much&lt;/P&gt;</description>
      <pubDate>Sat, 29 Jul 2023 11:03:32 GMT</pubDate>
      <guid>https://community.fabric.microsoft.com/t5/DAX-Commands-and-Tips/Calculate-Jaccard-Similarity-between-two-text-strings/m-p/3356338#M126135</guid>
      <dc:creator>eliasayyy</dc:creator>
      <dc:date>2023-07-29T11:03:32Z</dc:date>
    </item>
    <item>
      <title>Re: Calculate Jaccard Similarity between two text strings</title>
      <link>https://community.fabric.microsoft.com/t5/DAX-Commands-and-Tips/Calculate-Jaccard-Similarity-between-two-text-strings/m-p/3542617#M136181</link>
      <description>&lt;P&gt;Hi&amp;nbsp;&lt;/P&gt;&lt;P&gt;Good evening.&amp;nbsp;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Your solution was awesome.&amp;nbsp; Thanks a lot.&amp;nbsp; This formula works like magic.&amp;nbsp; It has saved me lot of time and effort.&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thanks.&amp;nbsp;&lt;/P&gt;&lt;P&gt;Karthik&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 20 Nov 2023 14:23:20 GMT</pubDate>
      <guid>https://community.fabric.microsoft.com/t5/DAX-Commands-and-Tips/Calculate-Jaccard-Similarity-between-two-text-strings/m-p/3542617#M136181</guid>
      <dc:creator>Karthik_s3</dc:creator>
      <dc:date>2023-11-20T14:23:20Z</dc:date>
    </item>
  </channel>
</rss>

