# New table using M Query

How to create a new table and retrieve distinct values from an existing column in another table using M Query?

There are not many courses around that teach M Query to an advanced level, so I am wondering how best to learn the M language?

Thank you!

data_PBI.zip (60.2 KB)

You can use the Table.Distinct M function it requires a table and returns a table.

Now if you input a single column from a table that will return a type List, to return a type Table just add another set of square brackets.

Your Query will look something like:

Source = Table.Distinct(#"My query[[MyColumnName]])
.

1 Like

Thanks Melissa,

I guess I should have asked the more complex version from the start, which is how to do this same thing for more than one column and perhaps also for more than one table. Iâ€™m guessing that one can use the same code batch for more than one table?

Thanks!

Hi

Have you tried the steps explained in this video by Sam McKay?

[Understanding M Code](

Understanding M code | Enterprise DNA Online

)

Regards

Najah

1 Like

Unable to fully automate this process but this is what Iâ€™ve got for you:

1. Turned your Table into a List containing a List of each Column
2. Added a Column where I transformed each List into a List that only contains distinct values
3. Now hereâ€™s the part that I havenâ€™t been able to automate, next for each ListDistinct List value I had to add that as a new query

You only have to set it up once but Iâ€™m hoping you donâ€™t have to split up 50, 20 wide column tablesâ€¦

eDNA - New table using M Query.pbix (56.1 KB)

1 Like

Slightly different approach from Melissa . PFA the solution.

1. This will give Tables for each Columns instead of Lists.
2. Then you can either use â€śAdd as New Queryâ€ť or simply extract the required Table using
Source = Table1{0}[Distinct]

Regarding Step 3 to create individual queries from List or Table, donâ€™t think is possible to automate.

Regards
Ankit J

1 Like

Najah
Yes, Iâ€™ve gone through the video and it provides great tips, thanks.

Melissa
This is very helpful, so thanks for your great input. I will see if I can take it further.

Ankit
Yours is also helpful, so thanks for your input. I like that they are in tables.

You have all shown me a way to do this so thank you all for such great help. I am not sure if all your suggestions can be marked as the solution, so Iâ€™ll have to choose the one that came first.

1 Like