Forum Discussion

khisla's avatar
khisla
Helper II
1 year ago
Solved

Error message when extracting part of a string

I am ussing the below to extract data from a sting

 

= if Text.Contains([Test String], "IL")
then Text.Middle([Test String], Text.PositionOf([Test String], "IL"), 5)
else null

 

However, when the Test String is blank / null I am getting an error message.

Is there a way I can adjust this to take that into consideration?

  • khisla Maybe:

    if [Test String] = null then null else if Text.Contains([Test String], "IL")
    then Text.Middle([Test String], Text.PositionOf([Test String], "IL"), 5)
    else null

2 Replies

  • Greg_Deckler's avatar
    Greg_Deckler
    Community Champion

    khisla Maybe:

    if [Test String] = null then null else if Text.Contains([Test String], "IL")
    then Text.Middle([Test String], Text.PositionOf([Test String], "IL"), 5)
    else null
  • let
    Source = #table(
    {"Test String"},
    {
    {"ABCDE IL12345 XYZ"},
    {"Something else"},
    {"IL67890 text after"},
    {"No IL here"},
    {"Another IL99999 case"},
    {null}
    }
    ),
    AddExtracted = Table.AddColumn(Source, "Extracted", each try
    if Text.Contains([Test String], "IL")
    then Text.Middle([Test String], Text.PositionOf([Test String], "IL"), 5)
    else null otherwise null
    )
    in
    AddExtracted