Power BI Table Query: How to Filter and Transform Data
In this article
To create a Power BI table query, you use the Power Query Editor to connect to a data source, filter rows, remove unnecessary columns, and transform the data types before loading the result into your report. This process ensures that your final table is lean, accurate, and optimized for performance by removing "noise" from the raw dataset at the source level rather than relying on visual-level filters.
What You'll Need
- Power BI Desktop installed on your Windows PC.
- Access to a data source (such as an Excel workbook, CSV file, SQL Server database, or web URL).
- The necessary permissions or credentials to access the data source.
- A basic understanding of the columns and rows within your dataset to determine what needs to be filtered or transformed.
Step-by-Step
- Connect to Your Data Source
Open Power BI Desktop. On the Home tab, click the Get Data button. A dropdown menu will appear listing common sources like Excel, SQL Server, and Text/CSV. Select the source that matches your data. If you chose Excel, browse your local folders, select the file, and click Open. In the Navigator window that appears, check the box next to the specific sheet or table you want to query. Instead of clicking Load, click Transform Data. This opens the Power Query Editor, which is where the actual querying happens. Note that menu labels may vary slightly depending on your version of Power BI Desktop.
- Profile Your Data
Before applying filters, it is important to see if your data contains errors or null values. Go to the View tab and check the boxes for Column quality, Column distribution, and Column profile. This adds a small graphic under each column header. Column quality shows the percentage of data that is Valid, Error, or Empty. If you see a high percentage of Empty values in a critical column, you will know you need to filter those out in the next step to avoid skewed calculations in your report.
- Filter Rows to Narrow the Dataset
To query specific data (for example, only sales from the year 2023), locate the column you wish to filter. Click the down arrow (filter icon) in the column header. You can use basic checkboxes to select specific values, or for more complex queries, select Date Filters, Text Filters, or Number Filters. For instance, choosing Number Filters > Greater Than allows you to exclude all transactions below a certain dollar amount. Once you click OK, Power BI removes those rows from the preview. This is a "query" step that happens during the data load, meaning the excluded data never enters the Power BI data model, which saves memory.
- Remove Unnecessary Columns
Large tables with dozens of columns slow down report performance. To remove columns you don't need, hold the Ctrl key and click the headers of the columns you want to keep. Right-click on one of the selected headers and choose Remove Other Columns. This is generally safer than selecting columns to remove individually, as it ensures that if new, unwanted columns are added to the source file in the future, they will not be automatically imported into your model.
- Transform Data Types
Power BI usually guesses the data type, but it is not always correct. Look at the icon to the left of each column header (e.g., 1.2 for decimal, ABC for text, or a calendar icon for date). If a date column is listed as text, you cannot perform time-intelligence calculations. Click the data type icon and select the correct format from the list. If you encounter errors during this conversion, it usually means there is a non-numeric character in a numeric column; you may need to use the Replace Values feature on the Transform tab to clean the data first.
- Apply Custom Transformations
If you need a column that doesn't exist in the source, use the Add Column tab. For a simple calculation, select Custom Column. Give the column a name and enter a formula (e.g.,
[Quantity] * [Unit Price]). For conditional logic, select Conditional Column. This allows you to create "If/Then/Else" rules, such as: "If [Region] equals 'North', then 'Northern Territory', else 'Other'." This transforms your raw data into categorized information that is easier to analyze in a table visual. - Load the Query to the Model
Once the data looks exactly how you want it in the preview window, go to the Home tab and click Close & Apply. Power BI will now run all the steps you recorded in the "Applied Steps" pane on the right side of the screen against the entire dataset. The resulting cleaned table will then appear in the Data pane on the right side of the main report canvas, ready to be dragged into a Table or Matrix visual.
Common Mistakes
One of the most frequent errors is clicking "Load" instead of "Transform Data" during the initial connection. Loading raw data directly into the model makes it much harder to clean later and often leads to memory crashes with large datasets. Another common mistake is relying on visual-level filters (the Filter pane on the report canvas) for everything. While visual filters are useful for the end-user, they do not reduce the amount of data Power BI has to store in memory. For permanent exclusions, always use the Power Query Editor.
Users also frequently forget to check for trailing spaces in text columns. A value that looks like "New York" might actually be "New York " (with a space at the end), which causes filters to fail or creates duplicate entries in a table. To fix this, right-click the column header in Power Query, select Transform, and then Trim.
Tips
To keep your queries organized, rename your steps in the Applied Steps pane. Instead of "Filtered Rows," right-click the step and rename it to "Filtered for 2023 Sales." This makes it much easier to audit your work or hand the report over to a colleague. If you find yourself performing the same set of transformations on multiple tables, consider creating a Power BI Dataflow, which allows you to store the query in the cloud and reuse it across different reports.
When working with very large SQL databases, try to use Query Folding. This occurs when Power BI translates your Power Query steps into a single SQL statement that the database executes before sending the results back. To check if folding is happening, right-click a step in the Applied Steps pane; if "View Native Query" is clickable, the query is folding. If it is greyed out, Power BI is doing the processing locally, which is slower.
Troubleshooting
If you receive an error stating that a column cannot be found after you have applied your changes, it is usually because a column name was changed or deleted in the source file. To fix this, open the Power Query Editor, look at the Applied Steps pane, and find the first step with a red X or warning icon. Click the gear icon next to that step to update the column reference or delete the step and re-apply it.
If the data types are not updating correctly or you see "Error" in your cells, first check for nulls or text in numeric columns. Use the Remove Errors or Replace Errors option under the Home tab to handle these anomalies. If the entire query is running slowly, try reducing the number of columns imported or check if you have too many "Merge Queries" steps, which can create a heavy computational load on your machine.
Frequently Asked Questions
Can I change the query after I have already loaded the data?
Yes. You can click the Transform Data button on the Home tab at any time to return to the Power Query Editor. Any changes you make there will be applied to the data the next time you click Close & Apply.
What is the difference between a Filter and a Query in Power BI?
A query (via Power Query) removes data before it ever reaches the report model, reducing file size and increasing speed. A filter (on the report canvas) simply hides data from the user's view while the data still exists in the background.
How do I combine two different tables into one query?
You can use Merge Queries to join tables based on a matching column (similar to a SQL Join) or Append Queries to stack one table on top of another (similar to a SQL Union).
Will my table query update automatically when the source file changes?
Yes. When you click the Refresh button in Power BI, the system re-runs every step in your Applied Steps list against the current version of the source file and updates the table.
Can I write my own SQL code instead of using the buttons?
Yes. When connecting to a database like SQL Server, you can click Advanced options in the connection window and paste a specific SQL statement into the "SQL statement" box to filter the data at the server level.