For some time it’s bothered me that we don’t have full parity between Excel and Power Query functions. In this post I’m going to look at four Excel text function equivalents, intended to allow an Excel user to just work with formulas as they’re used to.
=LEFT()
The first function is the LEFT function which, if implemented in M, would be:
(text as text,num_chars as number)=>
let
ReturnValue = Text.Start(text,num_chars)
in
ReturnValue
You’d call it like so:
=LEFT([Word],4)
And it would return the Excel text function equivalent as shown here. (Excel’s output is in teh LEFT(x,4) column, and the Power Query LEFT function is shown in the pqLeft column.)
This one may not be a big deal, as it’s just a new function name, but it now works exactly as an Excel user would expect. (Except for the fact that you MUST type the function name in uppercase.)
=RIGHT()
We can do the same thing with the RIGHT function:
(text as text,num_chars as number)=>
let
ReturnValue = Text.End(text,num_chars)
in
ReturnValue
And call it with:
=RIGHT([Word],4)
Returning:
=LEN()
This is yet another simple one:
(text as text)=>
let
ReturnValue = Text.Length(text)
in
ReturnValue
Called with:
=LEN([Word])
Returning:
Why I care about Excel text function equivalents?
Beyond allowing a user to just use the knowledge they’ve been investing in for years, it’s the next function that makes a difference. With the MID function we have a bunch of things that are different, and to replicate our expectations we need to do some manipulation and conditional testing.
=MID()
This is what it takes to replicate the MID() function in Power Query:
(text as text,start_num as number,num_chars as number)=>
lettLength = Text.Length(text),
StartCorrected = if start_num>tLength then -1 else start_num-1,
ReturnCorrected = if (StartCorrected+num_chars) > tLength then tLength - start_num + 1 else num_chars,
ReturnValue = if StartCorrected >-1
then Text.Range(text,StartCorrected,ReturnCorrected)
else null
in
ReturnValue
That’s a fairly big pain to just knock up in a couple of seconds! But once done, it’s easy to use:
=MID([Word],5,4)
And it returns results consistent to Excel as well:
Final Thoughts
I really wish these functions were built under and consistent with Excel’s function names to begin with. While we can replicate their form and function, it takes a lot of work to create/debug and implement them in a power query solution. In addition, some of the functions are VERY difficult to built. But regardless, at least we can.
The post Excel Text Function Equivalents appeared first on The Ken Puls (Excelguru) Blog.