Power Query Problems

How to Handle Empty Lists in Power Query M Without Errors

Maira MehtabMaira Mehtab Nov 10, 2026 869 views

Question details

The user needs to prevent SQL syntax errors caused by empty lists when dynamically constructing SQL queries in Power Query M.

How to Handle Empty Lists in Power Query M Without Errors
Product
Power Query
Device & OS
not provided
Scenario
Dynamically building a SQL WHERE clause using Power Query lists.
Observed behavior
Using Text.Combine on an empty list creates an invalid SQL condition (like an empty IN clause), resulting in database query errors.
Before you start

Ensure you have access to the Advanced Editor in Power Query and understand the basic structure of your M code as well as your target SQL database schema.

Solution 1Recommended

Use List.IsEmpty to Conditionally Build SQL Clauses

Prevent invalid SQL syntax by verifying list contents before constructing the IN clause for your native database query.

When manually building native SQL queries in M, passing an empty list to Text.Combine results in a malformed WHERE clause.

By wrapping the condition in a List.IsEmpty check, you can safely ignore empty lists and dynamically construct a valid query string.

1
Open the Advanced Editor

In Power Query Editor, go to the Home tab and click on 'Advanced Editor' to view and modify your M code.

2
Add conditional statements for your lists

Define your condition string using an IF statement. For example: FundCondition = if List.IsEmpty(lstFund) then "" else "[FUND_CD] in (" & Text.Combine(lstFund, ",") & ")".

3
Combine valid conditions

Filter out the blank strings and combine the valid conditions using Text.Combine(List.Select({FundCondition, FYCondition}, each _ <> ""), " AND ").

4
Append to the SQL source query

Concatenate the resulting combined condition string to the end of your SQL WHERE clause in the Sql.Database function.

Use List.IsEmpty to Conditionally Build SQL Clauses
Alternative Function: Instead of List.IsEmpty, you can also use List.NonNullCount(yourList) > 0 to verify the list contains valid items before processing.
Free Microsoft Office alternative

Discover WPS Office for Your Data Analysis Needs

While Power Query M advanced scripting is specific to Microsoft environments, WPS Office provides a lightweight, highly compatible alternative for comprehensive data processing, dashboard creation, and everyday spreadsheet tasks.

Free and lightweight office suite for Windows, Mac, and LinuxHigh format compatibility with Microsoft Excel (.xlsx, .xls) filesFamiliar user interface ensuring a seamless migration experienceBuilt-in advanced data analysis, pivot tables, and visualization tools
microsoft office alternative - wps office

Frequently Asked Questions

What causes an error when using Text.Combine on an empty list in Power Query?

When Text.Combine processes an empty list, it returns an empty string. If this empty string is concatenated directly into a SQL statement (such as 'IN ()'), it creates invalid SQL syntax, causing the database to reject the query.

How can I check if query folding is active in Power Query?

You can verify query folding by right-clicking an applied step in the Power Query Editor pane. If the 'View Native Query' option is clickable (not grayed out), it means Power Query is successfully folding those steps into a native database query.

Are there other ways to check for an empty list besides List.IsEmpty?

Yes, you can check the count of items by using List.Count(myList) = 0, or check for non-null values using List.NonNullCount(myList) = 0. Both are valid alternatives to determine if a list requires SQL parameter generation.