Forum Discussion

DanFromMontreal's avatar
4 years ago
Solved

Replace TEXT in a column using IF

Is it possible the create a Customized Function that will NOT add a new column but rather do what it is suppose to do directly on the column selected.  Let me explain... I want to be able to replace...
  • BA_Pete's avatar
    4 years ago

    Hi DanFromMontreal ,

     

    You would use the standard Replace Values function from the transform tab on your column that you want values to be replaced in, but in the 'text to find' and 'replace with' boxes, you would just add random values, let's assume "aaa" and "bbb". This sets up the query step for you nicely.

     

    You then edit the step something like this:

     

    //Change your step that looks like this:
    = Table.ReplaceValue(
        previousStep,
        "aaa",
        "bbb",
        Replacer.ReplaceText,{"columnName"}
    )
    
    //To something like this:
    = Table.ReplaceValue(
        previousStep,
        each [columnName],
        each if [columnName] = "A" then [columnA]
        else if [columnName] = "B" then [columnB]
        else if [columnName] = "C" then [columnC]
        else [columnName],
        Replacer.ReplaceText,{"columnName"}
    )

     

     

    If this doesn't seem to do anything, then change the last 'Replacer.ReplaceText' to 'Replacer.ReplaceValue'.

     

    Pete