Forum Discussion

courtney11's avatar
courtney11
New Member
2 years ago
Solved

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

 

 

  • dufoq3's avatar
    dufoq3
    2 years ago

    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

     

    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

     

5 Replies

  • dufoq3's avatar
    dufoq3
    Icon for Community Champion rankCommunity Champion

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

    • courtney11's avatar
      courtney11
      New Member

      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
      • dufoq3's avatar
        dufoq3
        Icon for Community Champion rankCommunity Champion

        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

         

        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