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

The Power BI Data Visualization World Championships is back! Get ahead of the game and start preparing now! Learn more

Reply
Migasuke
Memorable Member
Memorable Member

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
Memorable Member
Memorable Member

@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
Power BI DataViz World Championships

Power BI Dataviz World Championships

The Power BI Data Visualization World Championships is back! Get ahead of the game and start preparing now!

December 2025 Power BI Update Carousel

Power BI Monthly Update - December 2025

Check out the December 2025 Power BI Holiday Recap!

FabCon Atlanta 2026 carousel

FabCon Atlanta 2026

Join us at FabCon Atlanta, March 16-20, for the ultimate Fabric, Power BI, AI and SQL community-led event. Save $200 with code FABCOMM.