Forum Discussion

Anonymous's avatar
Anonymous
Not applicable
8 years ago
Solved

Power Query - Remove last two characters from a column

Hi,

 

Simple question. How to remove last two characters from each field of the column, i.e. replicate Excel's LEFT("column";LEN(column)-2) in M language?

 

Thanks!

  •  

    Use Split

     

    Right mouse on col, select split, then by char by position. Enter 2 and pick once from right. You'll get 2 columns so delete the one you don't want.

     

    The M /Power Query code looks like this.

     

    = Table.SplitColumn(#"Removed Columns1", "Field", Splitter.SplitTextByPositions({0, 2}, true), {"Field.1", "Field.2"})

     

     

  • Hi Anonymous,

     

    As explained you can use this formula in M:

    Text.Start([column];Text.Length([column])-2)

    That means your column is a text and it is not empty or null otherwise you need to add a condition...

     

    Hope it helps...

     

    Ninter

8 Replies

  • stretcharm's avatar
    stretcharm
    Memorable Member

     

    Use Split

     

    Right mouse on col, select split, then by char by position. Enter 2 and pick once from right. You'll get 2 columns so delete the one you don't want.

     

    The M /Power Query code looks like this.

     

    = Table.SplitColumn(#"Removed Columns1", "Field", Splitter.SplitTextByPositions({0, 2}, true), {"Field.1", "Field.2"})

     

     

    • baravo's avatar
      baravo
      Helper I

      A bit clearer explanation:

       

      inPower Query, use Split Column by Number of Characters. Enter 2 (or the number of characters you need to split) and pick "Once, as far right as possible".

       

    • suzannek's avatar
      suzannek
      Frequent Visitor

      Thank you for this answer. You saved me tons of time!

  • jthomson's avatar
    jthomson
    Solution Sage

    Text.Length is the M equivalent to LEN, if you use that in combination with Text.RemoveRange you should be able to do what you want

    • Interkoubess's avatar
      Interkoubess
      Solution Sage

      Hi Anonymous,

       

      As explained you can use this formula in M:

      Text.Start([column];Text.Length([column])-2)

      That means your column is a text and it is not empty or null otherwise you need to add a condition...

       

      Hope it helps...

       

      Ninter

      • Anonymous's avatar
        Anonymous
        Not applicable
        Thank you for multiple helpfuö answers!