Why I am getting Microsoft SQL: Incorrect syntax error on Power BI?

4.5k views Asked by At

I am trying to get some data using CTE, but it gives me an error that I have to use semicolons. I think I fixed that by putting ; in front of WITH, but now I am getting Microsoft SQL: Incorrect syntax near ';'. Incorrect syntax near ')'.

Can anybody please tell me what is wrong? It used to be very easy, just copy the query from MS SQL to Power BI. My query runs fine in MS SQL. 

;WITH unit AS (
   SELECT
         tm.create_date 
       , tm.timeslip_date
       , cases.case_sk
       , cases.case_number
       , cases.closed_ind
       , cases.atty2_sk
       , vc.atty2_name    AS [Business Leader]
       , em.smtp_reply_to AS [Business Leader Email]
       , cases.atty1_sk
       , vc.atty1_name    AS [Assign Attorney]
       , tm.detail_notes
   FROM dbo.cases
   LEFT JOIN dbo.vcases vc ON cases.case_sk = vc.case_sk
   LEFT JOIN dbo.employee em ON cases.atty2_sk = em.employee_sk    
   LEFT JOIN dbo.timeslips tm ON cases.case_sk = tm.case_sk
   WHERE cases.closed_ind = 'O'
   AND NOT EXISTS(SELECT * FROM dbo.timeslips tsm WHERE tsm.case_sk = cases.case_sk AND tsm.timeslip_date > DATEADD(day, -90, GETDATE()) )
), agg AS (
   SELECT
         MIN(u.create_date)   AS [Created Date]
       , MAX(u.timeslip_date) AS [Last Bill Date]
       , u.case_sk
       , u.case_number
       , u.closed_ind
       , u.atty2_sk
       , u.[Business Leader]
       , u.[Business Leader Email]
       , u.atty1_sk
       , u.[Assign Attorney]
   FROM unit u
   GROUP BY 
         u.case_sk
       , u.case_number
       , u.closed_ind
       , u.atty2_sk
       , u.[Business Leader]
       , u.[Business Leader Email]
       , u.atty1_sk
       , u.[Assign Attorney]
)
SELECT agg.*, unit.detail_notes
FROM agg
INNER JOIN unit
   ON  agg.[Last Bill Date] = unit.[timeslip_date]
        AND agg.case_sk = unit.case_sk
        AND agg.case_number = unit.case_number
        AND agg.closed_ind = unit.closed_ind
        AND agg.atty2_sk = unit.atty2_sk
        AND agg.atty1_sk = unit.atty1_sk

Thank you  

1

There are 1 answers

2
David Browne - Microsoft On BEST ANSWER

When you use a custom query as a DirectQuery source you must supply a query that PowerBI can compose additional critera and joins on at runtime.

If you create a DirectQuery source with a query like this

with q as (select * from sys.objects) select * from q

PowerBI will compose queries like

select * from (
    with q as (select * from sys.objects) select * from q
) SourceQuery where 1 = 2

The error you get has nothing to do with whether you have a statement terminator or not. ; is not allowed in the middle of a query, but nether is with. CTE's are great but they have a flaw: they are not "composable".

You can work around this by installing the query as a view in SQL Server, or transforming the CTE subqueries into nested subqueries, but that query is really much too complicated to be used in a DirectQuery model. So you'd really need to load the results into a table to use DirectQuery, or switch to Import and run the query only on refresh.