电源查询中的特殊 vlookup
special vlookup in power query
感谢您回答我们的问题。看下面的图片。将速率值从第一个范围适当地转移到第二个范围。
如果我理解正确,下面的代码应该可以满足您的要求。
阅读代码注释并探索应用步骤以更好地理解算法。
let
//Read in the lookup table
Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],
lookupTable = Table.TransformColumnTypes(Source,{
{"Date", Int64.Type},
{"P Code",Int64.Type},
{"SRC", Int64.Type},
{"Rate", Int64.Type}
}),
//read in data table
Source2 = Excel.CurrentWorkbook(){[Name="Table2"]}[Content],
typeIt2 = Table.TransformColumnTypes(Source2,{
{"Date", Int64.Type},
{"P.Code", Int64.Type},
{"Src", Int64.Type}
}),
//Join the two tables based on PCode and Src
join = Table.NestedJoin(typeIt2,{"P.Code","Src"},lookupTable,{"P Code","SRC"},"Joined", JoinKind.LeftOuter),
//for each joined subtable
// Sort descending by date
// Select only those rows where the date in table 2 is >= the corresponding date from table 1
// Then extract the first row Rate value (as that will be the closest to the date in table 2)
#"Added Custom" = Table.AddColumn(join, "Rate", each Table.SelectRows(Table.Sort([Joined],
{"Date",Order.Descending}),(t)=> t[Date] <= [Date])[Rate]{0}, Int64.Type),
//Remove unneeded join table column
#"Removed Columns" = Table.RemoveColumns(#"Added Custom",{"Joined"})
in
#"Removed Columns"
查找Table
结果
感谢您回答我们的问题。看下面的图片。将速率值从第一个范围适当地转移到第二个范围。
如果我理解正确,下面的代码应该可以满足您的要求。
阅读代码注释并探索应用步骤以更好地理解算法。
let
//Read in the lookup table
Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],
lookupTable = Table.TransformColumnTypes(Source,{
{"Date", Int64.Type},
{"P Code",Int64.Type},
{"SRC", Int64.Type},
{"Rate", Int64.Type}
}),
//read in data table
Source2 = Excel.CurrentWorkbook(){[Name="Table2"]}[Content],
typeIt2 = Table.TransformColumnTypes(Source2,{
{"Date", Int64.Type},
{"P.Code", Int64.Type},
{"Src", Int64.Type}
}),
//Join the two tables based on PCode and Src
join = Table.NestedJoin(typeIt2,{"P.Code","Src"},lookupTable,{"P Code","SRC"},"Joined", JoinKind.LeftOuter),
//for each joined subtable
// Sort descending by date
// Select only those rows where the date in table 2 is >= the corresponding date from table 1
// Then extract the first row Rate value (as that will be the closest to the date in table 2)
#"Added Custom" = Table.AddColumn(join, "Rate", each Table.SelectRows(Table.Sort([Joined],
{"Date",Order.Descending}),(t)=> t[Date] <= [Date])[Rate]{0}, Int64.Type),
//Remove unneeded join table column
#"Removed Columns" = Table.RemoveColumns(#"Added Custom",{"Joined"})
in
#"Removed Columns"
查找Table
结果