LEFT/RIGHT in 5 languages (VBA/SQL/PYTHON/M query/DAX powerBI) | by Aymen | Towards Data Science

Excel is a powerful spreadsheet used by most people working in data analysis. The increase of volume of data and development user-friendly tools is an opportunity of improvement of Excel reports by mixing them with another tool or language.
As working for a financial reporting department I faced the need to boost our reporting tools. A simple way to start working with a new language is to translate what we use to do in excel, in another language. “How can I pivot this?”, “How can I vlookup that ?”.
In this article I will share with you how you can make LEFT/RIGHT in 5 different languages: VBA, python, SQL, DAX (Power BI), M (Power query). it will simple tips but if you want to get more detailed article don’t forget to follow me!
The Excel LEFT function extracts a given number of characters from the left side of a supplied text string. RIGHT do the same but from the right. The syntax is as follow:
=LEFT (text, [num_chars])
Text: the text from which to extract characters.
num_chars: The number of characters to extract, starting on the left side of text. If you do not specify, by default it is 1.
If we take a practical example where in cell “A1” we have the phrase “This is an example”:
If we want to isolate “This” we put as argument “A1” and 4 characters from LEFT, and if we want to isolate “example” we put “A1” as argument in RIGHT and specify 7 characters.
Visual Basic for Application (VBA) is an implementation of Microsoft Visual Basic integrated into Microsoft Office applications.
Applying LEFT/RIGHT in VBA is so easy that I even hesitated to present it. But let’s enjoy this simplicity. In the cell where we want to apply the formula, we will just apply the formula. Actually we will write the formula in the cell and get the result directly.
Range("C1") = left(Range("A1"), 4)
SQL ( Structured Query Language) or sequel, is a standard language for storing, manipulating and retrieving data in databases. It is one of the common upgrade done by companies that face limits with Excel. Usually, the first reaction is to negotiate some budget in order to store the data into a database and use SQL to “speak” with this database and organise, manipulate the data. The language is also highly appreciable. Close to a natural language, you don’t feel coding when typing simple SQL request.
LEFT/RIGHT in SQL is very close to the Excel formula. In practice, let’s take the table we created for the VLOOKUP in 5 languages. It was a table with items and prices:
CREATE TABLE table1 (    Item varchar(255),    Price int    );INSERT INTO table1VALUES ('Item1', 4);INSERT INTO table1VALUES ('Item2', 12);INSERT INTO table1VALUES ('Item3', 56);CREATE TABLE table2 (    Item varchar(255),    Price int  );
Now let’s create 2 columns, one with just “item” isolated using left and one with the item number (just the number) using right.
The general template is:
LEFT(field_name, number of characters to extract from the left)
And applied to our example:
SELECT LEFT(Item,4) AS Product FROM table1;
Same with numbers:
SELECT RIGHT(Item,1) AS Type FROM table1;
Additional tip: In sql we can easily extract the characters in the middle of a set of data using the formula SUBSTRING
SUBSTRING(field_name, starting position, ending position relative to the starting position)
Python is an interpreted, high level language with a generic purpose. It is used in a large range of application, including data analysis. We can present python by saying “For all application its libraries”. And for data, without surprise, we will use the famous Pandas.
In our example we will make the extraction as string (text). Let’s first create a dataframe(df) with our items and prices:
import pandas as pditems = {'Item': ['Item1','Item2','Item3'], 'Price': [4, 12,56]}df = pd.DataFrame(data=items)
Now to get the data we will apply .str[:x] for the left and .str[-x:] for the right. Here the colon x (:x) means from the first character to the number x, and applying a minus x means starting from le last character.
Left = df['Item'].str[:4]Right= df['Item'].str[-1:]
Left will return only “Item” and Right will return the number.
Additional tip: As we explained in SQL, we can with python also isolate the middle of the data we want to extract using the same .str[x:y]. It means from x to y, but be careful, here x is not counted (for example .str[4:6] means character fifth and sixth);
M is the powerful language behind the tool power query. Even if you are working on the query editor, every single step will be written in M. M stands for Data Mashup or Data Modeling. I highly recommend to have a look at this language, we can do so much more than just using the graphical interface.
To extract the left side or right side of a text in M we just have to create a simple expression. The example we will used will be the one used in VLOOKUP in 5 languages.
Once your table is updated, you can use the graphical interface and clic on add column:
Then during the customisation of the the column, just apply the formula text.start to simulate left, or text.end to simulate right. In the aguments you will put the column name and the number of character:
Text.Start([Item], 4)Text.End([Item], 1)
So here text.start will return “Item” and text.end will return only the number.
DAX stands for Data Analysis Expressions. More than a language, it is a library of functions and operators that can be used to build formulas in Power BI and Power Pivot.
This part will be really short as the model is exactly the same as the Excel formula. You have in DAX 2 formulas, LEFT and RIGHT with exactly the same arguments :
LEFT(<text>, <num_chars>)RIGHT(<text>, <num_chars>)
This article is a part of a set of articles where I share my way of doing an Excel feature in other languages. For sure, when you decide to move from Excel to programming, your approach of data analysis will totally change and you will learn that there are tons of way to get to your solution. If you have another manner to do this or even know how to do it with other languages, feel free to share them in the comments or by contacting me in my social networks! I will be pleased to read your thoughts!