SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


POWER BI and Declare


POWER BI and Declare

Author
Message
craig.jenkins
craig.jenkins
Ten Centuries
Ten Centuries (1K reputation)Ten Centuries (1K reputation)Ten Centuries (1K reputation)Ten Centuries (1K reputation)Ten Centuries (1K reputation)Ten Centuries (1K reputation)Ten Centuries (1K reputation)Ten Centuries (1K reputation)

Group: General Forum Members
Points: 1017 Visits: 254

Hi Guys,

I have the following code but for some reason Power BI doesn't seem to accept declare. Does anyone know a way around this? The error message I get in Power BI is:

Query1 Microsoft SQL: Incorrect syntax near the keyword 'DECLARE'

Many thanks

DECLARE @sd DATETIME, @ed DATETIME;

-- set the start date to the first day of this month
SET @sd = DATEADD(MONTH, DATEDIFF(MONTH, 0, GETDATE()), 0);

-- if it's the first, we want last month instead
IF DATEPART(DAY, GETDATE()) = 1
BEGIN
SET @sd = DATEADD(MONTH, -1, @sd);
END
ELSE

-- set end dates
IF DATEPART(DAY, GETDATE()) = 1
SET @ed = DATEADD(MONTH, 1, @sd)
ELSE
SET @ed = DATEADD(dd, DATEDIFF(dd,0,GETDATE()), 0)


SELECT logfile.Dealer,Dealers.Name
,COUNT(*) AS Booked
, SUM(CASE RepairCodes.[Service] WHEN 'Y' THEN 1 ELSE 0 END) AS SER
, SUM(CASE ServCode WHEN 'MST' THEN 1 WHEN 'S&M' THEN 1 ELSE 0 END) AS MST
, SUM(CASE ServCode WHEN 'MOT' THEN 1 ELSE 0 END) AS MOT
, SUM(CASE ServCode WHEN 'WAR' THEN 1 ELSE 0 END) AS WAR
, SUM(CASE PayType WHEN 'INTERNAL' THEN 1 ELSE 0 END) AS [INT]
,COUNT(DISTINCT RegNo) AS Vehs

,COUNT(*)
-SUM(CASE RepairCodes.[Service] WHEN 'Y' THEN 1 ELSE 0 END)
-SUM(CASE ServCode WHEN 'MST' THEN 1 WHEN 'S&M' THEN 1 ELSE 0 END)
-SUM(CASE ServCode WHEN 'MOT' THEN 1 ELSE 0 END)
-SUM(CASE ServCode WHEN 'WAR' THEN 1 ELSE 0 END) AS Other

, SUM(TotalValue) AS [Value]

FROM LogFile
LEFT OUTER JOIN RepairCodes
ON RepairCode = ServCode
AND LogFile.Dealer = RepairCodes.Dealer

JOIN Dealers ON Dealers.Dealer=LogFile.Dealer

AND dbo.LogFile.Created >= @sd
AND dbo.LogFile.Created < @ed
AND DBO.LogFile.Tran1 in ( 'IBB','W3B')
GROUP BY LogFile.Dealer, Dealers.Name
order by Dealers.Name


pietlinden
pietlinden
SSC Guru
SSC Guru (54K reputation)SSC Guru (54K reputation)SSC Guru (54K reputation)SSC Guru (54K reputation)SSC Guru (54K reputation)SSC Guru (54K reputation)SSC Guru (54K reputation)SSC Guru (54K reputation)

Group: General Forum Members
Points: 54578 Visits: 17300
I tried to reproduce your problem by using DECLARE inside the query window in PowerBI (after you specify the database etc), and mine worked. Granted, my SQL was a lot simpler... so it's not the DECLARE that's causing the problem. I executed this inside that query window in PowerBi:

DECLARE @NumRecords INT
SET @NumRecords = 1+ABS(CHECKSUM(NEWID())%10);

SELECT TOP (@NumRecords) *
FROM dbo.SymptomData;

and mine worked, so there's something else going on in there. What happens if you execute your T-SQL inside SSMS? It works there, right?.

craig.jenkins
craig.jenkins
Ten Centuries
Ten Centuries (1K reputation)Ten Centuries (1K reputation)Ten Centuries (1K reputation)Ten Centuries (1K reputation)Ten Centuries (1K reputation)Ten Centuries (1K reputation)Ten Centuries (1K reputation)Ten Centuries (1K reputation)

Group: General Forum Members
Points: 1017 Visits: 254
Thanks for coming back to me. Yeah, its works fine in SSMS and also I import the query into excel and it also works fine.
Eirikur Eiriksson
Eirikur Eiriksson
SSC Guru
SSC Guru (164K reputation)SSC Guru (164K reputation)SSC Guru (164K reputation)SSC Guru (164K reputation)SSC Guru (164K reputation)SSC Guru (164K reputation)SSC Guru (164K reputation)SSC Guru (164K reputation)

Group: General Forum Members
Points: 164730 Visits: 23885
Quick question, have you tried separating the declare into two lines?
Cool


DECLARE @sd DATETIME;
DECLARE @ed DATETIME;

craig.jenkins
craig.jenkins
Ten Centuries
Ten Centuries (1K reputation)Ten Centuries (1K reputation)Ten Centuries (1K reputation)Ten Centuries (1K reputation)Ten Centuries (1K reputation)Ten Centuries (1K reputation)Ten Centuries (1K reputation)Ten Centuries (1K reputation)

Group: General Forum Members
Points: 1017 Visits: 254
Amazing Eirikur Eiriksson, it now works fine thank you so much
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum









































































































































































SQLServerCentral


Search