Courses Learn Blog Contact
Get Started →
Lesson 4

Day 3: Data Transformation in Power Query

Custom and conditional columns, dates broken into parts, Split Column, Group By and Unpivot.

Webbo3 Power BI Cohort 1 13 min read Free

Webbo3 Power BI · Cohort 1 · Day 3 of 16

Day 3: Data Transformation in Power Query

Cleaning made the data right. Transforming makes it useful: new columns, new shapes, and totals you can check.

By the end of today you can

  • Add a column that multiplies two others, and use it to check the file's own totals
  • Add a conditional column that labels each sale before or after the fuel subsidy removal
  • Pull the year and month out of a date
  • Extract part of a text, and split one column into two
  • Summarise a table with Group By
  • Turn a long table wide with Pivot, and back again with Unpivot

Cleaning answers "is this data right?" Transforming answers "is it in the shape I need to answer Alhaji Musa Bello?"

Today you work on your clean Sales_2023 query from Day 2 (3,116 rows). Every transformation is still an Applied Step, so it still replays on refresh, and every one is made with a button.

1. Columns From Other Columns

Definition

The Add Column tab builds a new column from the ones you already have, row by row, without changing the originals. Standard does arithmetic on the columns you select: add, subtract, multiply, divide.

Real-Life Example

An auditor checking receipts. She does not trust the total printed at the bottom; she multiplies quantity by price herself and compares. If they agree, she trusts the receipt.

Types: Where New Columns Come From

ButtonMakesToday
Standard (Multiply, Subtract...)Arithmetic across the selected columnsA check on TotalAmount_NGN
Conditional ColumnA label chosen by rules, without typing a formulaBefore or after the subsidy
DateYear, month, day and more from a dateYear, Month, Month Name
ExtractPart of a text, into a new columnA short station name

Checking the File's Own Arithmetic

Click path

1.Select Quantity › Ctrl+click UnitPrice_NGN › Add Column › Standard › Multiply
2.Select the new Multiplication column › Transform › Rounding › Round... › Decimal places: 2
3.Select Multiplication › Ctrl+click TotalAmount_NGN › Add Column › Standard › Subtract
4.Rename the result Check
What we are checkingWe want to multiply quantity by unit price ourselves and subtract the total the station typed, to see whether the file's own totals can be trusted.
Preview: Sales_2023 with the check
ABCTransactionIDDATEDateABCStationNameABCProductNameABCAttendantName1.2Quantity1.2UnitPrice_NGN1.2TotalAmount_NGNABCPaymentMethod1.2Multiplication1.2Check
1TXN-2023-00038313/02/2023Bodija Filling StationPMS (Petrol)Amina Nwosu9.7187.41,817.78Cash1,817.780
2TXN-2023-00246813/10/2023Bodija Filling StationEngine OilSegun Adeyemi23,402.876,805.74Cash6,805.740
3TXN-2023-00216110/09/2023Bodija Filling StationPMS (Petrol)Bisi Chukwu39.7612.6124,320.62POS24,320.620
4TXN-2023-00278920/11/2023GRA Filling StationPMS (Petrol)Kemi Balogun41638.4226,175.22Cash26,175.220
5TXN-2023-00110305/05/2023Wuse Filling StationPMS (Petrol)Halima Umeh36.1179.686,486.45Cash6,486.450
6TXN-2023-00114209/05/2023Independence Layout Filling StationGreaseChinwe Chukwu31,202.43,607.2POS3,607.20
7TXN-2023-00213907/09/2023Bodija Filling StationPMS (Petrol)Segun Adeyemi44.5629.9328,031.88POS28,031.880
8TXN-2023-00310127/12/2023Sabon Gari Filling StationGreaseIfeanyi Suleiman31,215.033,645.09Cash3,645.090
11 COLUMNS, 3,116 ROWS · first 8 shown

Filter the Check column for anything other than 0: 0 rows. Every total in the sheet is exactly quantity times price, so TotalAmount_NGN can be trusted as the revenue. Now delete the Multiplication and Check columns (right-click, Remove): they were for checking, and a model should only carry what the report needs.

Picture This

Picture the auditor with her calculator: trust the total only after you have done the sum.

2. Conditional Columns

Definition

A conditional column gives each row a label chosen by rules you set in a dialog: if this, then that, else something else. The rules are tested from the top, and the first one that is true wins.

Real-Life Example

The fuel subsidy was removed in June 2023, and pump prices tripled almost overnight. Alhaji Musa Bello wants every sale split into "before" and "after", so he can compare the two worlds his business has lived in.

Click path

1.Add Column › Conditional Column › New column name: Price Period
2.If: Date › Operator: is before › Value: 01/06/2023 › Output: Before subsidy removal
3.Else: After subsidy removal › OK
What this showsWe want every 2023 sale labelled by the price period it happened in, and to count each label.
Count of rows by Price Period
Price PeriodRows
Before subsidy removal1,294
After subsidy removal1,822
Total3,116

Set the new column's type to Text. Tomorrow and all through Week 2 this one column lets any visual compare before and after with a single click.

Picture This

Picture a line drawn across the calendar at 1 June 2023, and every sale landing on one side of it.

3. Working With Dates

Definition

Because Date is a real date (Day 2), Power Query can read its parts for you: the year, the month number, the month name, the quarter, the day of the week.

Real-Life Example

A calendar on the wall already knows that 13 October 2023 is a Friday in the fourth quarter. You do not work it out; you look.

Click path

1.Select Date › Add Column › Date › Year › Year
2.Select Date › Add Column › Date › Month › Month
3.Select Date › Add Column › Date › Month › Name of Month
What we are checkingWe want the year, month number and month name read off every sale's date.
Preview: new date columns
ABCTransactionIDDATEDateABCPrice Period1²3Year1²3MonthABCMonth Name
1TXN-2023-00038313/02/2023Before subsidy removal20232February
2TXN-2023-00246813/10/2023After subsidy removal202310October
3TXN-2023-00216110/09/2023After subsidy removal20239September
4TXN-2023-00278920/11/2023After subsidy removal202311November
5TXN-2023-00110305/05/2023Before subsidy removal20235May
6TXN-2023-00114209/05/2023Before subsidy removal20235May
7TXN-2023-00213907/09/2023After subsidy removal20239September
8TXN-2023-00310127/12/2023After subsidy removal202312December
6 COLUMNS, 3,116 ROWS · first 8 shown
Keep Month Number

Month Name sorts alphabetically (April, August, December...). You keep the Month number column so that on Day 7 you can tell Power BI to sort the names in calendar order.

4. Extract and Split

Definition

Extract copies part of a text into a new column and leaves the original alone. Split Column cuts one column into two or more, and the original is replaced.

Real-Life Example

"Independence Layout Filling Station" is a lot of words for a chart label when every station is a filling station. A short name, "Independence Layout", reads better on a phone.

Types

ButtonKeeps the original?Use it when
Extract, Text Before DelimiterYes, adds a new columnYou want a shorter label as well
Split Column, By DelimiterNo, replaces itOne column really holds two things

Click path

1.Select StationName › Add Column › Extract › Text Before Delimiter › Delimiter: " Filling Station" (with the space in front) › OK › Rename it Station
What we are checkingWe want a short station name for chart labels, next to the full name the model will join on.
StationName and the extracted short name
StationNameStation (extracted)
Bodija Filling StationBodija
GRA Filling StationGRA
Ikeja Filling StationIkeja
Independence Layout Filling StationIndependence Layout
Lekki Filling StationLekki
Sabon Gari Filling StationSabon Gari
Wuse Filling StationWuse

Keep StationName. On Day 5 it is the key that joins each sale to Station_Master, and the short name is only for labels.

Split: First Name and Surname

Click path

1.Attendant_Directory query › Select AttendantName › Transform › Split Column › By Delimiter › Space › At the left-most delimiter › OK
What we are checkingWe want each attendant's name split into two columns in the Attendant_Directory query.
Preview: Attendant_Directory after Split
ABCAttendantIDABCAttendantName.1ABCAttendantName.2ABCStation
1ATT001ChidiOgunleyeIkeja Filling Station
2ATT002FunkeYakubuIkeja Filling Station
3ATT003BisiEzeLekki Filling Station
4ATT004TundeOgunleyeLekki Filling Station
5ATT005ChidiDanjumaWuse Filling Station
6ATT006TundeDanjumaWuse Filling Station
7ATT007HalimaUmehWuse Filling Station
8ATT008ZainabBalogunGRA Filling Station
4 COLUMNS, 17 ROWS · first 8 shown
Undo This One

Split was for practice. Delete the step now: AttendantName must stay whole, because it is what each sale is joined on later.

Picture This

Picture 7 short names on a phone screen, each one readable at a glance.

5. Group By

Definition

Group By turns many rows into one row per group, with a total, count or average for each. It throws the detail away, so you do it on a copy.

Real-Life Example

The end-of-month report. The station manager does not read Head Office every receipt; he reads one line per product with the total.

Click path

1.Right-click Sales_2023 › Reference › Rename it Check_By_Station
2.Home › Group By › Advanced › Group by: StationName
3.New column: Revenue, Sum, TotalAmount_NGN › Add aggregation: Sales, Count Rows › OK
4.Right-click Check_By_Station › Untick Enable load
What this showsWe want one row per station with its 2023 revenue and number of sales, to check against the report later.
Check_By_Station
StationNameRevenueSales
Ikeja Filling Station₦8,573,343466
Lekki Filling Station₦8,496,794470
Bodija Filling Station₦8,311,608436
Sabon Gari Filling Station₦8,224,614462
Wuse Filling Station₦8,000,179437
GRA Filling Station₦7,814,377425
Independence Layout Filling Station₦7,635,615420
Total₦57,056,5293,116

A reference query starts from the result of Sales_2023 and adds its own steps, so the original keeps all its rows. Unticking Enable load keeps the check out of the model: it is for you, not for the report.

What this showsWe want a second Group By on a new reference: petrol only, grouped by Price Period, with litres, revenue and the months each period covers. This is Alhaji Musa Bello's price-jump question in miniature.
Petrol by Price Period, 2023
Price PeriodLitresLitres per monthAverage price per litre
Before subsidy removal21,6064,321₦185
After subsidy removal32,6954,671₦585

Filter ProductName to PMS (Petrol) first, then Group By Price Period with Sum of Quantity, Sum of TotalAmount_NGN and Count Distinct of Month. The price per litre is revenue divided by litres; you will build it properly as a measure in Week 2.

Picture This

Picture the monthly report: one line per group, the receipts filed away behind it.

6. Pivot and Unpivot

Definition

A long table has one column for the label and one for the value. A wide table spreads one kind of value across many columns, such as a column for each quarter. People like to read wide tables; Power BI needs long ones. Pivot turns long into wide; Unpivot turns wide back into long.

Real-Life Example

A school timetable is wide: days across the top, periods down the side, easy to read on a wall. But to ask "how many Maths lessons are there a week?" you would rather have a list: one line per lesson.

Build a wide table to practise on

1.Reference Sales_2023 › Select Date › Add Column › Date › Quarter › Quarter of Year
2.Select Quarter › Transform › Data Type: Text › Format › Add Prefix › Q
3.Home › Group By › Advanced › Group by: StationName, then Quarter › Revenue: Sum of TotalAmount_NGN
4.Select Quarter › Transform › Pivot Column › Values Column: Revenue › OK
What we are looking atWe want the kind of wide table a manager likes: 2023 revenue per station, one column per quarter.
After Pivot Column (wide)
ABCStationName1²3Q11²3Q21²3Q31²3Q4
1Bodija Filling Station1,463,2521,675,2002,206,1912,966,964
2GRA Filling Station1,464,2381,475,5432,375,2842,499,312
3Ikeja Filling Station1,148,7751,665,2253,165,4222,593,921
4Independence Layout Filling Station1,338,9751,453,9552,380,9252,461,760
5Lekki Filling Station1,140,0801,631,9273,471,5572,253,229
6Sabon Gari Filling Station1,538,5171,724,8362,120,3672,840,894
7Wuse Filling Station1,530,4241,160,0752,592,9632,716,717
5 COLUMNS, 7 ROWS

It reads well, but you cannot draw one line of revenue over the quarters from it, because the quarter is hiding in the column names. Unpivot moves it back into the data:

Click path

1.Select StationName › Transform › Unpivot Columns › Unpivot Other Columns
2.Rename Attribute to Quarter, Value to Revenue
What we are checkingWe want the wide table turned long again: one row per station per quarter.
After Unpivot Other Columns: 7 rows became 28
ABCStationNameABCQuarter1²3Revenue
1Bodija Filling StationQ11,463,252
2Bodija Filling StationQ21,675,200
3Bodija Filling StationQ32,206,191
4Bodija Filling StationQ42,966,964
5GRA Filling StationQ11,464,238
6GRA Filling StationQ21,475,543
7GRA Filling StationQ32,375,284
8GRA Filling StationQ42,499,312
3 COLUMNS, 28 ROWS · first 8 shown

Why Unpivot Other Columns rather than selecting Q1 to Q4? If next year's sheet adds another column, "other columns" picks it up automatically; a list of named columns would miss it.

Picture This

Picture the timetable on the wall, and the same timetable as a list you can count.

Do This Now

  1. Multiply Quantity by UnitPrice_NGN, round, subtract TotalAmount_NGN, and check the result is always 0. Then remove both helper columns.
  2. Add the Price Period conditional column.
  3. Add Year, Month and Month Name.
  4. Extract the short Station name. Split AttendantName for practice, then delete that step.
  5. Make the two reference queries and Group By, untick their Enable load, and check your totals.
  6. Build the quarterly wide table with Pivot and turn it back with Unpivot.

Checkpoint

Q1. Why check TotalAmount_NGN against quantity times price before trusting it?

Because it was typed or calculated by someone else. The check found 0 differences, so the totals can be used as revenue.

Q2. Why does the order of rules in a conditional column matter?

Rules are tested top to bottom and the first true one wins.

Q3. Why Group By on a reference and not on the query you load?

Group By throws away the detail rows. The report needs them so visuals can slice them any way.

Q4. What did the petrol table show about the price jump?

Prices per litre roughly tripled after June 2023, but litres sold per month did not fall. Customers kept buying about the same amount at a much higher price.

Tick Before You Move On

  • ☐ My check column was 0 on every row, and I removed it
  • ☐ Price Period labels every sale before or after 1 June 2023
  • ☐ Year, Month and Month Name come from the Date column
  • ☐ My Check_By_Station total is ₦57,056,529
  • ☐ I turned a table wide with Pivot and long again with Unpivot

Quick Recap

Add Column, Standard does arithmetic with clicks; use it to check a file before trusting it. Conditional columns label rows by rules tested in order. A real date gives you its year, month and name for free. Extract adds a column and keeps the original; Split replaces it. Group By summarises on a reference copy; Pivot makes wide, Unpivot makes long.

Final Scenario

Alhaji Musa Bello asks: "When the price jumped, did people just buy less fuel?"

Your petrol table answers in two rows: about ₦185 a litre before, ₦585 after, and litres per month did not drop. Customers paid the new price and kept coming.

One conditional column and one Group By, and nothing typed by hand.

Next, Day 4 Lab: the whole Sales_2023 cleaning built together as a class, with the mistakes everyone makes and how to find them. Assignment 1 is released at the end.

Learn this with a real instructor

Free lessons take you a long way on your own. The full programmes add live classes, assignments, Skill Points and a certificate — built for African students.