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

Don't miss out! 2025 Microsoft Fabric Community Conference, March 31 - April 2, Las Vegas, Nevada. Use code MSCUST for a $150 discount. Prices go up February 11th. Register now.

Reply
Simon_29
Helper II
Helper II

Function for finding a letter in a number and then changing it to a certain value

Hello,

I have a table like this with the Order Number column.1. This column contains numbers that sometimes contain the letters - a, b, c, d, e, f, g .... I would need to find these letters and exchange them for numbers: a = 1 b = 2 c = 3 ... I can find and change one value by right-clicking on the column header and selecting "Replace Values", but I would need a code to find and change multiple letters at once. For example, in the picture below we see the number: 310638736GAO7, where I need to change the letters G, A, 0 after the numbers - 7, 1, 10.

bug.png


Can anyone advise me? I can't solve it. Thank you very much

1 ACCEPTED SOLUTION
mahoneypat
Microsoft Employee
Microsoft Employee

Here is a custom function that does that.  Create a blank query, open Advanced Editor and replace the text there with the code below.  In your original query, you can then go to the Add Column tab, invoke custom function and choose this function and choose your OrderNumber column as the input.

// fnReplaceLetters
(inputtext as text) =>
let 
Result = Text.Combine(List.ReplaceMatchingItems(Text.ToList(inputtext), List.Zip({{"A".."Z"}, List.Transform({1..26}, each Number.ToText(_))})), "")
in 
Result

 

Pat

 





Did I answer your question? Mark my post as a solution! Kudos are also appreciated!

To learn more about Power BI, follow me on Twitter or subscribe on YouTube.


@mahoneypa HoosierBI on YouTube


View solution in original post

6 REPLIES 6
mahoneypat
Microsoft Employee
Microsoft Employee

Here is a custom function that does that.  Create a blank query, open Advanced Editor and replace the text there with the code below.  In your original query, you can then go to the Add Column tab, invoke custom function and choose this function and choose your OrderNumber column as the input.

// fnReplaceLetters
(inputtext as text) =>
let 
Result = Text.Combine(List.ReplaceMatchingItems(Text.ToList(inputtext), List.Zip({{"A".."Z"}, List.Transform({1..26}, each Number.ToText(_))})), "")
in 
Result

 

Pat

 





Did I answer your question? Mark my post as a solution! Kudos are also appreciated!

To learn more about Power BI, follow me on Twitter or subscribe on YouTube.


@mahoneypa HoosierBI on YouTube


Thank you very much. It worked! 🙂 

 

Gabriel_Walkman
Continued Contributor
Continued Contributor

Before addressing your issue, I'd like to point out that with your system, the converted order numbers can cause issues:

Letter K = 11
Letters AA = 11

You see the problem?

right now I realized that I would not have such an example of having two identical letters next to each other, so it would be enough if one letter finds and changes it.

It was just an example. The problem does not require identical letters. Again, just an example:

 

L = 12

AB = 12

Hi,
yes yes, I understand what would probably happen but then how can it be done? I can't believe that no one has tried such a thing yet 🙂

Helpful resources

Announcements
Las Vegas 2025

Join us at the Microsoft Fabric Community Conference

March 31 - April 2, 2025, in Las Vegas, Nevada. Use code MSCUST for a $150 discount!

Jan25PBI_Carousel

Power BI Monthly Update - January 2025

Check out the January 2025 Power BI update to learn about new features in Reporting, Modeling, and Data Connectivity.

December 2024

A Year in Review - December 2024

Find out what content was popular in the Fabric community during 2024.

Top Kudoed Authors