在电源查询语言(M语言)中,我们如何添加自定义"值"和"表"手动列到表格?

时间:2016-04-28 10:10:47

标签: powerbi powerquery

在电源查询中,如果我们从sql数据库获取数据," Value"和"表"如果数据库中存在关系,则会自动创建列。 enter image description here

AFAIK"表"和"价值"分别表示一对多和多对一关系。

我的问题是我们的数据库中没有任何关系。因此PowerQuery无法自动生成这些列。 如果我知道主题表之间的关系,我该如何手动添加这些列?

我找到了返回Table对象的Table.NestedJoin函数(但性能很低,即使数据库中存在关系。)

但我找不到任何返回Value对象的函数(另一个表的记录)。

可能存在缺陷的其他解决方案;

  1. 您可能会建议我在数据库中获取表格,并在Power BI中的“关系”部分(或Excel中的电源数据透视部分)中创建关系。但是我在power查询中需要这个Value对象,因为我想在加载表的所有行之前根据相关表过滤行。
  2. 创建一个连接表的本机查询,这不是我的首选。
  3. 创建Table对象而不是Value对象(我们确信只会有一条记录。)我仍然遇到Table.NestedJoin方法的性能问题。还有其他选择吗?
  4. 提前致谢...

1 个答案:

答案 0 :(得分:0)

Just today I had quite same issue with performance, but finally solved it. In my solution I work with views, but need to filter records coming.
When I use such a code:

let
    filter1 = 2016,
    filter2 = "SomeText",
    tbl = Sql.Database("MyServer","MyDB"){Schema="dbo",Item="MyTableOrView"}[Data],
    filteredTable = Table.SelectRows(tbl, each ([field1] = filter1) and ([field2] = filter2))
in
    filteredTable

it works slow. But if I try NestedJoin - it performs much better.

let
    Source = Table.FromColumns({{2016}, {"SomeText"}}, "filter1", "filter2"),
    tbl = Sql.Database("MyServer","MyDB"){Schema="dbo",Item="MyTableOrView"}[Data],
    filteredTable = Table.NestedJoin(tbl, {"field1", "field2"}, Source, {"filter1", "filter2"}, "NewColumn", JoinKind.Inner)
in
    filteredTable 

However, I noticed that even fastest design I got works slower than just a query that returns all ~~1300 rows from the view.

I have no SQL Profiler to track down what is exactly sent to the server, but it seems to me that query folding work when you use inner joins.

Try following: make 2 queries to 2 tables (no other actions!) and inner join them, then see if it works faster.