Forum Discussion
Extracting text IF these conditions are met (Power Query)
- 6 years ago
Hi, campelliann
You may try to add a custom column with following codes. The pbix file is attached in the end.
let len = Text.Length([ID]),id = [ID] in if Text.Contains("ID","P20") and len>=9 then Text.Middle(id,0,5)& Text.Middle(id,len-4,len-1) else id)Result:
Best Regards
Allan
If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.
Hi, campelliann
Based on your description, I created data to reproduce your scenario. The pbix file is attached in the end.
Table:
You may add a custom column with following codes in 'Query Editor'.
let
len = Text.Length([ID]),id = [ID]
in
if len>=9 then
Text.Middle(id,0,5)& Text.Middle(id,len-4,len-1)
else id
Result:
Best Regards
Allan
If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.
- campelliann6 years ago
Post Patron
Hi Allan,
This almost works, the problem is that I have some text fields which lenght is >= 9, and I want them to stay the same.
Ist it possible, instead of using the condition len>=9, if text contains "P20??".
Many thanks
- v-alq-msft6 years ago
Community Support
Hi, campelliann
You may try to add a custom column with following codes. The pbix file is attached in the end.
let len = Text.Length([ID]),id = [ID] in if Text.Contains("ID","P20") and len>=9 then Text.Middle(id,0,5)& Text.Middle(id,len-4,len-1) else id)Result:
Best Regards
Allan
If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.