I was chatting with my friend Scott of www.tinylizard.com today, discussing the structure of the M language, and how it’s kind of weird compared to VBA, C# and such. Well, “what the #?”, I figure. I’m going to do a blog post on what the # sign actually signifies today, as it’s kind of confusing when you’re just starting with M.
A Little M Code:
I knocked up a bit of M code today to illustrate the challenge for someone just getting into this language. Here’s a snap of the beginning of each line:
Okay, so every line starts with something followed the = character… but…
What the #?
The question that I’m looking at specifically is… why do some lines start with # and some don’t? (The second lines starts Source = and the fourth start ReplacedValue = but all others start with the # character.)
The secret is all in the step name. Check out the recorded steps for this query:
Notice anything about the two highlighted entries compared to the others? No spaces!
When Power Query comes up against a space in the code it expects that there is another command or parameter coming next. To deal with this, we need to tell Power Query that “Hey, I want you to literally use this…” I’ve always heard this referred to as “escaping literals”, and M isn’t the only language that this happens in.
But it’s not sufficient to just use the # sign either. It needs to be the # sign followed by two sets of quotes… kind of like how we refer to text in formulas in Excel. So basically what you get is:
#”Filtered Rows” =
I Don’t Like It!
Yeah, me either. I think it makes the code look horribly ugly. It’s easy to avoid if you want to though, although it takes a little work. Just right click the step, choose Rename, and remove the space:
At this point your M code will be updated to remove the literals, and you’ll be back to cleaner code.
You could also edit the M code manually to remove the # and the leading and trailing quotes. If you do this, however, just make sure that you get all instances of them, as there will always be two in your recorded code, and possibly more if you’ve been manually tweaking it:
While I’m not a big fun of ugly code, I’ve also got to accept that I’m one of a very small percentage of users who will actually read it. Most people will use the UI for this kind of stuff, so making it read easier there is probably the right design decision.
At any rate, now you know what the # character is all about.