Welcome, young data champion! In Module Five, we learned the basics of M language – variables, steps, and simple functions. Now it is time to level up. In this module, we will explore lists (like bags of items), records (like cards with details), and custom functions (like recipes you invent yourself). You will learn how to loop through data, transform lists, and build your own powerful tools. By the end, you will be able to write M code that can handle almost any data challenge!
Fatima loves fixing things. Her grandfather gives her a toolbox with many compartments. One compartment holds screws (like a list of values). Another holds instruction cards (like records with details). She also has a special tool that can turn any screw and any card into a new creation – that’s like a custom function!
In this module, you will build your own toolbox. You will learn to store data in lists, organize it in records, and create functions that can work on any data you give them. Let’s start building!
Definition: A list is a collection of values enclosed in curly braces { }.
Why important: Lists let you store many items together and work with them all at once.
Simple explanation: Like a bag of marbles – you can count them, add more, or take some out.
Real‑life example: A shopping list: {"milk", "bread", "eggs"}.
School example: A list of test scores: {80, 90, 70}.
Home example: A list of chores: {"sweep", "wash", "cook"}.
Nigerian example: A list of states: {"Lagos", "Abuja", "Kano"}.
let
MyList = {10, 20, 30, 40}
in
MyList
Mini summary: A list is a collection of items inside { }.
Definition: You can get an item from a list using its position (index), starting from 0.
Why important: You often need to pick one specific item from a list.
Simple explanation: Like picking the third marble from your bag.
Real‑life example: The first item on a list is position 0.
School example: In a list of scores, {80,90,70}, the second score is at index 1 (90).
Home example: Your chore list – index 0 is “sweep”.
Nigerian example: List of states – index 0 is “Lagos”.
let
MyList = {10, 20, 30},
FirstItem = MyList{0} // 10
in
FirstItem
Mini summary: Use {index} to pick an item from a list.
Definition: List.Transform applies a function to each item in a list and returns a new list.
Why important: It lets you change all items at once – like doubling every number.
Simple explanation: Like giving every marble a coat of paint – all at once.
Real‑life example: Convert all prices from naira to dollars.
School example: Add 5 marks to every score.
Home example: Double the quantity of each item in a recipe.
Nigerian example: Add VAT (7.5%) to every price.
let
MyList = {10, 20, 30},
NewList = List.Transform(MyList, each _ * 2) // {20, 40, 60}
in
NewList
Mini summary: List.Transform changes every item in a list.
Definition: A record is a set of fields with names and values, like a row in a table.
Why important: Records organize data with labels, so you know what each value means.
Simple explanation: Like a student ID card – it has Name, Age, and Class.
Real‑life example: A product record: [Product="Milk", Price=200].
School example: A student record: [Name="Ade", Score=80].
Home example: A chore record: [Chore="Sweep", Time="10am"].
Nigerian example: A state record: [State="Lagos", Population=20M].
let
MyRecord = [Name="Ade", Age=10, Class="P5"]
in
MyRecord
Mini summary: A record is a labelled collection of values inside [ ].
Definition: You can get a field’s value using the dot notation or Record.Field.
Why important: You need to extract specific information from a record.
Simple explanation: Like looking at the “Name” on an ID card.
Real‑life example: Get the price from a product record.
School example: Get the score from a student record.
Home example: Get the time from a chore record.
Nigerian example: Get the population from a state record.
let
MyRecord = [Name="Ade", Score=80],
StudentName = MyRecord[Name] // or MyRecord.Name
in
StudentName
Mini summary: Use [FieldName] to get a value from a record.
Definition: Record.Combine merges two or more records into one.
Why important: You may have information in separate records that you want to bring together.
Simple explanation: Like sticking two cards together to make a bigger card.
Real‑life example: Combine product details and price records.
School example: Combine student name and marks records.
Home example: Combine chore name and time records.
Nigerian example: Combine state name and capital records.
let
Rec1 = [Name="Ade", Age=10],
Rec2 = [Score=80, Class="P5"],
Combined = Record.Combine({Rec1, Rec2})
in
Combined // [Name="Ade", Age=10, Score=80, Class="P5"]
Mini summary: Record.Combine merges records.
Definition: A list of records is like a table – each record is a row.
Why important: Many data sources give you a list of records.
Simple explanation: Like a class register – each row is a student record.
Real‑life example: A list of products, each with Name and Price.
School example: A list of students, each with Name and Score.
Home example: A list of chores, each with Chore and Time.
Nigerian example: A list of states, each with State and Capital.
let
MyTable = {
[Name="Ade", Score=80],
[Name="Bola", Score=90],
[Name="Chidi", Score=70]
}
in
MyTable
Mini summary: A list of records is a great way to store tabular data.
Definition: You can use List.Transform on a list of records to change or extract fields.
Why important: It lets you manipulate each row of your data.
Simple explanation: Like going through each student and adding 5 marks.
School example: Add 10 to every score in a list.
Home example: Add a “Done” field to every chore.
Nigerian example: Add a “Region” field to every state record.
let
Data = {
[Name="Ade", Score=80],
[Name="Bola", Score=90]
},
Updated = List.Transform(Data, each _ & [Score = [Score] + 5])
in
Updated // scores become 85 and 95
Mini summary: You can transform lists of records row by row.
Definition: A custom function is a block of code you write to perform a specific task.
Why important: You can reuse the same code many times with different inputs.
Simple explanation: Like a recipe for “fried rice” – you can make it whenever you want.
Real‑life example: A function to calculate discount.
School example: A function to calculate average.
Home example: A function to convert currency.
Nigerian example: A function to add VAT.
let
AddTen = (x) => x + 10,
Result = AddTen(5) // 15
in
Result
Mini summary: Functions are reusable recipes in M.
Definition: A function can take more than one input, like (x, y) => x + y.
Why important: Many tasks need more than one piece of information.
Simple explanation: Like adding two numbers – you need both numbers.
School example: A function to calculate total (maths + english).
Home example: A function to calculate total cost (price * quantity).
Nigerian example: A function to calculate total with VAT.
let
Add = (x, y) => x + y,
Sum = Add(5, 3) // 8
in
Sum
Mini summary: Functions can have many inputs.
Definition: You can pass a custom function to List.Transform to apply it to every item.
Why important: It gives you complete flexibility.
Simple explanation: Like having a special machine that can do any job you tell it.
School example: Apply a “pass/fail” function to every score.
Home example: Apply a “discount” function to every price.
Nigerian example: Apply a “region” function to every state.
let
AddTen = (x) => x + 10,
MyList = {10, 20, 30},
NewList = List.Transform(MyList, AddTen) // {20, 30, 40}
in
NewList
Mini summary: Custom functions make List.Transform very powerful.
Definition: “try” attempts a step; if it fails, “otherwise” gives a fallback value.
Why important: It prevents your code from crashing when something goes wrong.
Simple explanation: Like wearing a helmet – if you fall, it protects you.
School example: If a score is missing, use 0.
Home example: If an item has no price, set it to 100.
Nigerian example: If a state population is missing, use “Unknown”.
let
SafeDivide = (a, b) => try a / b otherwise 0,
Result = SafeDivide(10, 0) // 0 (no error)
in
Result
Mini summary: try…otherwise keeps your code safe from errors.
Definition: A full function can have multiple steps inside using let…in.
Why important: Complex tasks need multiple steps.
Simple explanation: Like a recipe with many steps – mix, knead, bake.
School example: A function to calculate grade (A, B, C) from score.
Home example: A function to calculate total with tax and discount.
Nigerian example: A function to clean a name (capitalize, remove spaces).
let
Grade = (score) =>
let
GradeText = if score >= 70 then "A" else "B"
in
GradeText,
Result = Grade(85) // "A"
in
Result
Mini summary: Functions can have multiple steps inside.
Definition: Record.Field gets a field value; Record.HasField checks if a field exists.
Why important: Sometimes you don’t know if a field is present.
Simple explanation: Like checking if a student has a “Score” field before using it.
School example: Only add marks if the “Score” field exists.
Home example: Only add a price if the “Price” field exists.
Nigerian example: Only add a capital if the “Capital” field exists.
let
MyRecord = [Name="Ade"],
HasScore = Record.HasField(MyRecord, "Score") // false
in
HasScore
Mini summary: Record.Field and HasField help you work safely with records.
Definition: Recursion is when a function calls itself to solve a problem.
Why important: It is useful for repetitive tasks like summing a list.
Simple explanation: Like counting marbles one by one until you reach the end.
School example: Sum all numbers in a list.
Home example: Count all items in a shopping list.
Nigerian example: Count all states in a list.
let
SumList = (lst) =>
if List.Count(lst) = 0 then 0
else List.First(lst) + SumList(List.Skip(lst, 1)),
Result = SumList({10, 20, 30}) // 60
in
Result
Mini summary: Recursion is a function that calls itself – powerful but advanced.
CleanName = (fullName) => Text.Proper(Text.Trim(fullName))CleanName(" ade ojo ") returns “Ade Ojo”.Record.HasField(record, "FieldName") to check.Record.Field(record, "FieldName").List as a Bag of Marbles +-----+-----+-----+-----+ | 10 | 20 | 30 | 40 | +-----+-----+-----+-----+ Index: 0 1 2 3
Record as an ID Card +------------------+ | Name: Ade | | Age: 10 | | Class: P5 | +------------------+
List of Records (Table) +----------+----------+----------+ | Name | Score | Grade | +----------+----------+----------+ | Ade | 80 | A | +----------+----------+----------+ | Bola | 90 | A | +----------+----------+----------+
| Concept | Syntax | Use |
|---|---|---|
| List | {1,2,3} | Collection of values |
| Record | [Name="Ade"] | Labelled values |
| List.Transform | List.Transform(list, each _ * 2) | Change every item |
| Record.Combine | Record.Combine({rec1, rec2}) | Merge records |
You have now mastered lists, records, and custom functions in M! You can create lists of any type, access items by index, and transform entire lists with List.Transform. You know how to build records, combine them, and work with lists of records. You have written your own functions with parameters and even used error handling to make your code safe. You are now equipped to write powerful, reusable M code that can handle complex data challenges. Well done!
Match the M concept with its description:
| Concept | Description |
|---|---|
| 1. List | A. A collection of values in { } |
| 2. Record | B. A set of labelled fields in [ ] |
| 3. List.Transform | C. Applies a function to every item |
| 4. try … otherwise | D. Error handling |
Answers: 1-A, 2-B, 3-C, 4-D
Scenario 1: You have a list of product records: [Product="Rice", Price=1000], etc. Write M code to add a 10% tax to each product’s price.
Scenario 2: You have a record with Name, Age, and Score. Write a function that returns the grade based on the score.
Each group creates a list of 10 records (e.g., students with Name, Score). Then, using List.Transform and a custom function, they transform the list to add a “Grade” column. Groups compare their approaches.
Write a custom function that cleans a list of names (capitalizes each, removes extra spaces). Test it on a list of your friends’ names.
Build a Power Query that takes a list of state records (Name, Capital, Population). Use a custom function to add a “Region” column (e.g., South, North) based on the state. Output the transformed list.
Using a sample dataset of employees (Name, Department, Salary), write M code to: (1) Create a list of records, (2) Add a “Bonus” column (10% of salary), (3) Filter to only employees with salary > 50000. Submit your M code.
Write a recursive function that sums all numbers in a list without using List.Sum. Test it with a list of 10 numbers.
Fill‑in‑the‑Blank: 1. curly 2. square 3. item 4. Record.Combine 5. function
True or False: 1-F, 2-F, 3-F, 4-T, 5-T
Multiple Choice: 1-b, 2-a, 3-a, 4-b, 5-b, 6-a, 7-a, 8-a, 9-a, 10-a, 11-a, 12-a, 13-a, 14-a, 15-a
In Module Seven, we will dive into “Performance and Efficiency in Power Query”. You will learn how to make your queries run faster, reduce memory usage, and handle big data like a pro. Get ready to optimize your data magic!
Note: This is Module Six. Module Five covered basic M. We now move to Module Seven, where we will focus on making our queries efficient and fast. Keep coding – you are becoming a true M master!