简体   繁体   中英

Query works in Power Query Editor but not in Power BI

I am just getting started with Power BI and want to be able to compare report performance between Direct Query and Import mode. What I am about to describe may be possible by performing data manipulations in Import mode. That is not what this question is about.

Using Power BI against SQL Server 2016.

I am working with data that is, unfortunately, pre-pivoted. In order to unpivot it, I am doing something like this:

;
with a as (
       SELECT pd.[ProjectDollarsId]
       , pd.[VersionId]
       , pd.[ProjectId]
       , pd.[FundTypeId]
       , pd.[ApprovalId]
       , pd.[PriorDollar]
       , pd.[LastYearDollar]
       , pd.[ThisYearDollar]
       , pd.[NextYearDollar]
       , pd.[FutureDollar]
       , pd.[TotalDollar]
       , v.[Year] as VersionYear

       FROM [ProjectDollars] pd
         inner join [Version] v on v.VersionId = pd.VersionId

       where v.[Year] > 2016
), 
b as (
       select ProjectDollarsId
       , VersionId
       , ProjectId
       , ProjectPhaseId
       , FundTypeId
       , ApprovalId
       , VersionYear
       , DollarsYear
       , Dollars

       from a pvt
       unpivot (
              Dollars for DollarsYear in (
                     PriorDollar, 
                     LastYearDollar, 
                     ThisYearDollar, 
                     NextYearDollar, 
                     FutureDollar)
              ) as unpvt
)

select ProjectDollarsId
, VersionId
, ProjectId
, FundTypeId
, ApprovalId
, case DollarsYear
       when 'PriorDollar'    then VersionYear - 2
       when 'LastYearDollar' then VersionYear - 1
       when 'ThisYearDollar' then VersionYear
       when 'NextYearDollar' then VersionYear + 1
       when 'FutureDollar'   then VersionYear + 2
    else 0
  end as [Year]
, Dollars
from b

This works fine in Power Query Editor. I get a resulting table with correct column names and data to preview.

After I click Close and Apply, Power BI says:

Microsoft SQL: Incorrect syntax near ';'. Incorrect syntax near ')'.

What do I need to do to work around this?

Power BI will compose query like this:

select * from (<your query>) SourceQuery

ie

select * from (; with a as (SELECT pd.[ProjectDollarsId] ... ) SourceQuery

It does that, because it needs to add joins and conditions at runtime. Obviously the above query is not valid. You need to implement the query logic using M and DAX in Power BI, or to wrap it in a view in the database.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM