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

Don't miss out! 2025 Microsoft Fabric Community Conference, March 31 - April 2, Las Vegas, Nevada. Use code MSCUST for a $150 discount. Prices go up February 11th. Register now.

Reply
lovishsood1
Helper III
Helper III

Not able to save CTE as Table or View

Hello Everyone,

 

I have created a CTE as follows;

 

WITH cte as
(
SELECT
        Month_Name,
        Month_Number,
        Project_Category,
        
        100.0 * Actual_Hours / NULLIF(SUM(Actual_Hours) OVER (PARTITION BY Month_Name, Month_Number), 0) AS Percentage
    FROM
        [DW_MonthlyReport].[dbo].[TimelogByType]
        --GROUP by Month_Name,Month_Number,Project_Category
        )
        select cte.Month_Name,
        cte.Month_Number,
        cte.Project_Category,Sum(cte.Percentage) AS Percentage from cte GROUP by cte.Month_Name,cte.Month_Number,cte.Project_Category;

 

When I'm trying to save it as View of Table.
 
I'm getting this error;
lovishsood1_0-1699019582700.png

 

What I'm doing wrong Please tell.

1 ACCEPTED SOLUTION

I think this is now a Power BI issue rather than a Synapse issue. You will be better served posting this logic issue in the Power BI forum

View solution in original post

11 REPLIES 11
lovishsood1
Helper III
Helper III

I want to save this SCRIPT as TABLE so that i can use the OUTPUT in Power BI.

;

WITH cte AS
(
    SELECT
        Month_Name,
        Month_Number,
        Project_Category,
        100.0 * Actual_Hours / NULLIF(SUM(Actual_Hours) OVER (PARTITION BY Month_Name, Month_Number), 0) AS Percentage
    FROM
        [DW_MonthlyReport].[dbo].[TimelogByType]
    -- GROUP by Month_Name,Month_Number,Project_Category
)

SELECT
    cte.Month_Name,
    cte.Month_Number,
    cte.Project_Category,
    SUM(cte.Percentage) AS Percentage
FROM
    cte
GROUP BY
    cte.Month_Name,
    cte.Month_Number,
    cte.Project_Category;

Hello @lovishsood1 
When you say "I want to save this SCRIPT as TABLE "  , I am asuming that the intend is just to have these rows on some report , please correct me if I am wrong . I think we can use the view route for this for now.

 

Since you have a CTE which you are running and i suggest you can create a view using the the CTE ( @AndyDDC  was also pointing that out ) and the use that view to get the data in the report . I have some dummy data with which i tried and it worked . 

Create TABLE TimelogByType
(Month_Name varchar(100) ,Month_Number int ,Project_Category varchar(100) , Actual_Hours int  )
 
 
INSERT INTO TimelogByType values ('Jan',202301,'AAA',8)
INSERT INTO TimelogByType values ('Jan',202301,'AAA',18)
INSERT INTO TimelogByType values ('Feb',202302,'BBB',118)
INSERT INTO TimelogByType values ('Feb',202302,'CCC',1118)
 
CREATE view dbo.vwGetData
As

WITH cte as
(
SELECT
        Month_Name,
        Month_Number,
        Project_Category,
       
        100.0 * Actual_Hours / NULLIF(SUM(Actual_Hours) OVER (PARTITION BY Month_Name, Month_Number), 0) AS Percentage
    FROM
        [dbo].[TimelogByType]
        )
        select cte.Month_Name,
        cte.Month_Number,
        cte.Project_Category,Sum(cte.Percentage) AS Percentage from cte GROUP by cte.Month_Name,cte.Month_Number,cte.Project_Category;
 
select * from dbo.vwGetData
 
HimanshuSmsft_0-1699034375896.png

 

 You can also call this a inn powerBI report as this . 
HimanshuSmsft_1-1699034451638.png

 



 let me know if you have any question .
Thanks 
Himanshu 









 

 

 

 

 

I think this worked but problem is 

I'm still not able to resolve what I actually I want to achieve.

 

This should be my end output:

lovishsood1_0-1699035740789.png

But the Problem is My data is coming out of 100% and Above Output is showing DATA according to Total Hours for that month.

 

I hope I'm able to describe my problem statement.

Current Table : 

lovishsood1_1-1699035874595.png

 

 

Output Table : 

lovishsood1_2-1699035898881.png

 

I want to achieve like this so that I can create the graph as shown above.

You can ignore the Month Column of Output table as I'm okay with using Month Name & Month Number column as it is.

 

I hope my problem is clear.

 

 

Script I'm using I'm already mentioned above.

I think this is now a Power BI issue rather than a Synapse issue. You will be better served posting this logic issue in the Power BI forum

AndyDDC
Super User
Super User

Can you post the whole SQL script you're using the create the view?  eg

 

 

create view dbo.vwGetData

AS

with datav3

AS

(

    select * from fact_sales

)

select * from datav3

This is the entire script. 

lovishsood1_1-1699032058834.png

 

v-nikhilan-msft
Community Support
Community Support

Hi @lovishsood1 ,
Thanks for using Fabric Community.
Apologies for the issue you have been facing.
I tried to repro the scenario. I have a table named Allotment in my warehouse and I performed a T-SQL operation as follows:

vnikhilanmsft_1-1699022227020.png

 

I did not receive any error here.

Try adding a ; before your WITH clause.

Start your query like this ; WITH cte as ....

Hope this helps. Please let me know if you have any further issues. Glad to help.

Hi,

 

Thank you the reply.

 

But Output is perfectly being shown. 
Problem is coming when I try to save the OUTPUT as VIEW of Save it as the table.

 

lovishsood1_0-1699032006350.png

 

if you want to create a view, just put the following syntax before your WITH statement:

 

CREATE VIEW dbo.viewname 

AS

WITH...

Hi @lovishsood1 ,
Thanks for the input.
Apologies for the issue you have been facing. The option for SAVE AS TABLE  should not be available in the Fabric Warehouse SQL endpoint UI . SQL endpoint in Warehouse is read only for tables. Sorry for the inconvenience.

 

I have raised a bug on the same and escalated it to the internal team. The option should be removed from the Warehouse SQL endpoint. I will keep you posted regarding the updates. Appreciate your patience.

Hope this helps. Please let me know if you have any further questions.

I cannot save this as VIEW also?
How can I achieve this output in Power Query Editor so that i can calculate Each Project Category Hours for each month?

 

Formula :
(Actual Hours for Project Category / Tota Hours for that month)*100

You can check the QUERY SNAPSHOT also.

Helpful resources

Announcements
Las Vegas 2025

Join us at the Microsoft Fabric Community Conference

March 31 - April 2, 2025, in Las Vegas, Nevada. Use code MSCUST for a $150 discount! Prices go up Feb. 11th.

JanFabricDE_carousel

Fabric Monthly Update - January 2025

Explore the power of Python Notebooks in Fabric!

JanFabricDW_carousel

Fabric Monthly Update - January 2025

Unlock the latest Fabric Data Warehouse upgrades!

JanFabricDF_carousel

Fabric Monthly Update - January 2025

Take your data replication to the next level with Fabric's latest updates!