简体   繁体   English

Power BI:如何合并特定表

[英]Power BI: how to merge specific tables

I have Table A with data of specific people doing their tasks like this:我有表 A,其中包含执行任务的特定人员的数据,如下所示:

在此处输入图像描述

I have Table B with data of needs for specific people for different periods of time like this:我有表 B,其中包含特定人员在不同时期的需求数据,如下所示:

在此处输入图像描述

I also have additional table C with period definitions:我还有带有期间定义的附加表 C:

Period no  |  Date from | Date to
--------------------------------------
   1       | 27/01/2021 | 24/02/2021
   2       | 25/02/2021 | 24/03/2021
...

There are 2 problems here:这里有2个问题:

  1. Someone in Table A can have Start and End dates spanning multiple periods, like for example Human B表 A 中的某个人的开始日期和结束日期可以跨越多个时期,例如 Human B
  2. The Start and End dates may not encompass whole Periods, they can be for example just for a couple of days.开始日期和结束日期可能不包括整个期间,例如,它们可能只是几天。 And so there's an algorithm that calculates whether this counts as a period or not:所以有一个算法可以计算这是否算作一个时期:
  • if this is less than 5 days, than it doesn't count如果这少于 5 天,则不算在内
  • if this is between 6 and 14 days, than it's 0.5 period如果这在 6 到 14 天之间,则为 0.5 天
  • if it's more than 14 days, than it's 1 period如果超过 14 天,则为 1 期

So now I want to merge Table A with Table B, to compare needs with what was delivered, for every period.所以现在我想将表 A 与表 B 合并,以比较每个时期的需求和交付的内容。 The question is how to go with this?问题是如何用这个 go ? My first thought was to add columns to Table A for Period and Quantity, to be able to group & merge over it - but what about when this deployment can span over multiple periods?我的第一个想法是在表 A 中为 Period 和 Quantity 添加列,以便能够对其进行分组和合并 - 但是当这个部署可以跨越多个时期时呢? Also how to implement this conditional logic for periods?另外如何为周期实现这个条件逻辑?

I think this works我认为这行得通

Pull in Period definitions as Table1将期间定义拉入表 1

Add a custom column using formula使用公式添加自定义列

= {Number.From([Date from])..Number.From([Date to])}

And then expand that to rows.然后将其扩展到行。 That gives you a match for every date to every period这使您可以匹配每个日期到每个时期

在此处输入图像描述

File.. Close and Load... Connection文件..关闭并加载...连接

Full sample code for that part is:该部分的完整示例代码是:

let Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],
#"Changed Type" = Table.TransformColumnTypes(Source,{{"Period no", Int64.Type}, {"Date from", type date}, {"Date to", type date}}),
#"Added Custom" = Table.AddColumn(#"Changed Type", "Custom", each {Number.From([Date from])..Number.From([Date to])}),
#"Expanded Custom" = Table.ExpandListColumn(#"Added Custom", "Custom"),
#"Removed Columns" = Table.RemoveColumns(#"Expanded Custom",{"Date from", "Date to"})
in #"Removed Columns"

Pull in your TableA, called Table2 here拉入您的 TableA,此处称为 Table2

Add custom column with similar formula and expand to rows添加具有类似公式的自定义列并扩展到行

= {Number.From([Start of Deployment]) .. Number.From([End of Deployment])}

Now merge the other table into this one and pull in period现在将另一个表合并到这个表中并拉入期间

Click select the type and period columns and group them, pulling in the maximum and minimum dates from the new custom column单击 select 类型和周期列并将它们分组,从新的自定义列中提取最大和最小日期

在此处输入图像描述

Add custom column for working duration with formula使用公式为工作持续时间添加自定义列

= 1+[DayMax]-[DayMin]

Then add a custom column to apply your algo然后添加一个自定义列以应用您的算法

=  if [Duration]<6 then 0 else if [Duration] <15 then 0.5 else 1

Remove extra columns.删除多余的列。 Done.完毕。 File... Close and Load... Connection文件...关闭并加载...连接

You can merge this back into your Table B as needed您可以根据需要将其合并回您的表 B

在此处输入图像描述

Full code for this table此表的完整代码

let Source = Excel.CurrentWorkbook(){[Name="Table2"]}[Content],
#"Changed Type" = Table.TransformColumnTypes(Source,{{"Type", type text}, {"Start of Deployment", type date}, {"End of Deployment", type date}}),
#"Added Custom" = Table.AddColumn(#"Changed Type", "Custom", each {Number.From([Start of Deployment]) .. Number.From([End of Deployment])}),
#"Expanded Custom" = Table.ExpandListColumn(#"Added Custom", "Custom"),
#"Removed Columns" = Table.RemoveColumns(#"Expanded Custom",{"Start of Deployment", "End of Deployment"}),
#"Merged Queries" = Table.NestedJoin(#"Removed Columns",{"Custom"},Table1,{"Custom"},"Table1",JoinKind.LeftOuter),
#"Expanded Table1" = Table.ExpandTableColumn(#"Merged Queries", "Table1", {"Period no"}, {"Period no"}),
#"Grouped Rows" = Table.Group(#"Expanded Table1", {"Type", "Period no"}, {{"DayMin", each List.Min([Custom]), type number}, {"DayMax", each List.Max([Custom]), type number}}),
#"Added Custom1" = Table.AddColumn(#"Grouped Rows", "Duration", each 1+[DayMax]-[DayMin]),
#"Added Custom2" = Table.AddColumn(#"Added Custom1", "Algo", each if [Duration]<6 then 0 else if [Duration] <15 then 0.5 else 1),
#"Removed Columns1" = Table.RemoveColumns(#"Added Custom2",{"DayMin", "DayMax", "Duration"})
in #"Removed Columns1"

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

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