Data Quality and Cleansing

Earn 25 points (50 with Pro) in two steps

  1. ① Read through the lesson — each section gets a ✓ as you scroll through it.
  2. ② When every section has a ✓, tap Complete lesson.

0 of 9 read · keep scrolling

✦ See fewer ads and earn double points — 50 a lesson instead of 25 — with Pro

Lesson: Data Quality and Cleansing in Power BI

Introduction: Why Data Quality Matters

In the world of data analytics, the phrase "garbage in, garbage out" is more than just a cliché; it is the fundamental reality of business intelligence. You can build the most beautiful, interactive, and complex dashboards in Power BI, but if the underlying data is flawed, inaccurate, or inconsistent, your reports will lead to incorrect business decisions. Data cleansing is the process of identifying, correcting, or removing corrupt, inaccurate, or irrelevant records from a dataset.

Data transformation in Power BI is primarily handled through the Power Query Editor. This tool allows you to perform a wide range of operations to reshape and clean your data before it ever reaches your data model. When you clean your data at the source—or as close to the source as possible—you ensure that your measures, calculated columns, and relationships are built upon a reliable foundation. This lesson explores the critical components of data quality, the mechanics of cleaning data within Power Query, and the best practices to maintain a healthy data pipeline.

Not read yet

The Pillars of Data Quality

Before diving into the technical steps, it is helpful to categorize what we mean by "quality." High-quality data generally adheres to several core principles. If your data fails in any of these areas, your analysis is at risk:

  • Accuracy: Does the data reflect the reality of the business? For example, are sales figures recorded in the correct currency, and are dates associated with the correct fiscal periods?
  • Completeness: Are there missing values where there should be data? Missing entries in critical fields like "Customer ID" or "Product Category" can cause your visuals to misrepresent the total performance of those entities.
  • Consistency: Is the data formatted in a standard way across different tables? If one table lists a country as "USA" and another as "United States," your relationships will fail to link these records properly.
  • Timeliness: Is the data current enough for the needs of the report? Stale data provides a false sense of security, especially in fast-moving industries like retail or logistics.
  • Validity: Does the data follow the expected structure? For instance, an email address field should contain an "@" symbol, and a quantity field should not contain text characters.

Callout: Data Cleansing vs. Data Transformation While these terms are often used interchangeably, they represent different stages of the pipeline. Data cleansing is the act of fixing errors, filling gaps, and removing duplicates to ensure the data is accurate and reliable. Data transformation is the broader process of reshaping data to make it useful for analysis, such as pivoting columns, merging tables, or calculating new metrics. Cleansing is the prerequisite for effective transformation.

Not read yet

Getting Started with Power Query Editor

To access the data cleansing tools, you must open the Power Query Editor. You can do this by selecting "Transform Data" from the Home ribbon in Power BI Desktop. Once inside, you will see a ribbon filled with tools designed to manipulate your data. The most important thing to remember about Power Query is that it is a non-destructive process. Every step you take is recorded in the "Applied Steps" pane on the right side of the screen. If you make a mistake, you can simply delete the step to revert your data to its previous state.

Handling Missing Values

Missing values, often represented as null in Power Query, are one of the most common issues in raw datasets. Depending on the context, you have three primary ways to handle them:

  1. Remove: If a row is missing critical information that cannot be inferred, removing the row is often the safest option.
  2. Replace: If you can reasonably guess the value (like replacing a null "Country" with "Unknown"), you can use the "Replace Values" feature.
  3. Fill: In scenarios like time-series data, you might want to carry the previous value forward (Fill Down) or take the next value backward (Fill Up).

Tip: The "Replace Values" Utility When using the Replace Values feature, be very specific. If you are replacing "null" in a text column, ensure you are not accidentally replacing empty strings that were meant to remain blank. Always check the "Applied Steps" to ensure the replacement didn't have unintended consequences on your dataset.

Removing Duplicates and Inconsistencies

Duplicate rows can inflate your metrics, leading to over-counted sales or inaccurate inventory levels. Power Query makes it easy to handle these. To remove duplicates, select the columns that define a unique row (such as "Transaction ID" or a combination of "Date" and "Customer ID"), right-click the header, and select "Remove Duplicates."

However, be careful with "inconsistencies" that are not true duplicates. For example, "Apple Inc." and "Apple" might represent the same entity but appear as different rows. You can fix this by using the "Replace Values" tool to standardize naming conventions across your dataset.

Not read yet

Advanced Data Cleansing Techniques

Once you have mastered the basics, you can move toward more robust, automated cleansing processes. This is where you transition from manual edits to repeatable, high-performance data pipelines.

Standardizing Data Types

One of the most frequent causes of errors in Power BI is incorrect data typing. If a column containing numbers is imported as "Text," you will be unable to perform mathematical operations like sums or averages. Conversely, if a date column is imported as "Text," you cannot use it in time-intelligence functions like SAMEPERIODLASTYEAR.

Always check the data type icon to the left of the column header. You can change it by clicking the icon and selecting the appropriate type (e.g., Decimal Number, Whole Number, Date, or Text).

Splitting and Merging Columns

Raw data often arrives in a "dirty" state where multiple pieces of information are packed into a single column. For instance, a "Full Name" column might contain both first and last names. To clean this, use the "Split Column" feature:

  1. Select the column you want to split.
  2. Go to the "Transform" tab and select "Split Column."
  3. Choose your delimiter (e.g., space, comma, or custom character).
  4. Power Query will automatically create two new columns, which you should then rename for clarity.

Conversely, you might need to merge columns, such as combining "City" and "State" into a single "Location" field for a map visual. You can do this by selecting both columns, right-clicking, and choosing "Merge Columns."

Warning: The Impact of Data Cleaning on Performance While cleaning data is vital, performing too many complex transformations in Power Query can slow down your report refresh times. Try to perform heavy data cleaning at the source (e.g., in your SQL database) whenever possible. If you must do it in Power Query, keep your steps efficient and avoid unnecessary column additions.

Not read yet

Practical Example: Cleaning a Sales Export

Let’s walk through a scenario where you receive a CSV file from a legacy system. The file has the following issues:

  • The "Date" column is in a non-standard format (e.g., "20230101").
  • The "Price" column contains currency symbols (e.g., "$100.00").
  • There are rows where the "Quantity" is null.

Step-by-Step Cleansing Process:

  1. Fix the Date: Select the "Date" column. Go to "Add Column" -> "Column from Examples." Type in the correct format (e.g., "01/01/2023"). Power Query will often detect the pattern and suggest a transformation. Alternatively, you can use "Transform" -> "Parse" if the format is recognized.
  2. Fix the Price: Select the "Price" column. Right-click and choose "Replace Values." Replace "$" with nothing. Once the column contains only numbers, change the data type to "Fixed Decimal Number." This ensures that calculations are accurate and prevents floating-point errors.
  3. Fix the Quantity: Select the "Quantity" column. Click the filter arrow in the column header and uncheck "null." This will remove any rows where the quantity is missing.
  4. Review Applied Steps: Look at the right-hand panel. You will see "Replaced Value," "Changed Type," and "Filtered Rows." Ensure they are in a logical order.

Not read yet

Using M Code for Custom Cleansing

Power Query is powered by a language called "M." While the interface provides buttons for most tasks, sometimes you need more control. You can view the M code by going to the "View" tab and selecting "Advanced Editor."

Here is an example of a simple M function to clean leading and trailing whitespace from a column:

let
    Source = Table.FromRecords({[Name = " John "], [Name = "Jane "]}),
    CleanedData = Table.TransformColumns(Source, {{"Name", Text.Trim, type text}})
in
    CleanedData

In this snippet, Table.TransformColumns is used to apply the Text.Trim function to every entry in the "Name" column. This is much faster than manually cleaning strings and ensures that "John" and " John" are treated as the same value.

Best Practices for Data Quality

To keep your Power BI reports maintainable and accurate, follow these industry-standard practices:

  • Document Your Steps: Use the "Properties" feature in the "Applied Steps" pane to rename complex steps. Instead of "Filtered Rows1," rename it to "Remove Inactive Customers." This makes it easier for others to understand your logic.
  • Use Parameters: If you have file paths or specific threshold values that change, use Parameters. This allows you to update the configuration without editing the M code directly.
  • Keep Data Types Consistent: Always define your data types as early as possible in the query process. This helps Power Query optimize the data loading process.
  • Filter Early: Filter your data as close to the source as possible. If you only need data from the last two years, add a date filter at the very first step. This reduces the amount of data processed and speeds up your report.
  • Avoid "Change Type" Bloat: Power Query often adds an automatic "Changed Type" step after every operation. While this is helpful, it can lead to a long list of steps. Periodically group your transformations or manually clean up the M code if the list becomes unmanageable.

Callout: The "Query Folding" Concept Query Folding is a feature where Power Query translates your steps into a single query that is sent back to the data source (like a SQL database). This is highly efficient. When you perform complex transformations, some steps may prevent Query Folding. Always check if your steps are folded by right-clicking an "Applied Step" and checking if "View Native Query" is enabled. If it is grayed out, your query is no longer folding, and performance may suffer.

Not read yet

Common Pitfalls to Avoid

Even experienced analysts fall into traps when cleaning data. Here are the most frequent mistakes:

  1. Ignoring Data Granularity: If you aggregate data too early in the cleansing process, you lose the ability to drill down into the details later. Always keep your data as granular as possible until the final stages of report building.
  2. Over-Cleaning: Do not delete columns just because they look messy. You might find that a column you thought was useless is actually the key to a future relationship or a secondary calculation.
  3. Hardcoding Values: Avoid hardcoding values like "2023" or "Region_North" directly into your logic. If the business changes, your report will break. Use parameters or lookup tables instead.
  4. Neglecting Error Rows: Sometimes, a data conversion will fail (e.g., trying to turn "ABC" into a number). Power Query will show these as "Error" cells. Always right-click on the "Error" cell or the column header to "Remove Errors" or "Replace Errors" with a default value. Ignoring errors can cause your entire report refresh to fail.

Not read yet

Comparison: Manual vs. Automated Cleansing

Feature Manual Cleansing Automated (M Code/Parameters)
Scalability Low; requires manual intervention for every new file. High; works for any dataset of the same structure.
Consistency Risk of human error (typos, missed rows). Consistent; logic is applied exactly the same every time.
Maintainability Hard to audit or share with a team. Easy to document and version control.
Speed Slow for large datasets. Fast; optimized by the Power Query engine.

FAQ: Frequently Asked Questions

Q: What happens if I change the source file format? A: If the column names or data structure change, your Power Query steps will likely break. You will need to open the Advanced Editor and adjust the M code to reflect the new structure.

Q: Should I clean data in Excel before importing it into Power BI? A: It is generally better to clean data in Power BI. By using Power Query, you create a repeatable, auditable process. If you clean it in Excel, you are creating a manual, prone-to-error "middle step" that is difficult to update.

Q: How do I handle date formats that vary by region? A: Power Query has a "Using Locale" feature when changing data types. If your data is in UK format (DD/MM/YYYY) but your machine is set to US (MM/DD/YYYY), use the "Change Type with Locale" option to ensure the dates are interpreted correctly.

Q: Is it better to have many small queries or one large query? A: It is better to have many small, modular queries that you merge or append together. This makes troubleshooting much easier. If a specific transformation fails, you know exactly which query is the culprit.

Not read yet

Final Key Takeaways

  1. Foundation is Everything: Data cleansing is the most important step in the analytics lifecycle. A well-designed report is worthless if the data is inaccurate.
  2. Non-Destructive Workflows: Power Query’s "Applied Steps" feature provides a safety net. Never fear experimenting, as you can always undo your changes.
  3. Standardization is Key: Ensure data types are set correctly early in the process to avoid calculation errors and ensure that time-intelligence functions work as expected.
  4. Performance Matters: Keep an eye on "Query Folding" to ensure your transformations are being processed efficiently at the source rather than locally in Power BI.
  5. Automate for Repeatability: Move away from manual, one-off fixes. Use parameters and M code to create robust transformations that handle future data updates automatically.
  6. Handle Errors Proactively: Don't let your data load fail because of a few bad cells. Use the error-handling tools in Power Query to catch, filter, or replace problematic data.
  7. Document Your Logic: Use the built-in naming features for steps and queries. Future you—and your colleagues—will appreciate the clarity when it comes time to update the report.

By mastering these data cleansing techniques, you move from being a user of Power BI to an analyst who can reliably extract truth from raw, messy data. Remember that data quality is not a one-time task; it is a continuous commitment to maintaining the integrity of your information ecosystem. As you gain more experience, you will find that the time invested in cleaning your data at the start pays for itself tenfold in the time saved during report development and maintenance.

Not read yet

Each section gets a ✓ as you scroll through it. Tap the button to jump to the next one.