[英]How to add new column from calculation on two rows in power BI?
似乎Total Net Sales
總是排在Gross Margin
之前。
如果是這種情況,那么為了將一個值乘以前一行的同一列中的值,您需要添加一個索引列,或者,我的偏好是,因為它計算速度更快,添加一個“移位列”,其中前一行的值現在在同一行上。
let
Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("bZA/C4MwEMW/ijhLzWlyf0anTu2idBEHBykFacHY799LAqXELnnc8Xv3HhnHEkwNNVBZlZ33y14M7+2pw8kIR0FKE5VT9UsPr31ei6s6+nldvG6IBVXQIQegbZx1mem8vbwvLvN2f8QQaDCKiSEAeUh/68JhxxQ4JomX9eUv2fwrDwYCL2hMiuEMP7YPcMhCS6JqWxaTmQ7tQZLY+EWMGZ/a62EJHECb6oNzjrTQ9AE=", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Date = _t, GL = _t, #"Domestic Product" = _t, #"Total India Market" = _t, Export = _t]),
#"Changed Type" = Table.TransformColumnTypes(Source,{{"Date", type date}, {"GL", type text},
{"Domestic Product", type number}, {"Total India Market", type number}, {"Export", type number}}),
//add a column = offset column from Total India Market
//This will calculate faster than using an INDEX column
offsetTotalIndiaMarket =
let
tbl = #"Changed Type",
ShiftedList = {null} & List.RemoveLastN(tbl[Total India Market]),
Custom1 = Table.ToColumns(tbl) & {ShiftedList}
in
Table.FromColumns(Custom1, Table.ColumnNames(tbl) & {"Shifted Tot India Market"}),
//add custom column to execute the described calculation
#"Added Custom" = Table.AddColumn(offsetTotalIndiaMarket, "Total", each
if [GL] = "Gross Margin"
then [Total India Market]*[Shifted Tot India Market]
else [Total India Market]+[Export]),
//Remove shifted column
#"Removed Columns" = Table.RemoveColumns(#"Added Custom",{"Shifted Tot India Market"})
in
#"Removed Columns"
結果
如果不是的情況下Total Net Sales
始終是前行Gross Margin
,那么我們就可以使用不同的算法。 但還必須知道Gross Margin
是否總是有一個之前的Total Net Sales
,或者我們是否也必須考慮無法計算。
如果是這種情況,請對此答案發表評論
您可以使用電源查詢來實現此目的
首先添加一個索引列,然后計算 Total India Market 的前一行,然后添加自定義列以根據要求計算總計
let
Source = Excel.Workbook(File.Contents("C:\Users\anumua2\Downloads\im_25_oct_21.xlsx"), null, true),
Data_Sheet = Source{[Item="Data",Kind="Sheet"]}[Data],
#"Promoted Headers" = Table.PromoteHeaders(Data_Sheet, [PromoteAllScalars=true]),
#"Changed Type" = Table.TransformColumnTypes(#"Promoted Headers",{{"Date", type date}, {"GL", type text}, {"Domestic Product", type number}, {"Total India Market", type number}, {"Export", type number}}),
#"Added Index" = Table.AddIndexColumn(#"Changed Type", "Index", 1, 1, Int64.Type),
#"Prev" = Table.AddColumn(#"Added Index", "Custom", each #"Added Index"{[Index]-2}[#"Total India Market"]),
#"Renamed Columns" = Table.RenameColumns(Prev,{{"Custom", "Prev_Total India Market"}}),
#"Replaced Errors" = Table.ReplaceErrorValues(#"Renamed Columns", {{"Prev_Total India Market", 0}}),
#"Added Custom" = Table.AddColumn(#"Replaced Errors", "Custom", each if[GL] = "Gross Margin" then [Total India Market]*[Prev_Total India Market] else [Total India Market] + [Export]),
#"Renamed Columns1" = Table.RenameColumns(#"Added Custom",{{"Custom", "Total"}})
in
#"Renamed Columns1"
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.