简体   繁体   English

查询在 Power Query 编辑器中有效,但在 Power BI 中无效

[英]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.我刚开始使用 Power BI,希望能够比较直接查询和导入模式之间的报表性能。 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.对 SQL Server 2016 使用 Power BI。

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.这在 Power Query 编辑器中运行良好。 I get a resulting table with correct column names and data to preview.我得到一个包含正确列名和数据的结果表以进行预览。

After I click Close and Apply, Power BI says:单击关闭并应用后,Power BI 显示:

Microsoft SQL: Incorrect syntax near ';'. Microsoft SQL:“;”附近的语法不正确。 Incorrect syntax near ')'. ')' 附近的语法不正确。

What do I need to do to work around this?我需要做些什么来解决这个问题?

Power BI will compose query like this: Power BI 将像这样编写查询:

select * from (<your query>) SourceQuery

ie 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.您需要在 Power BI 中使用 M 和 DAX 实现查询逻辑,或者将其包装在数据库中的视图中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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