Power BI: how to merge specific tables

空扰寡人 提交于 2021-01-01 14:34:51

问题


I have Table A with data of specific people doing their tasks like this:

I have Table B with data of needs for specific people for different periods of time like this:

I also have additional table C with period definitions:

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:

  1. Someone in Table A can have Start and End dates spanning multiple periods, like for example 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
  • if this is between 6 and 14 days, than it's 0.5 period
  • if it's more than 14 days, than it's 1 period

So now I want to merge Table A with Table B, to compare needs with what was delivered, for every period. The question is how to go with this? 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? Also how to implement this conditional logic for periods?


回答1:


I think this works

Pull in Period definitions as Table1

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

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

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

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"


来源:https://stackoverflow.com/questions/65314541/power-bi-how-to-merge-specific-tables

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!