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

The Power BI DataViz World Championships are on! With four chances to enter, you could win a spot in the LIVE Grand Finale in Las Vegas. Show off your skills.

Reply
courtney11
New Member

Replace string of characters (data type text) with equivalent whole number

I have a  custom column that I am attempting to populate with mapped customer numbers.

If the mapped customer number exists, use that, else use the AccountNo or DeliveryCustomer fields (Text) and convert that to a unique number.

Issue is that I keep getting an error saying that the Text field cannot be converted to number when we have to use AccountNo or DeliveryCustomer.

 

I have tried the following functions with no luck converting the text to a number:

Character.ToNumber

Number.FromText

 

Example data that I would like to convert to unique number:

JOH06

STR04

 

Ex statement throwing error when the result is to use the ID:

if [#"Customer  Mapping.Account #"]<> null then [#"Customer  Mapping.Account #"]
else if [AccountNo]<>null then Character.ToNumber([AccountNo])
else if [DeliveryCustomer]<>null then Character.ToNumber([DeliveryCustomer])
else 0

 

 

1 ACCEPTED SOLUTION

Check this:

I've changed your Character.ToNumber([AccountNo]) to this, which converts every character to number and combine all numbers as text. If you want to make it really unique - you can add some random number before combining.

Text.Combine(List.Transform(Text.ToList([AccountNo]), (x)=> Text.From(Character.ToNumber(x))))

 

Result

dufoq3_0-1713208036406.png

 

Whole code:

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("jZAxC4MwEIX/Sshs6yVp1I5SQZBOOnQQh1QDFTRaa1r672tUUunUQLh3cO97l+Q5Tjo9KNFgBxOXuBQoMRIAppKAOYwDmxr0cwEXTo5TqeRrsdON/8BgxgTAuRV7umtXcxQzoFtdduoph1FWaOyQVvVdS6R0e5XDHJMJkU2jawSbI3wTQT3LyS6h1bZHp7/AxwVMv2AS+N4KitOQmFfFg+hvdSlQVD/6RrzReazMCtMfcU43qig+", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Source = _t, #"Revenue Date" = _t, Nominal = _t, InvoiceNo = _t, #"Lines.Renewal Deferrals.AccInvoiceNo" = _t, AccountNo = _t, DeliveryCustomer = _t, #"Customer  Mapping.Account#" = _t, #"EXPECTED RESULT" = _t]),
    TrimColumnNames = Table.TransformColumnNames(Source, each Text.Combine(List.Select(Text.Split(_, " "), (x)=> Text.Length(x) > 0), " ")),
    ReplaceBlankToNull = Table.TransformColumns(TrimColumnNames, {}, each if Text.Trim(_) = "" then null else _),
    Ad_Result = Table.AddColumn(ReplaceBlankToNull, "Result", each if [#"Customer Mapping.Account#"]<> null then [#"Customer Mapping.Account#"]
else if [AccountNo]<>null then Text.Combine(List.Transform(Text.ToList([AccountNo]), (x)=> Text.From(Character.ToNumber(x))))
else if [DeliveryCustomer]<>null then Text.Combine(List.Transform(Text.ToList([DeliveryCustomer]), (x)=> Text.From(Character.ToNumber(x))))
else 0, Int64.Type)
in
    Ad_Result

 


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

5 REPLIES 5
dufoq3
Super User
Super User

Hi, could you provide sample data (lets say 3 rows) for every column? And also expected result based on this data?


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

Here you go:

 

     TEXTTEXTWHOLE NUMBERWHOLE NUMBER
SourceRevenue DateNominalInvoiceNoLines.Renewal Deferrals.AccInvoiceNoAccountNoDeliveryCustomerCustomer  Mapping.Account#EXPECTED RESULT
Journal1/1/20211000J000003503    0
Renewal12/1/20211430108055108055.2-m DG302 DG302converted to unique number
SaaS2/1/202314702602 SWA02  SWA02 Converted to unique number
SaaS9/1/202214701876 GRA11Graphica Display Ltd20005522000552

Check this:

I've changed your Character.ToNumber([AccountNo]) to this, which converts every character to number and combine all numbers as text. If you want to make it really unique - you can add some random number before combining.

Text.Combine(List.Transform(Text.ToList([AccountNo]), (x)=> Text.From(Character.ToNumber(x))))

 

Result

dufoq3_0-1713208036406.png

 

Whole code:

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("jZAxC4MwEIX/Sshs6yVp1I5SQZBOOnQQh1QDFTRaa1r672tUUunUQLh3cO97l+Q5Tjo9KNFgBxOXuBQoMRIAppKAOYwDmxr0cwEXTo5TqeRrsdON/8BgxgTAuRV7umtXcxQzoFtdduoph1FWaOyQVvVdS6R0e5XDHJMJkU2jawSbI3wTQT3LyS6h1bZHp7/AxwVMv2AS+N4KitOQmFfFg+hvdSlQVD/6RrzReazMCtMfcU43qig+", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Source = _t, #"Revenue Date" = _t, Nominal = _t, InvoiceNo = _t, #"Lines.Renewal Deferrals.AccInvoiceNo" = _t, AccountNo = _t, DeliveryCustomer = _t, #"Customer  Mapping.Account#" = _t, #"EXPECTED RESULT" = _t]),
    TrimColumnNames = Table.TransformColumnNames(Source, each Text.Combine(List.Select(Text.Split(_, " "), (x)=> Text.Length(x) > 0), " ")),
    ReplaceBlankToNull = Table.TransformColumns(TrimColumnNames, {}, each if Text.Trim(_) = "" then null else _),
    Ad_Result = Table.AddColumn(ReplaceBlankToNull, "Result", each if [#"Customer Mapping.Account#"]<> null then [#"Customer Mapping.Account#"]
else if [AccountNo]<>null then Text.Combine(List.Transform(Text.ToList([AccountNo]), (x)=> Text.From(Character.ToNumber(x))))
else if [DeliveryCustomer]<>null then Text.Combine(List.Transform(Text.ToList([DeliveryCustomer]), (x)=> Text.From(Character.ToNumber(x))))
else 0, Int64.Type)
in
    Ad_Result

 


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

This worked, thank you for the help!

You're welcome.


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

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!

FebPBI_Carousel

Power BI Monthly Update - February 2025

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

Feb2025 NL Carousel

Fabric Community Update - February 2025

Find out what's new and trending in the Fabric community.

Top Solution Authors