简体   繁体   English

Power Query / Power BI 从另一个 Excel 工作簿中查找数据

[英]Power Query / Power BI get look for data from another excel workbook

I am trying to combine worksheets from two different workbooks with Power Query and I have trouble doing that.我正在尝试将来自两个不同工作簿的工作表与 Power Query 结合起来,但我在这样做时遇到了麻烦。

I do not want to merge the two workbooks.我不想合并两个工作簿。

I do not want to create relationships or "joints".我不想建立关系或“关节”。

However, I want to get very specific information for one workbook which has only one column.但是,我想获得一本只有一列的工作簿的非常具体的信息。 The "ID" column. “ID”列。

The ID column has rows with letter tags : AB or BE. ID 列包含带有字母标签的行:AB 或 BE。 Following these letters, sepcific numeric ranges are associated.在这些字母之后,关联了特定的数字范围。 For both AB and BE, number ranges first from 0000 to 3000 and from 3000 to 6000.对于 AB 和 BE,数字范围首先从 0000 到 3000 和从 3000 到 6000。

I thus have the following possibilities: From AB0000 to AB3000 From AB3001 to AB6000 From BE0000 to BE3000 From BE3001 to AB6000因此,我有以下可能性: 从 AB0000 到 AB3000 从 AB3001 到 AB6000 从 BE0000 到 BE3000 从 BE3001 到 AB6000

Each category match to the a specific item in my column geography, from the other workbook: From AB0000 to AB3000, it is ItalyZ From AB3001 to AB6000, it is ItalyB From BE0000 to BE3000, it is UKY From BE3001 to AB6000, it is UKM每个类别都与我的专栏地理中的特定项目匹配,来自另一个工作簿: 从 AB0000 到 AB3000,它是 ItalyZ 从 AB3001 到 AB6000,它是意大利 B 从 BE0000 到 BE3000,它是 UKY 从 BE3001 到 AB6000,它是 UKM

I am thus trying to find the highest number associated to the first AB category, the second AB category, the first BE category, and the second.因此,我试图找到与第一个 AB 类别、第二个 AB 类别、第一个 BE 类别和第二个相关联的最高数字。

I then want to "bring" this number in the other query and increment it each time that matching country is found in the other workbook.然后我想在另一个查询中“带来”这个数字,并在每次在另一个工作簿中找到匹配的国家时增加它。 For example :例如 :

AB356 is the highest number in the first workbook. AB356 是第一个工作簿中的最高数字。

Once the first "ItalyB" is found, the column besides writes "AB357".一旦找到第一个“ItalyB”,该列除了写“AB357”。 Once the second is "ItalyB" is found, the column besides write "AB358".一旦找到第二个是“ItalyB”,除了写“AB358”之外的列。

Here is the one columned worksheet:这是单列工作表: ![输入

Here is the other worksheet with the various countries in geography:这是地理上不同国家的另一个工作表: 输入

Here is an example of results:下面是一个结果示例: 结果

have one column (geography) with I think that this is something which I should work towards:有一个专栏(地理),我认为这是我应该努力的事情:

I added the index column, with a start as one, because each row (even row zero) should increment either of the four matching code.我添加了索引列,以一开始,因为每一行(甚至零行)都应该增加四个匹配代码中的任何一个。 在此处输入图片说明

In order to keep moving forward I have also been trying to create some sort of mapping in third excel sheet, that I imported in Power BI, but I am not sure that this is a good way forward:为了继续前进,我也一直在尝试在第三个 Excel 表中创建某种映射,我在 Power BI 中导入了它,但我不确定这是一个很好的前进方式:

映射

I have the following result when I create a blank query:当我创建一个空白查询时,我有以下结果: 在此处输入图片说明

After a correction, I still get this result when creating the blank query:更正后,我在创建空白查询时仍然得到这个结果: 在此处输入图片说明

This is not an easy answer as there are many steps to get to your result.这不是一个简单的答案,因为要获得结果有很多步骤。 I have choosen for m-query because of the complexity.由于复杂性,我选择了 m-query。

In PBi click on Transform data, now you are in m-query.在 PBi 点击 Transform data,现在你在 m-query 中。

  1. The table with the ID's (I called it "HighestID") needs expansion because we need to be able to map on prefix带有 ID 的表(我称之为“HighestID”)需要扩展,因为我们需要能够映射前缀
  2. You need a mapping table ("GeoMapping"), else there is no relation between the Prefixes and the geolocation.您需要一个映射表(“GeoMapping”),否则前缀和地理位置之间没有关系。
  3. We need the newID on the Geo-table (which I called "Geo").我们需要地理表(我称之为“地理”)上的 newID。

Expand the HighestID table.展开 HighestID 表。

Click on the table and open the Advanced Editor, look at your code and compare it to the one below, the last 2 steps are essential, there I add two columns (Prefix and Number) which we need later.单击表格并打开高级编辑器,查看您的代码并将其与下面的代码进行比较,最后两步是必不可少的,在那里我添加了我们稍后需要的两列(前缀和数字)。

let
    Source = Csv.Document(File.Contents("...\HighestID.csv"),[Delimiter=";", Columns=1, Encoding=1252, QuoteStyle=QuoteStyle.None]),
    #"Promoted Headers" = Table.PromoteHeaders(Source, [PromoteAllScalars=true]),
    #"Changed Type1" = Table.TransformColumnTypes(#"Promoted Headers",{{"ID", type text}}),
    #"Added Custom" = Table.AddColumn(#"Changed Type1", "Prefix", each Text.Middle([ID],0,2), type text),
    #"Added Custom1" = Table.AddColumn(#"Added Custom", "Number", each Number.FromText(Text.Middle([ID],2,5)))
in
    #"Added Custom1"

Result:结果: 在此处输入图片说明

Create mapping table创建映射表

Click right button under your last table and click Blank Query:单击最后一个表下的右键,然后单击空白查询: 在此处输入图片说明

Paste the source below, ensure the name of the merg table equals the name of your table.粘贴下面的源代码,确保合并表的名称等于您的表的名称。 As I mentioned, I called it HighestID.正如我提到的,我称它为 HighestID。

let
    Source = #table({"Prefix", "Seq_Start", "Seq_End","GeoLocation"},{{"AB",0,2999,"ItalyZ"},{"AB",3000,6000,"ItalyB"},{"BC",0,299,"UKY"},{"BC",3000,6000,"UKM"}}),
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"Seq_Start", Int64.Type}, {"Seq_End", Int64.Type}}),
    #"Merged Queries" = Table.NestedJoin(#"Changed Type", {"Prefix"}, HighestID, {"Prefix"}, "HighestID", JoinKind.LeftOuter),
    #"Expanded HighestID" = Table.ExpandTableColumn(#"Merged Queries", "HighestID", {"Number"}, {"Number"}),
    #"Filtered Rows" = Table.SelectRows(#"Expanded HighestID", each [Number] >= [Seq_Start] and [Number] <= [Seq_End]),
    #"Grouped Rows" = Table.Group(#"Filtered Rows", {"Prefix", "Seq_Start", "Seq_End", "GeoLocation"}, {{"NextSeq", each List.Max([Number]) + 1, type number}})
in
    #"Grouped Rows"

Result:结果: 在此处输入图片说明

Adding the NextSeq Column添加 NextSeq 列

This is the hard bit because when I would only give you teh code, I am afraid it will not work so I give you the steps you need to do.这是困难的一点,因为当我只给你代码时,我担心它不起作用所以我给你需要做的步骤。

1.Select the table, right click on Geography and click Group by. 1.选择表格,右键单击地理并单击分组依据。 select as below:选择如下: 在此处输入图片说明

  1. Merge with table Geomapping as below:与表地理映射合并如下: 在此处输入图片说明

  2. Expand the GeoMapping with NextSeq使用 NextSeq 扩展 GeoMapping 在此处输入图片说明

  3. Add a custom column:添加自定义列: 在此处输入图片说明

  4. Remove columns not needed so only custom is left created in step 4.删除不需要的列,以便在步骤 4 中只创建自定义。

  5. Expand the column (all select).展开列(全选)。 End result all your columns you had earlier plus an Index column.最终结果是您之前拥有的所有列以及一个索引列。

在此处输入图片说明

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

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