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 in 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 LEFT Excel Function, it is not supported in M. But I have found a way for us to replicate the LEFT Function in M!
Key Takeaways
- While Power Query’s M language does not natively support Excel’s LEFT function, using a custom column formula with Text.Start will make this work. This approach allows users to perform similar text manipulations as they would with the Excel function.
- With the M language, you simply specify the column and the number of characters to get.
Table of Contents
Step By Step Guide
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 2016:
Excel 2013 & 2010:
STEP 3: This will open up the Power Query Editor.
Go to Add Column > Add Custom Column
We want to get the first 3 characters of the Sales Month:
STEP 4: Let us create a simple M expression to replicate the LEFT function in Excel.
In the New column name text box, type SALES MONTH (Shortened)
In the Custom column formula, type in: Text.Start(
From the Available columns choose SALES MONTH and Insert
Then finish off the formula by entering , 3)
We now have build the following formula:
Text.Start([SALES MONTH], 3)
So lets quickly break down what we just did:
- We are using the Text.Start formula to get the first X characters of the SALES MONTH column
- We place in 3, to specify that we want the first 3 characters.
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 LEFT function!
Practical Applications
Solving Real-World Data Transformation Challenges
When facing real-world data challenges, Power Query’s transformation tools, including your newly-acquired expertise in the LEFT function replication, come to the rescue. Imagine having a dataset where you need to separate product codes from a mixed description field. This is where employing the Text.Start
function, you can accurately extract the precise substring needed, simplifying the process massively.
Moreover, conditional column creation can extend the LEFT function logic to cater to varying lengths of product codes or implementing error handling when dealing with empty or null values.
Using these skills, you can transform data that would otherwise require complex formulas or manual interventions. It’s about applying the right combination of Power Query features to yield clean, useful data ready for analysis or reporting.
Incorporating LEFT Function Logic in Complex Queries
Incorporating the LEFT function logic into complex queries requires a mix of creativity and logical structuring. Let’s say you’re dealing with a multi-step data transformation that includes filtering, parsing, and ultimately aggregating data. In such a query, extracting the leftmost characters from a string is often just one link in the chain of data transformation.
By nesting the Text.Start
call within other M functions or custom steps, you can feed its output into subsequent transformations seamlessly. Perhaps you need to categorize data based on certain prefixes or create dynamic groupings – your LEFT logic becomes the foundational step that supports these intricate operations. Always remember to keep performance in mind, ensuring that your query runs smoothly even as it grows in complexity.
Enhancing Efficiency
Custom Functions: Automating Repeated LEFT Operations
Custom functions are your best friend when it comes to automating those repetitive LEFT operations in Power Query. Think of them as your personal assistant, ready to take on the mundane tasks, freeing up your time for more strategic data analysis. Crafting a custom function based on the Text.Start
technique is like setting up a template — you define the logic once and then simply call the function wherever needed in your other queries.
Not only do these custom functions save you time, but they also ensure consistency across your data transformation processes. Need to extract the first four characters from multiple columns or different queries? No problem. Your custom function can be invoked repeatedly without having to rewrite the logic every single time.
And the beauty of this approach? Should you ever need to adjust the extraction logic, you only have to update the custom function in one place, and the changes propagate throughout your queries. Efficiency and maintainability, all in one go.
Optimizing Performance for Large Datasets
When you’re wrangling large datasets in Power Query, optimizing performance becomes a priority. If you’ve ever felt like your queries were running slower than molasses, it’s time to fine-tune your approach. Replicating the LEFT function with Text.Start
can add unnecessary load if not used judiciously. To keep your queries lightning-fast, consider filtering down your data before applying text functions.
Remember, each transformation step is another calculation for Power Query to execute. By minimizing the rows processed before text extraction, you lessen the work that Power Query has to churn through. It’s also wise to limit the number of calculated columns and use them later in the query only if absolutely necessary. These strategies reduce the strain on your system, ensuring that even the most massive datasets remain under your control and responsive to your every command.
Common Pitfalls and How to Avoid Them
Troubleshooting Power Query Errors Related to LEFT Function
Encountering errors in Power Query, especially when replicating Excel functions like LEFT, can be a bump in the data road, but it’s nothing you can’t handle. The first step in troubleshooting is to verify your code syntax in the Text.Start
function you’re using to emulate LEFT. Ensure that the number of characters you’re specifying to extract isn’t larger than the string itself, as this may lead to out-of-range errors.
Then, consider the data type involved. Is the column you’re manipulating actually recognized as text? Power Query can be particular about data types, so a quick data type conversion might be the fix you need.
Finally, check for any extra spaces or hidden characters that might throw off the character count. A smart combo of Text.Trim
and Text.Clean
before using Text.Start
can clean up the text, minimizing chances for errors.
Best Practices for Maintainable and Readable M Code
Maintainable and readable M code is a Power Query artist’s masterpiece. It’s not just about getting the job done, it’s about crafting your M code so that anyone who follows can understand and manage it with ease. To start, name your steps intuitively; ‘ExtractFirstName’ tells a clearer story than ‘Step1.’ Commenting your code is another hallmark of best practice; it’s your chance to explain the ‘why’ behind the code, not just the ‘what.’
Additionally, keep your transformations as modular as possible. Smaller, self-contained steps are easier to debug and update than monumental, complex ones. Think of it as building with Lego blocks rather than sculpting from a single slab of marble.
The key takeaway? Write your M code as if you were to hand it off to someone else tomorrow — clarity, simplicity, and documentation are your guiding principles.
FAQ: Mastering LEFT Function in Power Query
How Can I Ensure Accurate Replication of Excel’s LEFT Function in Power Query?
To ensure accurate replication of Excel’s LEFT function in Power Query, use the Text.Start
function. It requires two parameters: the text you’re extracting from and the number of characters to extract. Make sure the column data type is text and that the number of characters specified does not exceed the length of the text. Always test your function with a variety of data samples to confirm its accuracy.
What Are Some Common Mistakes When Using Text.Start in Power Query?
Some common mistakes when using Text.Start
in Power Query include not converting data to text format, extracting more characters than the string length, and overlooking null or blank values which may cause errors. Always verify the data type and handle special cases to avoid these pitfalls.
Bryan
Bryan Hong is an IT Software Developer for more than 10 years and has the following certifications: Microsoft Certified Professional Developer (MCPD): Web Developer, Microsoft Certified Technology Specialist (MCTS): Windows Applications, Microsoft Certified Systems Engineer (MCSE) and Microsoft Certified Systems Administrator (MCSA).
He is also an Amazon #1 bestselling author of 4 Microsoft Excel books and a teacher of Microsoft Excel & Office at the MyExecelOnline Academy Online Course.