Recommend a guide to M language?

gazpage

Active Member
Joined
Apr 17, 2015
Messages
393
Can anyone point me to a good guide to the M language? I have M is for Data Monkey, which is good, but I need the next level I think.

In particular I would like to understand 'each' and '_' well enough to apply them myself. I can usually read code that includes them well enough, but not write my own.
 

Excel Facts

Return population for a City
If you have a list of cities in A2:A100, use Data, Geography. Then =A2.Population and copy down.
Actually I'm working on a guide, but it won't be available shortly.

But I can share the draft text about 'each' and '_':

When performing conversions on structured values (lists, records, tables), the conversions are actually applied to each element of the structured value. So when adding a column to a table, each value in the new column is determined using the same conversion function.
An example: add column “b” to a table as 10 * the values from column “a”:
Code:
Added_b = Table.AddColumn(MyTable,”b”, each 10 * [a])
The parameter starting with each is actually a function, according to the syntax of Table.AddColumn:
Code:
Table.AddColumn(table as table, newColumnName as text, [B]columnGenerator as function[/B], optional columnType as nullable type) as table
So this querystep can be rewritten as:
Code:
columnGenerator = each 10 * [a],
Added_b = Table.AddColumn(MyTable, "b", columnGenerator)
In fact, keyword each is equivalent with function parameter _, so the first function can be rewritten as:
Code:
Added_b = Table.AddColumn(MyTable,”b”, (_) => 10 * [a])
Which is equivalent with:
Code:
Added_b = Table.AddColumn(MyTable,”b”, (_) => 10 * _[a])
It is allowed to omit the _ parameter, but this is restricted to _ only, so this won’t work:
Code:
Added_b = Table.AddColumn(MyTable,”b”, (x) => 10 * [a])
The correct code:
Code:
Added_b = Table.AddColumn(MyTable,”b”, (x) => 10 * x[a])
 
Upvote 0
Sounds great. Where will the guide be when it is ready? Will it be available to buy?

Thanks for the example, but I am still not sure I follow it. Maybe I can't even use 'each' for the thing I was trying to do anyway. What I was trying to do was remove the space from all of my column names.

Doing it manually I ended up with part of the code line that looked like:

Code:
{{"John Smith","JohnSmith"},{"Jane Doe","Jane Dow"}}

So a list of lists. I created a list of the source column names and a list without the space easily enough, but wasn't able to work out how to combine them together. Is this the kind of thing that could be done using each'?
 
Upvote 0
A bit strange that you provided a similar answer here.

Anyhow, this can be done with:

Code:
= Table.TransformColumnNames(PreviousStep, each Text.Remove(_," "))

in which "PreviousStep" is the name of yourpevious step.
 
Upvote 0
Sorry for any confusion. That answer is just from when I was googling and doesn't represent any understanding on my part.

I am trying to improve my understanding so that I develop the ability resolve other problems that I have. Another recent problem I had was with an Expand Columns step but I didn't know what the underlying columns are ahead of time. When you Expand Columns using the UI you end up with the actual Column names in your code. I think I eventually found an answer to that as well via Google or a kind stranger but this really shouldn't be beyond me.

I will be reviewing your earlier response in detail and hopefully I can get to grips with it.

Thanks again for your help.
 
Upvote 0

Forum statistics

Threads
1,216,101
Messages
6,128,840
Members
449,471
Latest member
lachbee

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top