Forum Discussion

afaber's avatar
afaber
Regular Visitor
7 months ago
Solved

Adding a new column in PowerBI

Hello,

I'm trying to find a way to quickly analyse a column in my table that contains various veriations of the word "voucher" in it and then create a column with a specific description, e.g. "Voucher Issue" in it.

Examples of the data I'm analysing from the [Case_Title] column

 

Complaint - voucher

 

This is my Power Query statement = Table.AddColumn(#"Changed Type", "Case_Category", each if Text.Contains([Case_title], "Voucher") then "Voucher Issue" else "Other"), it runs, but returns the value of Error in the new columnn.

What am I doing wrong?

Thanks

  • Thanks, clicking on the Error column gave me insight to the problem!  Appreciate the help.

9 Replies

  • Column name is case-sensitive and If any rows have null in Case_Title, Text.Contains will throw an error. Try this instead:

    Table.AddColumn(
        #"Changed Type",
        "Case_Category",
        each
            if [Case_Title] <> null
               and Text.Contains([Case_Title], "Voucher", Comparer.OrdinalIgnoreCase)
            then "Voucher Issue"
            else "Other"
    )

     


  • Hallo,

     

    die Anweisung ist nicht zu beanstanden. Wie lautet denn die Fehlermeldung?

  • afaber's avatar
    afaber
    Regular Visitor

    Thanks, I tried that, but it's still returning the word Error in the results of the column.

    • BA_Pete's avatar
      BA_Pete
      Super User

      Hi afaber ,

       

      Can you click in the cell that contains the 'Error' word please? Don't click the actual word itself, just the white space in the cell around it. This should display the error message at the bottom of the screen.

      Please let me know what the error says.

       

      Pete

      • afaber's avatar
        afaber
        Regular Visitor

        Thanks, clicking on the Error column gave me insight to the problem!  Appreciate the help.

  • v-dineshya's avatar
    v-dineshya
    Community Support

    Hi afaber ,

    Thank you for reaching out to the Microsoft Community Forum.

     

    If your column contain errors, nulls  or unexpected values, Please try below M code to fix the issue.


    Table.AddColumn(
    #"Changed Type",
    "Case_Category",
    each
    try
    if Text.Contains(
    Text.From([Case_Title]),
    "voucher",
    Comparer.OrdinalIgnoreCase
    )
    then "Voucher Issue"
    else "Other"
    otherwise "Other",
    type text
    )


    Below M code handles nulls and case variations.

     

    = Table.AddColumn(
    #"Changed Type",
    "Case_Category",
    each
    if [Case_title] <> null
    and Text.Contains([Case_title], "voucher", Comparer.OrdinalIgnoreCase)
    then "Voucher Issue"
    else "Other",
    type text
    )

     

    I hope this information helps. Please do let us know if you have any further queries.

     

    Regards,

    Dinesh