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

Data Days is here! Join us now for 60+ days of learning, challenges, and connection. Learn more

Reply
nhallmark
Frequent Visitor

Query not supported in DirectQuery mode - SQL with subquery

I am attempting to organize data from a SQL server by removing rows which contain duplicates in a certain column. When using the normal remove duplicates function after a simply database connection in Power Query I received the "not supported in DirectQuery" error. I thought that I could write a SQL statement and use the advanced connection to the database to get around this error:

 

Select * from database as db,
(select min(dateandtime) as dateandtime, num, seq, type from database
where seq = 1
and type = 'A'
group by seq, num, type) Min_Date
where db.dateandtime = min_date.dateandtime

 

But this also throws the not supported error. I know one solution is to change to an import connection rather than DirectQuery, but that path is highly undesirable. Any help you can give on a work around for this method removing duplicates would be greatly appreciated.

 

Thank you

1 ACCEPTED SOLUTION

@nhallmark,

 

Any M step after a custom SQL step will prevent query folding. Here's a way to do it in M (no custom SQL).

 

1. Connect to the table (DirectQuery). Query name: lab_stl_all

 

DataInsights_0-1663854490874.png

 

2. Create a Group By query based on lab_stl_all (by Reference). Query name: lab_stl_group_by

 

DataInsights_3-1663854709666.png

 

 

DataInsights_1-1663854586062.png

 

3. Create query lab_stl using "Merge Queries as New". This will be an inner join between lab_stl_all and lab_stl_group_by. Use Ctrl-click to select multiple join columns.

 

DataInsights_4-1663854855669.png

 

4. Result:

 

DataInsights_5-1663854931476.png

 

Note that the resulting table uses Import mode.





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

Proud to be a Super User!




View solution in original post

3 REPLIES 3
DataInsights
Super User
Super User

@nhallmark,

 

I changed the SELECT clause in your custom SQL to "Select db.*" to avoid returning the columns twice, and it ran successfully. Would you confirm? If this doesn't work, please provide sample data and a screenshot of the error.





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

Proud to be a Super User!




I made the change and the query ran but once I tried to do any basic change, such as sorting or filtering a column the error gets thrown again. Here is some sample data I am using:

nhallmark_0-1663846040098.png

 

This is the exact SQL I ran:

Select stl.dateandtime, stl.htnum, stl.seq, stl.atype, stl.ccode from lab_stl stl,
(select min(dateandtime) as dateandtime, htnum, seq, atype from lab_stl
where atype = 'L'
and seq = 2

and htnum < '100'
group by seq, htnum, atype) Min_Date
where stl.dateandtime = min_date.dateandtime;

 

 

nhallmark_2-1663846298262.png

 

The query ran fine, but when I sorted by the ccode column in descending order in (you can see the step taken on the far right) the error gets thrown (yellow bar above the data). I know that I can make adjustments like this in the SQL but I need to be able to make adjustments within Power BI because I wish to merge data from two different sources into a master table and I can't do that in SQL and use it in Power BI (I think).

 

Thank you for your help!

 

@nhallmark,

 

Any M step after a custom SQL step will prevent query folding. Here's a way to do it in M (no custom SQL).

 

1. Connect to the table (DirectQuery). Query name: lab_stl_all

 

DataInsights_0-1663854490874.png

 

2. Create a Group By query based on lab_stl_all (by Reference). Query name: lab_stl_group_by

 

DataInsights_3-1663854709666.png

 

 

DataInsights_1-1663854586062.png

 

3. Create query lab_stl using "Merge Queries as New". This will be an inner join between lab_stl_all and lab_stl_group_by. Use Ctrl-click to select multiple join columns.

 

DataInsights_4-1663854855669.png

 

4. Result:

 

DataInsights_5-1663854931476.png

 

Note that the resulting table uses Import mode.





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

Proud to be a Super User!




Helpful resources

Announcements
Fabric Data Days is here Carousel

Fabric Data Days 2026

Don't miss out on Data Days, June 15 through August 7. Learn Fabric, Power BI, SQL, AI and more.

May Power BI Update Carousel

Power BI Monthly Update - May 2026

Check out the May 2026 Power BI update to learn about new features.

Power BI DataViz World Championships carousel

Power BI DataViz World Championships - June 2026

A new Power BI DataViz World Championship is coming this June! Don't miss out on submitting your entry.