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
Jackie003
Frequent Visitor

How to extract specific number of characters in Power Query

Hello, do you know how to extract specific number of characters from text? 

Jackie003_0-1712058488779.png

The main rule is: if text contains CC then extract only 6 characters (as you can see on the picture) and if text contains RECHARGE then extract  specific18 characters. 

                           

1 ACCEPTED SOLUTION

@Jackie003, you can try this. But I can only guess that you want to extract 6 or 18 characters after 1st space (you haven't mentioned this).

Add this as Custom Column and replace [Column1] with your column reference

 

[ a = Text.Trim([Column1]),
  b = Text.PositionOf(a, " ")+1,
  c = if Text.StartsWith(a, "CC")
      then Text.Range(a, b, 6) else
      if Text.StartsWith(a, "RECHARGE")
      then Text.Range(a, b, 18) else null
][c]

 


Note: Check this link to learn how to use my query.
Check this link if you don't know how to provide sample data.

View solution in original post

6 REPLIES 6
Jackie003
Frequent Visitor

Sorry, but I received first characters from Column 1 (6 or 18) and for example the output is: "CC. 50" instead of "505050"

@Jackie003, you can try this. But I can only guess that you want to extract 6 or 18 characters after 1st space (you haven't mentioned this).

Add this as Custom Column and replace [Column1] with your column reference

 

[ a = Text.Trim([Column1]),
  b = Text.PositionOf(a, " ")+1,
  c = if Text.StartsWith(a, "CC")
      then Text.Range(a, b, 6) else
      if Text.StartsWith(a, "RECHARGE")
      then Text.Range(a, b, 18) else null
][c]

 


Note: Check this link to learn how to use my query.
Check this link if you don't know how to provide sample data.

If you use my code with your sample data - it will work 😉


Note: Check this link to learn how to use my query.
Check this link if you don't know how to provide sample data.

dufoq3
Super User
Super User

Hi, this will do the job for your sample data: use space as start and also end delimiter

 

dufoq3_0-1712059184473.png

In line 5 your sample contains CC as well as RECHARGE. What do you want to do with this row? If you want just extract A23456, you can use instructions above. I'm asking because you mentioned CONTAINS, not STARTS WITH.

dufoq3_1-1712059361912.png

 


Note: Check this link to learn how to use my query.
Check this link if you don't know how to provide sample data.

Sorry, Of course I meant Starts with 🙂 

Khushidesai0109
Super User
Super User

@Jackie003 

 

if Text.Contains([Text], "CC") then Text.Middle([Text], Text.PositionOf([Text], "CC"), 6)
else if Text.Contains([Text], "RECHARGE") then Text.Middle([Text], Text.PositionOf([Text], "RECHARGE"), 18)
else [Text])


try this M language in power query add custom column will help you

if this help you please give thumbs up and accept it as solution!!

Proud to be a Super User!!

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.

Top Kudoed Authors