When working with data in Power BI, the Power Query Editor is where you clean and prepare your data before building reports.
Think of it like preparing ingredients before cooking
If the data is messy, your analysis will be wrong.
1️⃣ What is Power Query Editor?
Power Query Editor is the data transformation tool inside Power BI.
To open it:
Here you can:
2️⃣ Renaming Columns
🔹 Why Renaming is Important
Raw data often comes like this:
| CustNm | OrdDt | Amt | |--------|----------|------| | John | 1/2/2023 | 200 | | Jane | 1/3/2023 | 350 |
These names are unclear.
After renaming:
| Customer Name | Order Date | Amount | |--------------|------------|--------| | John | 1/2/2023 | 200 | | Jane | 1/3/2023 | 350 |
Now the data is:
🔹 How to Rename Columns (Beginner Method)
Power BI automatically creates M code in the background.
🔹 M Code for Renaming Columns
= Table.RenameColumns(Source, {
{"CustNm", "Customer Name"},
{"OrdDt", "Order Date"},
{"Amt", "Amount"}
})
Explanation:
"CustNm" is the old name"Customer Name" is the new name🔹 Best Practices for Naming Columns
✅ Use clear names
✅ Avoid special characters (#, %, @)
✅ Keep names consistent
✅ Avoid very short unclear abbreviations
3️⃣ Reordering Columns
🔹 Why Reorder Columns?
Sometimes data loads like this:
| Amount | Order Date | Customer Name | OrderID | |--------|------------|--------------|--------|
But logically, it should be:
| OrderID | Customer Name | Order Date | Amount | |--------|--------------|------------|--------|
Reordering:
🔹 How to Reorder (Beginner Method)
Method 1:
Method 2:
🔹 M Code for Reordering
= Table.ReorderColumns(Source,
{"OrderID", "Customer Name", "Order Date", "Amount"}
)
Important:
You must list ALL columns in the exact order you want.
4️⃣ Finding Anomalies (Very Important in Data Analysis)
🔍 What is an Anomaly?
An anomaly is:
Example:
| OrderID | Amount | |--------|--------| | 1001 | 200 | | 1002 | 250 | | 1003 | 99999 | | 1004 | -50 |
Possible problems:
5️⃣ How to Find Anomalies in Power Query Editor
✅ Method 1: Sort the Column
Click Amount → Sort Descending
This helps you quickly see:
✅ Method 2: Use Column Statistics
Go to:
View → Column Distribution
You will see:
If max value is extremely high → investigate.
✅ Method 3: Filter Specific Values
Find Negative Values
= Table.SelectRows(Source, each [Amount] < 0)
Find Extremely Large Values
= Table.SelectRows(Source, each [Amount] > 10000)
✅ Method 4: Find Null (Missing) Values
Example data:
| OrderID | Amount | |--------|--------| | 1001 | 200 | | 1002 | | | 1003 | 350 |
M Code:
= Table.SelectRows(Source, each [Amount] is null)
✅ Method 5: Detect Errors
If a number column contains text:
| Amount | |--------| | 200 | | three hundred | | 400 |
Power BI shows an error icon.
To filter rows with errors:
= Table.SelectRowsWithErrors(Source, {"Amount"})
6️⃣ Complete Practical Example
Raw Data:
| OrderID | CustNm | OrdDt | Amt | |--------|--------|----------|-------| | 1001 | john | 1/2/23 | 200 | | 1002 | jane | 1/3/23 | -100 | | 1003 | JOHN | 1/4/23 | 50000 | | 1004 | alice | | 300 |
Cleaning Steps:
Full M Code Example
let
Source = Excel.CurrentWorkbook(){[Name="Sales"]}[Content],
// Rename columns
RenamedColumns = Table.RenameColumns(Source,{
{"CustNm","Customer Name"},
{"OrdDt","Order Date"},
{"Amt","Amount"}
}),
// Change data types
ChangedType = Table.TransformColumnTypes(RenamedColumns,{
{"Order Date", type date},
{"Amount", type number}
}),
// Reorder columns
ReorderedColumns = Table.ReorderColumns(ChangedType,{
"OrderID","Customer Name","Order Date","Amount"
}),
// Find negative values (anomalies)
NegativeValues = Table.SelectRows(ReorderedColumns,
each [Amount] < 0
),
// Find extremely large values
LargeValues = Table.SelectRows(ReorderedColumns,
each [Amount] > 10000
)
in
ReorderedColumns
7️⃣ Cleaned Result Example
| OrderID | Customer Name | Order Date | Amount | |--------|--------------|------------|--------| | 1001 | John | 2023-01-02 | 200 | | 1004 | Alice | 2023-01-05 | 300 |
8️⃣ Summary Table
| Task | Purpose | Why Important | |------|---------|--------------| | Rename Columns | Improve clarity | Easier understanding | | Reorder Columns | Organize structure | Better readability | | Sort Values | Identify extremes | Detect anomalies | | Filter Values | Isolate unusual data | Prevent wrong analysis | | Check Errors | Detect wrong data types | Improve data quality |
COMPILATION OF ALL CODE BLOCKS (Single Combined Script)
let
Source = Excel.CurrentWorkbook(){[Name="Sales"]}[Content],
// Rename columns
RenamedColumns = Table.RenameColumns(Source,{
{"CustNm","Customer Name"},
{"OrdDt","Order Date"},
{"Amt","Amount"}
}),
// Change data types
ChangedType = Table.TransformColumnTypes(RenamedColumns,{
{"Order Date", type date},
{"Amount", type number}
}),
// Reorder columns
ReorderedColumns = Table.ReorderColumns(ChangedType,{
"OrderID","Customer Name","Order Date","Amount"
}),
// Detect negative anomalies
NegativeValues = Table.SelectRows(ReorderedColumns,
each [Amount] < 0
),
// Detect extremely large anomalies
LargeValues = Table.SelectRows(ReorderedColumns,
each [Amount] > 10000
),
// Detect null values
NullValues = Table.SelectRows(ReorderedColumns,
each [Amount] is null
),
// Detect errors
ErrorRows = Table.SelectRowsWithErrors(ReorderedColumns, {"Amount"})
in
ReorderedColumns