Forum Discussion

Migasuke's avatar
Migasuke
Memorable Member
2 years ago
Solved

Dynamic SUBSITUTE

HI,
I am having an use case where I need to use SUBSTITUTE function. SUBSTITUTE should have dynamic occurance - which means, someties it changes 1st occurance , sometimes 2nd etc.

Summarizing the problem:

1. This is my code: Measure= SUBSTITUTE(SELECTEDVALUE('Table'[Text]),"X","REP",[LastOccurance])
2. I want to replace last "X" in a text. Last occurance is given by the measure LastOccurance, which returns number.
3. Problem is, I receive following error: "An argument of function 'SUBSTITUTE has the wrong data type or has an invalid value."
4. Once I replace measure with a fixed number, problem dissapears.


The same issue was solved in following link, but I don't understand the solution:

https://community.fabric.microsoft.com/t5/DAX-Commands-and-Tips/DAX-SUBSTITUE-Instance-not-dynamic/m-p/1171790#M18064

Thanks for any idea.
  • I think you're just not handling the case when there are no occurrences. This works for me:

    Where

    Last Occurance = 
    VAR _text = SELECTEDVALUE ( 'table'[Text] )
    VAR _ToPath = SUBSTITUTE ( _text, "X", "|" )
    VAR _Result = PATHLENGTH ( _ToPath ) - 1
    RETURN
        _Result

     

2 Replies

  • I think you're just not handling the case when there are no occurrences. This works for me:

    Where

    Last Occurance = 
    VAR _text = SELECTEDVALUE ( 'table'[Text] )
    VAR _ToPath = SUBSTITUTE ( _text, "X", "|" )
    VAR _Result = PATHLENGTH ( _ToPath ) - 1
    RETURN
        _Result