MSBA-260 · Lesson 4 of 12 · 19 min
Cleaning and transforming columns
▶ Video coming soon A video version of this lesson is on the way.
With the profiling findings in hand, Kwabena spent an afternoon cleaning the enrolment and student tables. He worked with Maribeth on a call so that each decision about the data had an owner. "I can make the data look tidy," he told her, "but you have to tell me what correct means." This lesson covers the column-level tools Power Query offers: choosing data types, fixing text, splitting and combining columns, adding columns from examples, conditions and formulas, and filtering out rows that do not belong.
These skills map to several PL-300 objectives: *select appropriate column data types*, *create and transform columns* and *resolve inconsistencies, unexpected or null values, and data quality issues*. Most of the tools live on two ribbon tabs. The Transform tab changes existing columns in place. The Add Column tab creates new columns and leaves the original unchanged. Many commands appear on both, and choosing between them is a real decision: transforming in place loses the original, while adding a column keeps it for checking.
Data types first
Power Query assigns types in an automatic Changed Type step when it detects them, usually from the first 200 rows. Detection is a guess. A column of postal codes can be detected as numbers and lose leading zeros; a column of dates written day first can be read month first. Kwabena's rule was to review every Changed Type step and correct it rather than trust it. Power BI Desktop can turn automatic detection off for unstructured sources under the file's options for Type detection, but the essential habit is the same either way: set types deliberately.
| Type | Icon | Use for | Watch out for |
|---|---|---|---|
| Whole number | 123 | Counts, IDs that are truly numeric | Codes with leading zeros should stay text |
| Decimal number | 1.2 | Measurements, rates | Floating-point rounding in sums |
| Fixed decimal number | Currency icon | Money: four fixed decimal places | Rounds beyond four decimals |
| Date, Date/Time, Date/Time/Timezone | Calendar | Days and timestamps | Locale decides day-month order |
| Duration | Stopwatch | Lengths of time | Different from a time of day |
| Text | ABC | Names, codes, descriptions | Numbers stored as text will not sum |
| True/False | Check mark | Flags | Source may use Y/N or 1/0 |
The Fixed decimal number type, shown as Currency in some menus, stores exactly four decimal places, which avoids the tiny rounding errors of floating-point decimals when money is summed. Kwabena used it for every sales amount.
Locale: when the same text means different dates
One campus exported dates as 03/10/2026 meaning 3 October; the finance system meant 10 March. When Power Query converts text to a date, it uses the file's locale, which comes from the computer's regional settings unless set otherwise. To convert a column written in a specific convention, right-click the header and choose Change Type, then Using Locale, and pick the data type and the locale the text was written in, such as English (United Kingdom) for day-first dates or English (United States) for month-first. The same option matters for decimal separators: in many European locales 1,250 means one and a quarter, not one thousand two hundred and fifty.
Lumenvale's Kelowna campus machines were set to English (Canada), which uses day-month-year in some places and year-month-day in others, so Kwabena set the locale explicitly on every text-to-date conversion instead of relying on the regional setting.
Fixing text
Text columns carry most of the mess. The Format menu on the Transform tab offers Trim (remove spaces at the start and end), Clean (remove non-printing characters such as line breaks), and case changes: lowercase, UPPERCASE and Capitalize Each Word. Add prefixes or suffixes from the same menu. Replace values swaps one value for another, either matching the entire cell content or any part of it, and can also replace null with a value. Kwabena applied Trim and Clean to every text key column as a matter of course: an invisible trailing space is enough to make two program codes fail to match in a merge.
#"Trimmed Text" = Table.TransformColumns(Source, {{"ProgramName", Text.Trim, type text}, {"Campus", Text.Trim, type text}}),
#"Cleaned Text" = Table.TransformColumns(#"Trimmed Text", {{"ProgramName", Text.Clean, type text}}),
#"Replaced Value" = Table.ReplaceValue(#"Cleaned Text", "Business Admin", "Business Administration", Replacer.ReplaceValue, {"ProgramName"})The difference between the two replacers is a common trap. With Replacer.ReplaceText, replacing "Admin" with "Administration" would also turn "Administration" into "Administrationistration". Matching the entire cell content avoids that.
Splitting and merging columns
Split column divides one column into several by a delimiter, by a number of characters, by positions, or by transitions such as lowercase to uppercase or digit to non-digit. Splitting can create new columns or new rows: splitting "Math;English;Biology" by semicolon into rows gives three rows, which is the right shape when a cell lists several values. Merge columns does the reverse, joining selected columns with a separator into one text column.
The student system stored names as "Lastname, Firstname". Kwabena split by the left-most comma into two columns, trimmed both, and then merged them back as "Firstname Lastname" for display, keeping the originals for sorting.
Column from examples
Column from examples, on the Add Column tab, lets you type the result you want for a few rows and lets Power Query infer the transformation. Kwabena typed "DTN" next to a section code "DTN-NURS-1101-A", then "LKS" next to "LKS-BUSN-2200-C", and Power Query proposed extracting the text before the first hyphen. It shows the M it generated, so you can check that it found the rule you meant rather than one that only happens to fit the examples. Two or three well-chosen examples, including an unusual one, usually pin the rule down.
Conditional and custom columns
A conditional column builds if-then-else logic through a dialog: if Credits is greater than or equal to 12, then "Full time", else if Credits is greater than 0, then "Part time", else "Audit or withdrawn". It writes the M for you. A custom column lets you write any M expression. Custom columns are more flexible, for example combining several columns or calling functions, but they are written by hand and are harder for colleagues to maintain.
#"Added Load Status" = Table.AddColumn(#"Replaced Value", "Load Status",
each if [Credits] = null then "Unknown"
else if [Credits] >= 12 then "Full time"
else if [Credits] > 0 then "Part time"
else "Audit or withdrawn", type text)The condition checks for null first. Comparing null with a number in M returns null rather than true or false, and an if test on null raises an error. Handling nulls explicitly is one of the most common fixes in real queries, and lesson 10 covers it in depth.
Filling and filtering
Fill down and fill up copy a value into the empty cells below or above it. They fix a common spreadsheet layout in which a category appears once and the rows beneath it belong to it. The campus services timetable listed each campus name once, with blank cells below for its outlets; Fill down put the campus on every row.
Filtering removes rows. The drop-down arrow on any column header offers value lists, text filters (begins with, contains), number filters and date filters (such as in the previous year). The Home tab offers row-level tools: Keep rows (top, bottom, a range, duplicates, errors) and Remove rows (top, bottom, alternate, blank rows, duplicates, errors). Remove duplicates keeps the first occurrence of each combination of the selected columns, so selecting the right columns matters: removing duplicates on StudentID alone keeps one row per student, while selecting StudentID and TermCode keeps one per student per term.
Worked problem
Deciding what to remove
The enrolment table has 96,400 rows. Profiling showed 310 rows with ProgramCode TEST, 1,215 rows with negative credits from withdrawal reversals, and 4 duplicate student records. Maribeth says test rows should be removed, reversals kept for the withdrawals report but excluded from active counts, and duplicates resolved in the source. What should Kwabena do in Power Query?
Show solution
- Filter out ProgramCode TEST: 96,400 - 310 = 96,090 rows remain.
- Do not delete the reversals; instead add a conditional column, Record Type, marking them as Reversal so measures can exclude them.
- Leave duplicates in the enrolment table, which is many rows per student anyway; handle them in the student dimension until the registrar fixes the source.
- Add a comment to each step (right-click, Properties, Description) explaining the decision.
Answer: Remove only the test rows, flag the reversals, and document both decisions. 96,090 rows load, and the withdrawals report still has the data it needs.
Renaming, reordering and removing columns
The final cleaning steps are often the simplest: rename columns to business-friendly names, reorder them sensibly and remove columns the model does not need. Choose columns or Remove other columns keeps only what you select, which is safer than removing named columns: if the source later adds a new column, Remove other columns ignores it, while a list of removed columns would let it through. Every column left in an imported table costs memory, so removing columns is also a performance step.
Step descriptions help later readers. In the Applied steps list, right-click a step and choose Properties to rename it and add a description; renamed steps such as "Filter out test programs" make the query read like a recipe. Kwabena renamed every step that was not obvious.
Text tools beyond the basics
The Extract menu, on both the Transform and Add Column tabs, pulls part of a text value: the first or last characters, a range, the text before, after or between delimiters, or the length. Kwabena used Text Before Delimiter with a hyphen to take the campus prefix from section codes, and Length to find student IDs that had the wrong number of characters, a quick way to spot typing mistakes. The Statistics, Standard and Scientific menus on the Transform tab apply arithmetic to numeric columns, such as multiplying a fee by a rate or rounding to two decimals, without writing a custom column.
Dates have their own menus. Selecting a date column and choosing Date on the Add Column tab offers the year, quarter, month, month name, week of year, day of week, start or end of month and age, among others. These are useful for quick checks, but Rupert discouraged adding many date parts to every fact table. A shared date table, with one row per day and all the date attributes, related to the fact tables, is the better design, and the modelling course builds one.
Index columns
Index column, on the Add Column tab, numbers the rows from 0, from 1 or from a custom start and increment. It has three common uses: creating a surrogate key for a dimension (lesson 5), keeping the original row order before sorting, and enabling patterns that compare a row with the one before it. An index column depends on row order at the time it is added, and it usually stops query folding, so it belongs late in a query.
Cleaning in Power Query or at the source
Every cleaning step answers a question: should this be fixed here, or at the source? Kwabena used a simple test with Maribeth. If the problem is about how the data is entered, such as misspelled program names, duplicate students or test records left in production, the source owner should fix it, and Power Query only works around it until they do. If the problem is about how the data is used for analysis, such as combining first and last names for display, grouping credit loads into bands, or converting codes to readable labels, Power Query is the right place. The first kind of fix should be temporary and documented; the second kind is part of the design.
Exam patterns for cleaning questions
Cleaning questions usually describe a column and its problem and ask which transformation fixes it. Leading or trailing spaces point to Trim. Non-printing characters such as line breaks point to Clean. Codes that lose leading zeros point to the Text type. Dates read in the wrong order point to Change Type Using Locale. A category that appears once with blank rows beneath point to Fill down. A cell holding several values separated by a delimiter, when the model needs one value per row, points to Split column into rows. Reading the symptom carefully usually narrows the answer to one tool.
A cleaned enrolment query, read as a recipe
By the end of the afternoon, Kwabena's enrolment query read like a recipe that anyone at Lumenvale could follow: connect to the database; keep the needed columns; set types; trim and clean the text keys; filter out test programs; add the load status column; rename columns to business names. Each step had a short, descriptive name. When Maribeth asked a month later why audit-only registrations were not counted as full time, he could point to the step and its description instead of searching his memory.
Common mistakes
MistakeAccepting the automatic Changed Type step without checking.
InsteadReview each type; keep codes with leading zeros as text and set the locale for dates.
MistakeUsing Replace values on part of the text when you meant whole cells.
InsteadMatch entire cell contents so replacements do not alter longer values.
MistakeWriting if tests that ignore null.
InsteadTest for null first; comparisons with null do not return true or false.
MistakeRemoving named columns one by one.
InsteadUse Choose columns or Remove other columns so new source columns do not slip in.
Summary and key terms
- Set data types deliberately; use Change Type Using Locale for dates and numbers written in a specific convention.
- Trim and Clean text keys before merging; replace whole cells unless you mean partial text.
- Split column can create columns or rows; Column from examples infers a rule from typed examples.
- Conditional columns build if-then-else logic; custom columns accept any M expression.
- Filter rows, fill down, remove duplicates on the right columns, and keep only needed columns.
- Changed Type
- The step that sets column data types, often added automatically.
- Locale
- The regional convention used to read dates and numbers from text.
- Fixed decimal number
- A type with four fixed decimal places, suited to money.
- Column from examples
- A feature that creates a column by inferring a rule from sample results.
- Conditional column
- A column built from if-then-else rules in a dialog.
- Fill down
- Copying a value into the empty cells below it.
Practice problems
Work each problem on paper first, then reveal the answer to check.
How do you convert "03/10/2026" to 3 October when the computer reads month first?
Show answer
Change Type Using Locale, choosing Date and a day-first locale such as English (United Kingdom).
Why trim text keys before a merge?
Show answer
Invisible spaces make otherwise identical keys fail to match.
Which removes rows that repeat StudentID and TermCode together?
Show answer
Select both columns, then Remove duplicates.
Lesson quiz · 5 questions
Score 60% or more to complete this lesson.
Create a free account to save your progress, take quizzes and earn a certificate.