问题
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:
- in table_1 I have created a key column - year.
- I have modified the formula (below - a pic):
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