Forum Discussion

meierli's avatar
meierli
Helper I
2 years ago
Solved

Combine 4 M-Codes into 1

i have 4 different m-codes all achiving the same result just with slight;y different "lookup" values (see bottom of message) and would like to combine them all into one m-code if possible.

 

m-code #1

let
after_prefix = Text.AfterDelimiter([Transaction_Comment], "CHK#"),
remove_digits = Text.Remove(after_prefix, {"0".."9", "A".."Z"}),
delimiter = Text.Start(remove_digits, 1),
trim_start = Text.AfterDelimiter([Transaction_Comment], "CHK#"),
prod_num = Text.BeforeDelimiter(trim_start, delimiter),
result = if delimiter = ""
then trim_start
else " " & prod_num
in result

 

m-code 2

let
after_prefix = Text.AfterDelimiter([Transaction_Comment], "CHK# "),
remove_digits = Text.Remove(after_prefix, {"0".."9", "A".."Z", "-"}),
delimiter = Text.Start(remove_digits, 1),
trim_start = Text.AfterDelimiter([Transaction_Comment], "CHK# "),
prod_num = Text.BeforeDelimiter(trim_start, delimiter),
result = if delimiter = ""
then trim_start
else " " & prod_num
in result

 

m-code 3

let
after_prefix = Text.AfterDelimiter([Transaction_Comment], "CHK #"),
remove_digits = Text.Remove(after_prefix, {"0".."9", "A".."Z"}),
delimiter = Text.Start(remove_digits, 1),
trim_start = Text.AfterDelimiter([Transaction_Comment], "CHK #"),
prod_num = Text.BeforeDelimiter(trim_start, delimiter),
result = if delimiter = ""
then trim_start
else " " & prod_num
in result

 

m-code 4

let
after_prefix = Text.AfterDelimiter([Transaction_Comment], "Chk#"),
remove_digits = Text.Remove(after_prefix, {"0".."9", "A".."Z"}),
delimiter = Text.Start(remove_digits, 1),
trim_start = Text.AfterDelimiter([Transaction_Comment], "Chk#"),
prod_num = Text.BeforeDelimiter(trim_start, delimiter),
result = if delimiter = ""
then trim_start
else " " & prod_num
in result

  • let
      Source = Table.FromRows(
        Json.Document(
          Binary.Decompress(
            Binary.FromText(
              "jY/NaoNQEEZfZdCtifNzr/eOu0RNI20TUUMI4iJQS6E0hTYU+vbVdidZdBbf6hw403VBtr0PAcUtkFTESWI9tKeqSEGdB6SYNGYT9FEXVKsTPJS7okkpGjUI2SDSmnA857xxiLe5CTDJCJuo3LVFXTQtrB7bFGgpMwVSjszCQnUH6wzqwy606hzYKWZKFWZV8WrN3KPJOWz/JFUiGIfhuP/1qDmSYfTiDew3m6Zob6d6FrE69pKSkP4Lmj0ly7k3xeV5XA9fw8fn8ATl5fkdqvP323C5QvbyGorhRIXVQn5NAU1MFDOyBH3/Aw==",
              BinaryEncoding.Base64
            ),
            Compression.Deflate
          )
        ),
        let
          _t = ((type nullable text) meta [Serialized.Text = true])
        in
          type table [Transaction_Comment = _t]
      ),
      #"Added Custom4" = Table.AddColumn(
        Source,
        "Custom.4",
        each Text.Trim(
          Text.AfterDelimiter(Text.AfterDelimiter(Text.Upper([Transaction_Comment]), "CHK"), "#")
        )
      ),
      #"Split Column by Character Transition" = Table.SplitColumn(
        #"Added Custom4",
        "Custom.4",
        Splitter.SplitTextByCharacterTransition(
          {"0" .. "9", "A" .. "Z", "-"},
          (c) => not List.Contains({"0" .. "9", "A" .. "Z", "-"}, c)
        ),
        {"CHK#"}
      )
    in
      #"Split Column by Character Transition"

    How to use this code: Create a new Blank Query. Click on "Advanced Editor". Replace the code in the window with the code provided here. Click "Done". Once you examined the code, replace the Source step with your own source.

20 Replies

  • I would first replace "CHK #" with "CHK#", and then you should be able to use your M code #2 for all scenarios.

     

     

    • meierli's avatar
      meierli
      Helper I

      I tried and it didn't work since it wont captutre all the different spelling outlined above in the 4 different scenarios.

      • lbendlin's avatar
        lbendlin
        Super User

        Please provide sample data that fully covers your issue.
        Please show the expected outcome based on the sample data you provided.

  • thank you. how many columns will your code create? i was trying to avoid spliting the text. all i want is just to extract the check number, which can contain number, letters and - , plus be of variable length.

     

    also, what is all this stuff?

    Json.Document(
    Binary.Decompress(
    Binary.FromText(
    "jY/NaoNQEEZfZdCtifNzr/eOu0RNI20TUUMI4iJQS6E0hTYU+vbVdidZdBbf6hw403VBtr0PAcUtkFTESWI9tKeqSEGdB6SYNGYT9FEXVKsTPJS7okkpGjUI2SDSmnA857xxiLe5CTDJCJuo3LVFXTQtrB7bFGgpMwVSjszCQnUH6wzqwy606hzYKWZKFWZV8WrN3KPJOWz/JFUiGIfhuP/1qDmSYfTiDew3m6Zob6d6FrE69pKSkP4Lmj0ly7k3xeV5XA9fw8fn8ATl5fkdqvP323C5QvbyGorhRIXVQn5NAU1MFDOyBH3/Aw==",
    BinaryEncoding.Base64
    ),
    Compression.Deflate
    )
    ),
    let
    _t = ((type nullable text) meta [Serialized.Text = true])
    in
    type table [Transaction_Comment = _t]
    ),

     

    • lbendlin's avatar
      lbendlin
      Super User

      My code will create a single column.

       

      "that stuff"  is just your sample data in a fancy encoding.

      • meierli's avatar
        meierli
        Helper I
        thank you. do i need to include all of that fancy encoded data "stuff" when copying the code?