【问题标题】:Can I add a new column with Linear Interpolation in Power Query M?我可以在 Power Query M 中添加带有线性插值的新列吗?
【发布时间】:2021-11-13 17:43:46
【问题描述】:

我正在从期货市场价格中提取利率曲线,并在电力查询中创建一个表(表 1),其中包含以下列:

- BusinessDays: 表示从今天到每份未来合同到期的 nr o 个工作日

- InterestRate: 表示从今天到期货合约到期的利率

第二张表(表2)是指不同工作日到期的内部金融产品的ID。

- InstrumentID: 金融机构销售的金融产品的唯一内部 ID

- BusinessDays: 表示从今天到每个金融产品到期的 nr o 个工作日

我在使用 M 语言时遇到了一些问题,不幸的是,这个特定的计算必须在 Excel 中执行,所以我仅限于 Power Query M。

我无法做到的具体步骤是:

  • 在 power query 中创建一个函数,该函数在表 2 中添加一个新列,其中包含每个金融产品的插值利率。

我正在寻找的最终结果是这样的

【问题讨论】:

  • 你试过什么?你是如何进行插值的? (有些似乎与我得到的相符,有些则不然)。

标签: powerquery linear-interpolation


【解决方案1】:

有几种方法可以解决这个问题,但无论哪种方法,您都需要进行某种查找以确定与您的 BusinessDays 值匹配的括号,以便计算插值。

我认为生成一个包含天数与利率的全包列表,然后执行Join 来提取匹配项更简单。

命名第一个查询intRates 并扩展了利率表:

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

这会生成一个表格,其中包含每天(从 39 到 ,以及相应的利率。

然后读入“Instruments”表并使用 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"

表格中间部分的一些结果与您发布的结果不同,但似乎与两个值之间的线性插值公式一致,所以我不确定差异是如何产生的

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2023-03-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多