Excel Power Query. Different Formulas for Each Row of Values from Other Rows

时间秒杀一切 提交于 2021-02-10 14:41:00

问题


I have a table 1 of 500+ rows that are updated multiple times during a month by a request. And there is a updated table of different formulas (200+) that should be calculated using values from table 1.

Table 1

year product value
1993 Apple 98.45
1994 Mushrooms 67.54
1992 Apple 95.45
2021 Melon 112.0

Table 2

id formula
1 1994/1993*100-100
2 1994-1993
3 2021/1992*100-100

etc...

My way:

  1. in table_1 I have created a key column - year.
  2. I have modified the formula (below - a pic):

table_2

Code for table_2:

   let
        src = Excel.CurrentWorkbook(){[Name="table_2"]}[Content],
        pq_auto1 = Table.PromoteHeaders(src, [PromoteAllScalars=true]),
        pq_auto2 = Table.TransformColumnTypes(pq_auto1,{{"Id", Int64.Type}, {"formula", type text}}),
        result = Table.AddColumn(pq_auto2,"value",each Expression.Evaluate([modified_formula],#shared))
   in
        result

Can you suggest me to find a better way to do this calculations by each row?


回答1:


It is not possible to do that just like with Excel formula however BI Accountant showed elegant solution by shifting column by one row:

let
     Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content]
    ,ShiftedList = {null} & List.RemoveLastN(Table.Column(Source, "ColumnToBeShifted"), 1)
    ,NewShiftedColumn = Table.FromColumns(Table.ToColumns(Source) & {ShiftedList}, Table.ColumnNames(Source) & {"NewColumnName"})
in
    NewShiftedColumn

With this your calculation should be very easy.



来源:https://stackoverflow.com/questions/66090912/excel-power-query-different-formulas-for-each-row-of-values-from-other-rows

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