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

Power BI is turning 10! Let’s celebrate together with dataviz contests, interactive sessions, and giveaways. Register now.

Reply
cdebackere
Resolver I
Resolver I

DAX column to trim and find match in other table

I Have 2 table,

In first table there is a column 'shortname'

In the second table there is a column 'longname'

Most of the longnames start with a string matching a shortname

 

So I want to add a calculated dax column to the second table to list the matching (trimmed) longname.

 

WHat I do: I trim the longname at the end, and then look for a match in the other table.

THen I need to return the longest (least trimmed) match.

 

The code I have works, and I am able to get the number of matches, but I am unable to the get the actuall trimmed longname, matching the shortname.

I spent nearly 2 hours trying out a million variants, but it just doesn't work (various errors)

 

This is the code I have. And the problem (with some of my attemps commented) is in the RETURN part

 

name.short = 
VAR OriginalString = [name.long]
VAR OtherTable = 'studentset' 
VAR CheckMatch = 
    GENERATE(
        ADDCOLUMNS(
            GENERATESERIES(1,LEN(OriginalString)),
            "TrimmedString", LEFT(OriginalString, LEN(OriginalString) - [Value] + 1)
        ),
        FILTER('studentset', [TrimmedString] = [name.short])
    )
VAR LargestMatch = 
    TOPN(1,
        CheckMatch)//,[Value],ASC)
VAR n1 = SELECTCOLUMNS(LargestMatch,"sln",[TrimmedString])

RETURN
//OriginalString
CheckMatch[[Value]]
//DISTINCTCOUNT(CheckMatch)
//IF(COUNTROWS(n1) > 0,FIRSTNONBLANK(LargestMatch[[TrimmedString]],1), "")
//IF(COUNTROWS(LargestMatch) > 0, COUNTROWS(LargestMatch), 0)
//))))))))

So now I reach out to the community

The issue seems to be that whenever I refer to a table as defined in one of the VAR's, I cannot use or get hold of a column of that table. So, for instance, the code editor does not accept CheckMatch[TrimmedString] to designate a column in the 'RETRUN' section

 

Thx for helping

Christof

1 ACCEPTED SOLUTION
techies
Solution Sage
Solution Sage

Hi @cdebackere please check this

 

name.short =
VAR OriginalString = [name.long]
VAR TrimmedTable =
    ADDCOLUMNS(
        GENERATESERIES(1, LEN(OriginalString)),
        "TrimmedString", LEFT(OriginalString, LEN(OriginalString) - [Value] + 1)
    )
VAR Matches =
    FILTER(
        ADDCOLUMNS(TrimmedTable, "IsMatch",
            LOOKUPVALUE('studentset'[name.short], 'studentset'[name.short], [TrimmedString])
        ),
        NOT ISBLANK([IsMatch])
    )
VAR BestMatch =
    TOPN(1, Matches, [Value], ASC)  
RETURN
    IF(
        COUNTROWS(BestMatch) > 0,
        SELECTCOLUMNS(BestMatch, "Result", [TrimmedString]),
        BLANK()
    )
― Power BI | Microsoft Fabric | PL-300 | DP-600 | Blog: medium.com/@cseprs_54978

View solution in original post

3 REPLIES 3
cdebackere
Resolver I
Resolver I

@techies 

It seems like I was close, but I fail to see/understand why I didn't make it to the end, because I did try SELECTCOLUMNS as well. I'm going to dig a little because I hate it when I don't understand my mistake.. because I feel like I might be locked in some general misconception.

 

The important thing is: it works, so a massive 'thank you'

 

C.

welcome, i totally get it, happens to me all the time too 🙂

― Power BI | Microsoft Fabric | PL-300 | DP-600 | Blog: medium.com/@cseprs_54978
techies
Solution Sage
Solution Sage

Hi @cdebackere please check this

 

name.short =
VAR OriginalString = [name.long]
VAR TrimmedTable =
    ADDCOLUMNS(
        GENERATESERIES(1, LEN(OriginalString)),
        "TrimmedString", LEFT(OriginalString, LEN(OriginalString) - [Value] + 1)
    )
VAR Matches =
    FILTER(
        ADDCOLUMNS(TrimmedTable, "IsMatch",
            LOOKUPVALUE('studentset'[name.short], 'studentset'[name.short], [TrimmedString])
        ),
        NOT ISBLANK([IsMatch])
    )
VAR BestMatch =
    TOPN(1, Matches, [Value], ASC)  
RETURN
    IF(
        COUNTROWS(BestMatch) > 0,
        SELECTCOLUMNS(BestMatch, "Result", [TrimmedString]),
        BLANK()
    )
― Power BI | Microsoft Fabric | PL-300 | DP-600 | Blog: medium.com/@cseprs_54978

Helpful resources

Announcements
June 2025 Power BI Update Carousel

Power BI Monthly Update - June 2025

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

June 2025 community update carousel

Fabric Community Update - June 2025

Find out what's new and trending in the Fabric community.