How to Handle Empty Lists in Power Query M Without Errors
Question details
The user needs to prevent SQL syntax errors caused by empty lists when dynamically constructing SQL queries in Power Query M.

- 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.
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.
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.
In Power Query Editor, go to the Home tab and click on 'Advanced Editor' to view and modify your M code.
Define your condition string using an IF statement. For example: FundCondition = if List.IsEmpty(lstFund) then "" else "[FUND_CD] in (" & Text.Combine(lstFund, ",") & ")".
Filter out the blank strings and combine the valid conditions using Text.Combine(List.Select({FundCondition, FYCondition}, each _ <> ""), " AND ").
Concatenate the resulting combined condition string to the end of your SQL WHERE clause in the Sql.Database function.

Utilize Native Query Folding with Table.SelectRows
Let Power Query automatically generate the correct SQL by using standard M functions instead of manually injecting SQL strings.
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.

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.




