簡體   English   中英

計算 Power BI 中兩行之間的差異

[英]Calculate difference between two rows in Power BI

我正在嘗試計算兩行之間累積費用之間的差異。

Net expenditure for the month = 

var diff= 
'Source data'[Cummulative expense for the month]- 
CALCULATE(
    SUM('Source data'[Cummulative expense for the month]),
    FILTER(
        'Source data',
        'Source data'[index]=EARLIER('Source data'[Numerator])
    )
) 

return 
IF(
    diff=VALUE('Source data'[Cummulative expense for the month]),
    0,
    diff
)

不知道是什么錯誤。 我還附上了數據的截圖在此處輸入圖片說明

您可以使用電源查詢來實現此目的

let
    Source = Excel.Workbook(File.Contents("C:\Users\anumua2\Downloads\im_08nov.xlsx"), null, true),
    Sheet1_Sheet = Source{[Item="Sheet1",Kind="Sheet"]}[Data],
    #"Promoted Headers" = Table.PromoteHeaders(Sheet1_Sheet, [PromoteAllScalars=true]),
    #"Changed Type" = Table.TransformColumnTypes(#"Promoted Headers",{{"Cumulative Expense for month", type number}}),
    #"Added Index" = Table.AddIndexColumn(#"Changed Type", "Index", 1, 1, Int64.Type),
    #"Prev" = Table.AddColumn(#"Added Index", "Custom", each #"Added Index"{[Index]-2}[#"Cumulative Expense for month"]),
    #"Changed Type1" = Table.TransformColumnTypes(Prev,{{"Custom", type number}}),
    #"Replaced Errors" = Table.ReplaceErrorValues(#"Changed Type1", {{"Custom", 0}}),
    #"Inserted Subtraction" = Table.AddColumn(#"Replaced Errors", "Subtraction", each [Cumulative Expense for month] - [Custom], type number),
    #"Renamed Columns" = Table.RenameColumns(#"Inserted Subtraction",{{"Custom", "Prev row"}, {"Subtraction", "Net Expenditure for month"}})
in
    #"Renamed Columns"

在此處輸入圖片說明

你能試試下面的東西嗎?

Net expenditure for the month = 

var current_row_index = min('Source data'[index])
var current_row_cummulative_expense = min('Source data'[Cummulative expense for the month])

var diff= 
current_row_cummulative_expense - 
CALCULATE(
    SUM('Source data'[Cummulative expense for the month]),
    FILTER(
        ALL('Source data'),
        'Source data'[index] = current_row_index - 1
    )
) 

return 
IF(
    diff=VALUE(min('Source data'[Cummulative expense for the month])),
    0,
    diff
)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM