Skip to main content
cancel
Showing results for 
Search instead for 
Did you mean: 

Join the Fabric FabCon Global Hackathon—running virtually through Nov 3. Open to all skill levels. $10,000 in prizes! Register now.

Reply
AbhinavJoshi
Responsive Resident
Responsive Resident

Replace query value with null when it has no data

Hello,

 

I am passing a list of values from a query to my sql query in where clause. The problem is when the value is null, it gives me incorrect syntax error. Any ideas how to fix this?
My Query in Source

= Sql.Database("server", "db", [Query="Select val1, val2, val3,  from table INNER JOIN table2 ON table.id = table1.oid where CONCAT(table.val, table1.val1) in ("& Text.Combine(UniqueCerts,",") &") and val3= 'ABC' "])

 

The Highleted text causes an error when UniqueCerts value is blank. I would like to replace it with some value if it is null or some workaround.

 

Thanks

2 ACCEPTED SOLUTIONS

Got it, adjusted the query below:

 

let
DefaultCert = "'N/A'", // Ensure the default value is properly quoted
FilteredCerts = List.Select(UniqueCerts, each _ <> null),
CertsList = if List.Count(FilteredCerts) > 0 then Text.Combine(FilteredCerts, ",") else DefaultCert,
Query = "Select val1, val2, val3 from table INNER JOIN table2 ON table.id = table1.oid where CONCAT(table.val, table1.val1) in (" & CertsList & ") and val3= 'ABC'"
in
Sql.Database("server", "db", [Query=Query])




Did I answer your question? Mark my post as a solution!

Proud to be a Super User!





View solution in original post

This would work but I figured it out the best option is to skip the sql query as a whole if the UniqueCerts list is empty, and used an empty default table. Thanks for your help

View solution in original post

4 REPLIES 4
Kaviraj11
Super User
Super User

Hi,

 

can you try with the code below:

 

let
FilteredCerts = List.Select(UniqueCerts, each _ <> null),
Query = "Select val1, val2, val3 from table INNER JOIN table2 ON table.id = table1.oid where CONCAT(table.val, table1.val1) in (" & Text.Combine(FilteredCerts, ",") & ") and val3= 'ABC'"
in
Sql.Database("server", "db", [Query=Query])




Did I answer your question? Mark my post as a solution!

Proud to be a Super User!





Hi @Kaviraj11 ,
This won't work as the FilteredCerts would still be null if the UniqueCerts has no values. It would cause the same error.

Got it, adjusted the query below:

 

let
DefaultCert = "'N/A'", // Ensure the default value is properly quoted
FilteredCerts = List.Select(UniqueCerts, each _ <> null),
CertsList = if List.Count(FilteredCerts) > 0 then Text.Combine(FilteredCerts, ",") else DefaultCert,
Query = "Select val1, val2, val3 from table INNER JOIN table2 ON table.id = table1.oid where CONCAT(table.val, table1.val1) in (" & CertsList & ") and val3= 'ABC'"
in
Sql.Database("server", "db", [Query=Query])




Did I answer your question? Mark my post as a solution!

Proud to be a Super User!





This would work but I figured it out the best option is to skip the sql query as a whole if the UniqueCerts list is empty, and used an empty default table. Thanks for your help

Helpful resources

Announcements
September Power BI Update Carousel

Power BI Monthly Update - September 2025

Check out the September 2025 Power BI update to learn about new features.

FabCon Atlanta 2026 carousel

FabCon Atlanta 2026

Join us at FabCon Atlanta, March 16-20, for the ultimate Fabric, Power BI, AI and SQL community-led event. Save $200 with code FABCOMM.

Top Solution Authors
Top Kudoed Authors