Power Query lets you perform a series of steps to transform your Excel data. There are times when we want to do things that are not built into the user interface.
This is possible with Power Query’s programming language, which is M.
Unfortunately, not all of Excel’s formulas can be used in M.
For example, if we want to use the Excel RIGHT Function, it is not supported in M.
But I will show you how we can replicate the RIGHT Function Power Query in M and the Text.End formula!
In this example, you can see there is a column I mentioning quarter. You want to extract the last character from the text. QTR1 mentioned in cell I2, you want to extract the number 1.
You can use the Text.End function in Excel PowerQuery to returns the number of characters from the end of a text value.
Syntax of Text.End Function
The syntax of Text.End Function is:
- text – the cell from which you want to extract characters
- num_chars – number of characters you want to extract
Watch it on YouTube and give it a thumbs-up!
Follow the step-by-step tutorial on Replicating Excel RIGHT Function with M in Power Query and download this Excel workbook to practice along:DOWNLOAD EXCEL WORKBOOK
Let’s go through the steps in detail:
STEP 1: Select your data and turn it into an Excel Table by pressing the shortcut Ctrl + T or by going to Insert > Table
STEP 2: Go to Data > Get & Transform > From Table (Excel 2016) or Power Query > Excel Data > From Table (Excel 2013 & 2010)
Excel 2013 & 2010:
STEP 3: This will open up the Power Query Editor.
Select the SALES QTR column.
Go to Add Column > Add Custom Column
STEP 4: We want to get the last character of the SALES QTR cell values. For example: 1, 2, 3 or 4
So, let us create a simple M expression to replicate the Power Query RIGHT function.
In the New column name text box, type SALES QTR (Shortened)
In the Custom column formula, type in: Text.End(
From the Available columns choose SALES QTR and Insert
Then finish off the formula by entering, 1)
We now have built the following formula:
Text.End([SALES QTR], 1)
So let us quickly break down what we just did:
- We are using the Text.End formula to get the last X characters of the SALES QTR column
- We place in 1, to specify that we want the last 1 character.
Click OK to confirm.
Now you will see your changes take place.
STEP 5: Click Close & Load from the Home tab and this will open up a brand new worksheet in your Excel workbook with the updated values.
Congratulations! You have used a M formula for replicating the RIGHT function in Power Query!
Just like Text.End can be used to replicate the Excel Right function,
- Use Text.Start to replicate the LEFT function
- Use Text.Position to replicate the FIND function
- Use Text.Length to replicate the LEN function
Make sure to download our FREE PDF on the 333 Excel keyboard Shortcuts here: