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

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

Reply
Anonymous
Not applicable

select with direct query

Hello,
This sql works in sql server and now I would like  to run it in power BI.
So I thought may be I can somehow run the below query using direct query?
How is it possible please?
It seems I can not just paste this code in the sql statement section of the directquery

 

create table #tblQuarterlyDate(QuarterKey int, StartMonthDayKey varchar(10))
insert into #tblQuarterlyDate values (1, '0105')
insert into #tblQuarterlyDate values (2, '0405')
...


Select DateKey, [Quarter],Price
,sum(case when right(str(DateKey,8),4)=qd.StartMonthDayKey then Price else null end)
over( order by DateKey) qPrice
from tblFact f join #tblQuarterlyDate qd on f.Quarter=qd.QuarterKey

1 ACCEPTED SOLUTION
Anonymous
Not applicable

Hi,
This seems good although now the issue is using your method I would like to add an extra column which is a calculation i.e.

Select DateKey, [Quarter],Price
,sum(case when right(str(DateKey,8),4)=qd.StartMonthDayKey then Price else null end)

over( order by DateKey) qPrice

, CalculationPerformance = Price/qPrice - 1

from tblFact f join #tblQuarterlyDate qd on f.Quarter=qd.QuarterKey

 

Thank you

View solution in original post

2 REPLIES 2
TomMartens
Super User
Super User

Hey @Anonymous ,

from your post, it seems that there is a DDL part and a DML part. You can use only SELECT ... or EXECUTE PROCEDURE ... statements as a native query.

 

Because of this, you have to rewrite your SQL code to something like this

SELECT
...
FROM tblFact f join
(SELECT 1 as QuarterKey, '0105' as StartMonthDayKey
UNION
SELECT 1 as QuarterKey, '0105' as StartMonthDayKey
UNION
...
) as qd on f.Quarter=qd.QuarterKey

You also have to be aware that SQL code written in something like SQL Server Management Studio or Azure Data Studio, does not necessarily represent one SQL Object like a view, as often different statements are separated by a semicolon, or should be separated by a semicolon. Sometimes the SQL code is separated into multiple batches by using GO.

 

What can be used inside Power BI without any modification is a single statement that starts with SELECT.

 

Regards,
Tom



Did I answer your question? Mark my post as a solution, this will help others!

Proud to be a Super User!
I accept Kudos 😉
Hamburg, Germany
Anonymous
Not applicable

Hi,
This seems good although now the issue is using your method I would like to add an extra column which is a calculation i.e.

Select DateKey, [Quarter],Price
,sum(case when right(str(DateKey,8),4)=qd.StartMonthDayKey then Price else null end)

over( order by DateKey) qPrice

, CalculationPerformance = Price/qPrice - 1

from tblFact f join #tblQuarterlyDate qd on f.Quarter=qd.QuarterKey

 

Thank you

Helpful resources

Announcements
Fabric Data Days Carousel

Fabric Data Days

Advance your Data & AI career with 50 days of live learning, contests, hands-on challenges, study groups & certifications and more!

October Power BI Update Carousel

Power BI Monthly Update - October 2025

Check out the October 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.