Forum Discussion
Trying to transform data into different data structure using direct query
Hi,
I have data that is stored in a SQL database that I would like to do some analysis in Power BI using direct query. But since the data structure in the database do not meet my need to present in Power BI, I need to change the formatting and pivot the table before I can do analysis.
Here is the old data structure,
Old data structure
and I would like to transform the data into this using direct query
So, I wrote a query and tried in Azure to see if I can get the new data structure, and it works
Here is the query
----
WITH CTE_MoSM (BreakOutID, M1, M2)
AS
(Select *
FROM (
Select mosmMetric.MetricName AS MetricName, BreakoutID, Value AS MetricValue from mosmMetricValue
Left join mosmMetric ON mosmMetricValue.MetricID = mosmMetric.MetricID
Where Year = '2021') AS MoSM2021NP
PIVOT
(MAX(MetricValue) FOR MetricName IN ([Sourceable spend as a percent of revenue],
[Managed spend as a percent of sourceable spend])) AS MoSM2021P
)
SELECT BreakoutID, FORMAT(M1, 'P')AS 'Sourceable spend as a percent of revenue',FORMAT(M2, 'P')AS 'Managed spend as a percent of sourceable spend' FROM CTE_MoSM
---
However, Power BI popped up an error message,
Error
it looks like I can't use CTE in the direct query of Power BI, does anyone have any solution for this?
Thank you very much for your help!!
16 Replies
- AlexisOlsonSuper User
Have you tried pivoting in the Power Query?
- IWallisHelper I
Yeah I was able to pivot and format the data in Power Query, if I used the import method to get the data instead of direct query. When I use direct query to pull out the data, power BI doesnt allow me to use power query to transform the data, or the changes wont be updated.
- AlexisOlsonSuper User
I've tested it and it's possible to pivot in the Query Editor without breaking DirectQuery.
It does require you to use an aggregation function rather than choosing Don't Aggregate though:
- IWallisHelper I
I did try to use pivot and it will do it for me in power query but it said on the top that it won't apply, but I am wondering if it is because I am not authorized to write in the database?
- smpa01Community Champion
- v-angzheng-msftCommunity Support
Hi, IWallis
May I ask if your problem has been solved? Is the above post helpful to you?
If it does, could you please mark the post which help as Answered? It will help the others in the community find the solution easily if they face the same problem with you. Thank you.
Best Regards,
Community Support Team _ Zeon ZhengIf this post helps, then please consider Accept it as the solution to help the other members find it more quickly.