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

Earn a 50% discount on the DP-600 certification exam by completing the Fabric 30 Days to Learn It challenge.

Reply
Migasuke
Super User
Super User

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...

Thanks for any idea.


If my answer was helpful please give me a Kudos or even accept as a Solution.

Let's connect on LinkedIn!

1 ACCEPTED SOLUTION
AlexisOlson
Super User
Super User

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

AlexisOlson_0-1704925248619.png

Where

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

 

View solution in original post

2 REPLIES 2
Migasuke
Super User
Super User

@AlexisOlson Awesome solution. Thanks for that!



If my answer was helpful please give me a Kudos or even accept as a Solution.

Let's connect on LinkedIn!

AlexisOlson
Super User
Super User

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

AlexisOlson_0-1704925248619.png

Where

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

 

Helpful resources

Announcements
Microsoft Fabric Learn Together

Microsoft Fabric Learn Together

Covering the world! 9:00-10:30 AM Sydney, 4:00-5:30 PM CET (Paris/Berlin), 7:00-8:30 PM Mexico City

PBI_APRIL_CAROUSEL1

Power BI Monthly Update - April 2024

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

April Fabric Community Update

Fabric Community Update - April 2024

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