How to Use Google Sheets’ QUERY Function for Data Cleaning (Versus Excel’s Power Query)

How to Use Google Sheets' QUERY Function for Data Cleaning (Versus Excel's Power Query)

Both Google Sheets’ QUERY function and Excel’s Power Query are powerful tools for data cleaning and transformation. They offer efficient ways to clean data for analysis, but the approach is different. This post will show the use of the QUERY function in Google Sheets for data cleaning with practical examples while comparing it with Excel’s Power Query tool.

Google Sheets’ QUERY Function

The QUERY function allows you to run SQL-like queries on your data range. SQL queries help to clean data by filtering, sorting, aggregating, formatting, and transforming data within a spreadsheet. 

Syntax:

=QUERY(data, query, [headers])

  • data: The range of cells to perform the query on.
  • query: SQL-like queries to apply to your data range.
  • [headers]; [Optional]: It indicates the number of header rows at the top of the data. 

Power Query in Excel

Power Query is a robust tool for data cleaning and transformation. Its interactive graphical interface helps to clean data before loading it into Excel. Power Query imports data from various sources and offers data connectivity. It has flexible features to perform complex data-cleaning tasks. 

Example of Data Cleaning by Using Google Sheets QUERY Function and Excel’s Power Query Tool

Let’s consider sales data that has issues like inconsistent data, incorrect data format, and duplicate rows that need cleaning.  Now, you can use the QUERY function and the Power Query tool for data cleaning. Let us show you the most common data cleaning process in Google Sheets and Excel by using both tools.

Sample Sales Data: 

This sales data contains inconsistent Product and Salesperson names, missing Sales Amounts, and duplicate rows. We will clean this whole dataset through the examples.

Order ID Date Product Category Sales Amount Salesperson
10001 01/01/2024 Laptop Electronics $2,200.00 miKe Jonas
10001 01/01/2024 Laptop Electronics $2,200.00 miKe Jonas
10002 05/01/2024 Smartphone Electronics   Steve smitH
10003 06/01/2024 TV Electronics $1,500.00 lara Jonson
10004 07/01/2024 Air Pods Electronics $100.00 emily Copper
10005 07/01/2024 Book Books   Erin Doe
10006 08/01/2024 Chair Furniture $150.00 Sarah Lee
10006 08/01/2024 Chair Furniture $150.00 Sarah Lee
10007 09/01/2024 table Furniture $300.00 Lio Green
10008 10/01/2024 Jacket Clothing $80.00 phobe buffy
10009 18/01/2024 shoes Clothing $120.00 Elian Swift
10010 20/01/2024 Monitor Electronics $200.00 Erin Doe

Remove Duplicates

Let’s remove the duplicate rows from the sales data.

QUERY Function in Google Sheets:

The QUERY function directly can’t remove duplicates in Google Sheets. You will need to use the combination of the UNIQUE function with the QUERY function. Insert the following formula cell H1.

=QUERY(UNIQUE(A1:F13), “SELECT *”)

This formula first uses the UNIQUE function to remove duplicate rows then the QUERY function extracts the cleaned data.

Power Query in Excel:

You can simply use the “Remove Duplicates” command in the Power Query editor to remove the duplicate rows in Excel. To do so,

  • Select your dataset in Excel and open it in Power Query by navigating to Data >> from Table/Range.
  • Now, go to Home tab >> select Reduce Rows >> from Remove Rows >> select Remove Duplicates

How to Use Google Sheets' QUERY Function for Data Cleaning (Versus Excel's Power Query)

Output: Duplicate rows are removed from the sales dataset in both Google Sheets and Excel. You will get the output in cell H1:M13.

Order ID Date Product Category Sales Amount Salesperson
10001 01/01/2024 Laptop Electronics $2,200.00 miKe Jonas
10002 05/01/2024 Smartphone Electronics   Steve smitH
10003 06/01/2024 TV Electronics $1,500.00 lara Jonson
10004 07/01/2024 Air Pods Electronics $100.00 emily Copper
10005 07/01/2024 Book Books   Erin Doe
10006 08/01/2024 Chair Furniture $150.00 Sarah Lee
10007 09/01/2024 table Furniture $300.00 Lio Green
10008 10/01/2024 Jacket Clothing $80.00 phobe buffy
10009 18/01/2024 shoes Clothing $120.00 Elian Swift
10010 20/01/2024 Monitor Electronics $200.00 Erin Doe

Filter Out Missing/Incomplete Rows

By using both the QUERY function and the Power Query tool you can filter out the unwanted rows from the sales dataset. This sales data contains null values in the “Sales Amount” column.

QUERY Function in Google Sheets:

To remove the incomplete rows from the (H1:M13) cell range where the Sales Amount is missing you can use the QUERY function to filter out those unwanted rows. Select cell O1 to insert the following formula.

=QUERY(H1:M13, “SELECT * WHERE L IS NOT NULL”)

This formula will filter out the rows where the sales amount is not null. It will remove the empty sales amount rows.

Power Query in Excel:

To remove the unwanted rows, load your data into the Power Query editor,

  • Select “Removed Duplicates” from Applied Steps.
  • Click on the drop-down arrow in the Sales Amount column header.
  • In the filter menu, only deselect null.
  • Click on OK to apply the filter. It will filter out rows with missing sales amounts.

How to Use Google Sheets' QUERY Function for Data Cleaning (Versus Excel's Power Query)

This Filter Rows feature easily removes unwanted data. 

Output: Unwanted empty sales amount rows are removed from the sales dataset in both Google Sheets and Excel. You will get the output in cell O1:T9.

Order ID Date Product Category Sales Amount Salesperson
10001 01/01/2024 Laptop Electronics $2,200.00 miKe Jonas
10003 06/01/2024 TV Electronics $1,500.00 lara Jonson
10004 07/01/2024 Air Pods Electronics $100.00 emily Copper
10006 08/01/2024 Chair Furniture $150.00 Sarah Lee
10007 09/01/2024 table Furniture $300.00 Lio Green
10008 10/01/2024 Jacket Clothing $80.00 phobe buffy
10009 18/01/2024 shoes Clothing $120.00 Elian Swift
10010 20/01/2024 Monitor Electronics $200.00 Erin Doe

Data Transformation: Use Consistent Data Formats

Removing inconsistent data formats from the sales data to get cleaned data. The “Product” and “Salesperson” columns have inconsistent data. Let’s convert all text to the proper case.

QUERY Function in Google Sheets:

Now, use the ARRAYFORMULA and PROPER functions with the QUERY function to convert all texts to the proper case. Insert the following formula in cell V1.

=ARRAYFORMULA(QUERY({O1:T9, PROPER(Q1:Q9), PROPER(T1:T9)}, “SELECT *”))

  • This formula uses ARRAYFORMULA to apply operations over a range of data from O1:T9. 
  • PROPER(Q1:Q13) and PROPER(T1:T13) convert the text in the Product and Salesperson columns to the proper case (capitalizing the first letter of each word). 
  • The QUERY function then selects all columns (SELECT *), to return the original data including the properly capitalized versions of columns Product and Salesperson.

This formula transforms the text into new columns while maintaining the original structure of the data.

Output: Here is your final cleaned data.

Order ID Date Product Category Sales Amount Salesperson Product Salesperson
10001 01/01/2024 Laptop Electronics $2,200.00 miKe Jonas Laptop Mike Jonas
10003 06/01/2024 TV Electronics $1,500.00 lara Jonson Tv Lara Jonson
10004 07/01/2024 Air Pods Electronics $100.00 emily Copper Air Pods Emily Copper
10006 08/01/2024 Chair Furniture $150.00 Sarah Lee Chair Sarah Lee
10007 09/01/2024 table Furniture $300.00 Lio Green Table Lio Green
10008 10/01/2024 Jacket Clothing $80.00 phobe buffy Jacket Phobe Buffy
10009 18/01/2024 shoes Clothing $120.00 Elian Swift Shoes Elian Swift
10010 20/01/2024 Monitor Electronics $200.00 Erin Doe Monitor Erin Doe

Power Query in Excel:

You can perform a similar data transformation in the Power Query editor, by following the steps given below.

  • Select “Filtered Rows” from Applied Steps.
  • Next, choose the “Product” and “Salesperson” columns.
  • Go to the Transform tab >> from Text Column >> under Format >> select Capitalize Each Word.

Removed Duplicates

Product and Salesperson columns all texts are converted to proper case.

How to Use Google Sheets' QUERY Function for Data Cleaning (Versus Excel's Power Query)

Data transformation output is slightly different in Google Sheets and Excel. In Power Query, it’s easy to transform data by using commands and features. 

Conclusion

Google Sheets QUERY function is ideal for quick and simple data cleaning when users are familiar with SQL-like commands. It’s best suited for quick filtering, sorting, and basic transformations. You can use the QUERY function combined with other functions for advanced data cleaning. Excel’s Power Query is more powerful and user-friendly, especially for complex data-cleaning tasks. It performs tasks like handling missing data, removing duplicates, and advanced data transformations by using features and commands. The choice of both tools will depend on user skill and dataset complexity. 

Leave a Reply

Your email address will not be published. Required fields are marked *