Custom Functions in M
Part of the Power BI Roadmap
In short
Twelve monthly files, all needing the same six cleaning steps. A custom function lets you write those steps once and run them on every file.
A custom function is a query that takes inputs, letting you apply the same transformation to many files or tables. The Folder connector creates one automatically.

Think of it like this
A masala grinder
You don't grind each spice with a different method. You put whatever goes in (cumin, coriander, chilli) into the same grinder and get powder out. A custom function is that grinder: one set of steps, any input you feed it.
Watch it happen
One function, many files
1/4Folder.Files lists every file in the folder, with its binary Content.
The big ideas
What you need to know
The => arrow
(file as binary) as table => ... defines inputs on the left and the body on the right.
A function is a query
Save the function as its own query, like CleanFile, and call it from any other query.
Combine Files makes one
The Folder connector's Combine Files button generates a helper function and a sample file query for you.
Type your inputs
Declaring as binary or as table catches wrong inputs early with a clear error.
See it in code
Walk through the code
1// Query named CleanFile2(file as binary) as table =>3let4 Source = Csv.Document(file, [Delimiter = ",", Encoding = 65001]),5 Promoted = Table.PromoteHeaders(Source, [PromoteAllScalars = true]),6 Typed = Table.TransformColumnTypes(Promoted, {{"OrderDate", type date}, {"Amount", type number}})7in8 Typed9 10// Query named AllMonths11let12 Files = Folder.Files("D:\Reports\Monthly"),13 CsvOnly = Table.SelectRows(Files, each [Extension] = ".csv"),14 Cleaned = Table.AddColumn(CsvOnly, "Data", each CleanFile([Content])),15 Combined = Table.Combine(Cleaned[Data])16in17 Combined1/5The function takes one binary (a file's contents) and promises to return a table.
Two queries: a function that cleans one file, and a query that applies it to every CSV in a folder.
Save yourself the pain
Common mistakes
Oops
Copying the same steps into twelve separate queries.
Do this instead
Write one function and apply it to all files. A fix then happens in one place.
Oops
Using ExpandTableColumn or Combine when files have different column names.
Do this instead
Clean and rename columns inside the function first, so every output table has the same shape.
Your mission
Write your first function
Quick check
What does this line define? (t as table) as table => Table.Distinct(t)
Go deeper
- 1Turn a cleaning query into a function with Create Function.
- 2Invoke it on every file in a folder.
- 3Read the function the Folder connector generated to see how it works.
More topics at this stop
Stuck on this topic? Ask an Insider
Get 1:1 guidance from people who've walked this exact path — on the InsideEdge app.