我可以在 Power Query M 中添加一个带有线性插值的新列吗?
Can I add a new column with Linear Interpolation in Power Query M?
我正致力于从期货市场价格中提取利率曲线,并创建一个 table (Table 1) 内部幂查询,其中包含以下列:
- BusinessDays: 表示从今天到每个期货合约到期的nr o 个工作日
- InterestRate: 表示从今天到期货合约到期的利率
第二个table(table2)指不同工作日到期的内部理财产品ID
- InstrumentID: 金融机构销售的金融产品的唯一内部ID
- BusinessDays: 表示从今天到每个金融产品到期的nr o 个工作日
我在使用 M 语言时遇到了一些问题,不幸的是这个特定的计算必须在 Excel 中执行,所以我只能使用 Power Query M。
我做不到的具体步骤是:
- 在 power query 中创建一个添加新列的函数 do table 2 包含每个金融产品的内插利率 os。
我正在寻找的最终结果如下所示
有几种方法可以解决这个问题,但无论哪种方式,您都需要进行某种查找以确定与您的 BusinessDays
值匹配的括号,这样您就可以计算内插值.
我认为更简单的做法是只生成一个包含所有天数与利率的列表,然后执行 Join
来找出匹配项。
我 Name 是第一个查询 intRates
并扩展了利率 table:
let
//Get the interest rate/business day table
Source = Excel.CurrentWorkbook(){[Name="intRates"]}[Content],
#"Changed Type" = Table.TransformColumnTypes(Source,{{"BusinessDays", Int64.Type}, {"InterestRate", Percentage.Type}}),
//Add two columns which are the interest rate and business day columns offset by one
//It is faster to subtract this way than by adding an Index column
offset=
Table.FromColumns(
Table.ToColumns(#"Changed Type")
& {List.RemoveFirstN(#"Changed Type"[BusinessDays]) & {null}}
& {(List.RemoveFirstN(#"Changed Type"[InterestRate])) & {null}},
type table[BusinessDays=Int64.Type, InterestRate=Percentage.Type, shifted BusDays=Int64.Type, shifted IntRate=Percentage.Type]),
//Add a column with a list of the interest rates for each data interpolated between the segments
#"Added Custom" = Table.AddColumn(offset, "IntList", each let
sbd=[shifted BusDays],
intRateIncrement = ([shifted IntRate]-[InterestRate])/([shifted BusDays]-[BusinessDays]),
Lists= List.Generate(
()=>[d=[BusinessDays],i=[InterestRate]],
each [d]< sbd,
each [d=[d]+1, i = [i]+intRateIncrement],
each [i])
in Lists),
//add another column with a list of days corresponding to the interest rates
#"Added Custom1" = Table.AddColumn(#"Added Custom", "dayList", each {[BusinessDays]..[shifted BusDays]-1}),
//remove the last row as it will have an error
remErrRow = Table.RemoveLastN(#"Added Custom1",1),
//create the new table which has the rates for every duration
intRateTable = Table.FromColumns(
{List.Combine(remErrRow[dayList]),List.Combine(remErrRow[IntList])},
type table[Days=Int64.Type, Interest=Percentage.Type])
in
intRateTable
这导致 table 每天(从 39 到 )都有相应的利率。
然后读入“Instruments”table 并使用 JoinKind.LeftOuter
将其与 intRates 相结合
let
Source = Excel.CurrentWorkbook(){[Name="Instruments"]}[Content],
#"Changed Type" = Table.TransformColumnTypes(Source,{{"InstrumentID", type text}, {"BusinessDays", Int64.Type}}),
//add the rate column
#"Merged Queries" = Table.NestedJoin(#"Changed Type", {"BusinessDays"}, intRates, {"Days"}, "intRates", JoinKind.LeftOuter),
#"Expanded intRates" = Table.ExpandTableColumn(#"Merged Queries", "intRates", {"Interest"}, {"Interest"})
in
#"Expanded intRates"
table中间的一些结果和你发的不一样,但是好像符合两个值之间的线性插值公式,所以我我不确定差异是如何产生的
我正致力于从期货市场价格中提取利率曲线,并创建一个 table (Table 1) 内部幂查询,其中包含以下列:
- BusinessDays: 表示从今天到每个期货合约到期的nr o 个工作日
- InterestRate: 表示从今天到期货合约到期的利率
第二个table(table2)指不同工作日到期的内部理财产品ID
- InstrumentID: 金融机构销售的金融产品的唯一内部ID
- BusinessDays: 表示从今天到每个金融产品到期的nr o 个工作日
我在使用 M 语言时遇到了一些问题,不幸的是这个特定的计算必须在 Excel 中执行,所以我只能使用 Power Query M。
我做不到的具体步骤是:
- 在 power query 中创建一个添加新列的函数 do table 2 包含每个金融产品的内插利率 os。
我正在寻找的最终结果如下所示
有几种方法可以解决这个问题,但无论哪种方式,您都需要进行某种查找以确定与您的 BusinessDays
值匹配的括号,这样您就可以计算内插值.
我认为更简单的做法是只生成一个包含所有天数与利率的列表,然后执行 Join
来找出匹配项。
我 Name 是第一个查询 intRates
并扩展了利率 table:
let
//Get the interest rate/business day table
Source = Excel.CurrentWorkbook(){[Name="intRates"]}[Content],
#"Changed Type" = Table.TransformColumnTypes(Source,{{"BusinessDays", Int64.Type}, {"InterestRate", Percentage.Type}}),
//Add two columns which are the interest rate and business day columns offset by one
//It is faster to subtract this way than by adding an Index column
offset=
Table.FromColumns(
Table.ToColumns(#"Changed Type")
& {List.RemoveFirstN(#"Changed Type"[BusinessDays]) & {null}}
& {(List.RemoveFirstN(#"Changed Type"[InterestRate])) & {null}},
type table[BusinessDays=Int64.Type, InterestRate=Percentage.Type, shifted BusDays=Int64.Type, shifted IntRate=Percentage.Type]),
//Add a column with a list of the interest rates for each data interpolated between the segments
#"Added Custom" = Table.AddColumn(offset, "IntList", each let
sbd=[shifted BusDays],
intRateIncrement = ([shifted IntRate]-[InterestRate])/([shifted BusDays]-[BusinessDays]),
Lists= List.Generate(
()=>[d=[BusinessDays],i=[InterestRate]],
each [d]< sbd,
each [d=[d]+1, i = [i]+intRateIncrement],
each [i])
in Lists),
//add another column with a list of days corresponding to the interest rates
#"Added Custom1" = Table.AddColumn(#"Added Custom", "dayList", each {[BusinessDays]..[shifted BusDays]-1}),
//remove the last row as it will have an error
remErrRow = Table.RemoveLastN(#"Added Custom1",1),
//create the new table which has the rates for every duration
intRateTable = Table.FromColumns(
{List.Combine(remErrRow[dayList]),List.Combine(remErrRow[IntList])},
type table[Days=Int64.Type, Interest=Percentage.Type])
in
intRateTable
这导致 table 每天(从 39 到 )都有相应的利率。
然后读入“Instruments”table 并使用 JoinKind.LeftOuter
let
Source = Excel.CurrentWorkbook(){[Name="Instruments"]}[Content],
#"Changed Type" = Table.TransformColumnTypes(Source,{{"InstrumentID", type text}, {"BusinessDays", Int64.Type}}),
//add the rate column
#"Merged Queries" = Table.NestedJoin(#"Changed Type", {"BusinessDays"}, intRates, {"Days"}, "intRates", JoinKind.LeftOuter),
#"Expanded intRates" = Table.ExpandTableColumn(#"Merged Queries", "intRates", {"Interest"}, {"Interest"})
in
#"Expanded intRates"
table中间的一些结果和你发的不一样,但是好像符合两个值之间的线性插值公式,所以我我不确定差异是如何产生的