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

Calling all Data Engineers! Fabric Data Engineer (Exam DP-700) live sessions are back! Starting October 16th. Sign up.

Reply
allondon
Frequent Visitor

DAX does not recognise underscore as an argument

I have a column BUSINESS_RULE_REF in a merged Power BI table that contains values like this:

105_AL_Subgroup_CIRCE_Consistency.

 

Each token between the underscores can be of a different length, I need the text between the first 2 underscores (AL in the example above).

 

Category =
VAR FirstUnderscore =
    SEARCH("_", 'Append ALL'[BUSINESS_RULE_REF], 1, -1)
VAR SecondUnderscore =
    SEARCH("_", 'Append ALL'[BUSINESS_RULE_REF], FirstUnderscore + 1, -1)
RETURN
    IF(
        FirstUnderscore > 0 && SecondUnderscore > FirstUnderscore,
        MID(
            'Append ALL'[BUSINESS_RULE_REF],
            FirstUnderscore + 1,
            SecondUnderscore - FirstUnderscore - 1
        ),
        BLANK()
    )
 
Copilot test is fine, in Power BI it fails: An argument of function 'SEARCH' has the wrong data type or has an invalid value.
 
If I remove -1 as the fourth argument it says the text given to SEARCH function is not found.
 
Thanks in advance for any ideas.
 
1 ACCEPTED SOLUTION
grazitti_sapna
Super User
Super User

Hi @allondon,

 

The fourth argument (not_found_value) is optional. Using -1 is fine, but if the first underscore doesn’t exist, SEARCH will return -1, which breaks the second search.

 

Try below DAX

 

Category =
VAR TextValue = TRIM( 'Append ALL'[BUSINESS_RULE_REF] ) -- ensure text
VAR FirstUnderscore = SEARCH("_", TextValue, 1, 0) -- 0 if not found
VAR SecondUnderscore =
IF(
FirstUnderscore > 0,
SEARCH("_", TextValue, FirstUnderscore + 1, 0),
0
)
RETURN
IF(
FirstUnderscore > 0 && SecondUnderscore > FirstUnderscore,
MID(
TextValue,
FirstUnderscore + 1,
SecondUnderscore - FirstUnderscore - 1
),
BLANK()
)

🌟 I hope this solution helps you unlock your Power BI potential! If you found it helpful, click 'Mark as Solution' to guide others toward the answers they need.
💡 Love the effort? Drop the kudos! Your appreciation fuels community spirit and innovation.
🎖 As a proud SuperUser and Microsoft Partner, we’re here to empower your data journey and the Power BI Community at large.
🔗 Curious to explore more? [Discover here].
Let’s keep building smarter solutions together!

View solution in original post

6 REPLIES 6
allondon
Frequent Visitor

The solution proivided by grazitti_sapna worked, though I'm still trying to understand how TRIM made the difference. Thanks everyone, I'll try other suggestions as well.

allondon
Frequent Visitor

It worked, thanks! Now I need to understand why 🙂

Ahmedx
Super User
Super User

and this code try

Category = 
VAR _FirstUnderscore =SUBSTITUTE( SUBSTITUTE(
 SUBSTITUTE('Append ALL'[BUSINESS_RULE_REF],"_","|"),"||","|"),"||","|")
VAR _ST = PATHITEM(_FirstUnderscore,2,TEXT)
RETURN
_ST

 

Screenshot_2.png 

mh2587
Super User
Super User

Category = //Try SUBSTITUTE()with PATHITEM() might help you
PATHITEM(SUBSTITUTE('Append ALL'[BUSINESS_RULE_REF], "_", "|"), 2)

Did I answer your question? If so, please mark my post as a solution!


Proud to be a Super User!




LinkedIn Icon
Muhammad Hasnain



Ahmedx
Super User
Super User

try this code
Screenshot_1.png

grazitti_sapna
Super User
Super User

Hi @allondon,

 

The fourth argument (not_found_value) is optional. Using -1 is fine, but if the first underscore doesn’t exist, SEARCH will return -1, which breaks the second search.

 

Try below DAX

 

Category =
VAR TextValue = TRIM( 'Append ALL'[BUSINESS_RULE_REF] ) -- ensure text
VAR FirstUnderscore = SEARCH("_", TextValue, 1, 0) -- 0 if not found
VAR SecondUnderscore =
IF(
FirstUnderscore > 0,
SEARCH("_", TextValue, FirstUnderscore + 1, 0),
0
)
RETURN
IF(
FirstUnderscore > 0 && SecondUnderscore > FirstUnderscore,
MID(
TextValue,
FirstUnderscore + 1,
SecondUnderscore - FirstUnderscore - 1
),
BLANK()
)

🌟 I hope this solution helps you unlock your Power BI potential! If you found it helpful, click 'Mark as Solution' to guide others toward the answers they need.
💡 Love the effort? Drop the kudos! Your appreciation fuels community spirit and innovation.
🎖 As a proud SuperUser and Microsoft Partner, we’re here to empower your data journey and the Power BI Community at large.
🔗 Curious to explore more? [Discover here].
Let’s keep building smarter solutions together!

Helpful resources

Announcements
FabCon Global Hackathon Carousel

FabCon Global Hackathon

Join the Fabric FabCon Global Hackathon—running virtually through Nov 3. Open to all skill levels. $10,000 in prizes!

October Power BI Update Carousel

Power BI Monthly Update - October 2025

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

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.