Nested functions in Power Query (text into date)
Hey there!
I'm a bit lost with M syntax when nesting functions.
I regularly parse monthly reports with Power Query where the current month is a text field composed of the last two letters of the year and the month, like for example 2411 is November of 2024. I do not like that. I always want date to be a proper date, so I would try and convert 2411 to 1/11/2024 (in d/m/y format).
An easy solution for this with short reports is to create a column from example where I plug in the date I want in a few rows and PQ usually gets the idea pretty fast. But when it comes to longer tables this often does not work. It seem to work for rows that I see but when I load up the query following rows are often wrong or just empty. So it is safer to use a function.
What works for me is to create multiple columns.
First I create a [year2] column with =Text.Start([yearmonth],2) which I then set to number.
I then create a [year4] column by simply using = [year2]+2000 (just ignore the last century for now. I haven't yet received date from before 2006 in more than a decade. I don't even think that data exists anywhere anymore.)
Then I create a [month2] column with =Text.End([yearmonth],2) and set it also to number.
And finally I tie it all together with =#date([year4],[month2],1) I will set it as date and eventually format it so that only the year and the month will show.
But this seems overly complicated and tiresome, so I'd like to do it all in a single step.
But when I want to do that with
= #date( Text.Start([yearmonth],2) + 2000 , Text.End([yearmonth],2) , 1)
I only get an error. Even when I turn to usual Excel solutions, such as multiplying the result of a text function with 1 to get a number as a result. It would work in plain Excel, it does not work in M.
How can I tie text function results together into a nice, lovely date? How can I trick M to accept a text as a number?
Thanks a bunch in advance!
[link] [comments]
Want to read more?
Check out the full article on the original site