Power BI 中客户级别的当前月/月差异

Difference current month / month a year back on customer level in Power BI

我正在尝试计算给定月份与一年前同月(例如 2021 年 1 月 - 2020 年 1 月)之间的销量差异。棘手的部分是它应该在客户层面完成。我的 table 现在看起来像这样:

Date Customer_number Sales_Volume
14.01.2020 1111 1000
13.01.2020 2222 2000
21.02.2020 1111 4000
17.02.2020 2222 5000
22.01.2021 1111 1500
24.01.2021 2222 2600
19.02.2021 1111 4700
18.02.2021 2222 5800

我的输出应该是这样的:

Date Customer_number Sales_Volume Volume_Difference
14.01.2020 1111 1000 0
13.01.2020 2222 2000 0
21.02.2020 1111 4000 0
17.02.2020 2222 5000 0
22.01.2021 1111 1500 500
24.01.2021 2222 2600 600
19.02.2021 1111 4700 700
18.02.2021 2222 5800 800

试试下面这个 措施代码-

your_value = 

var current_row_year = YEAR(MIN(your_table_name[Date]))
var current_row_month = MONTH(MIN(your_table_name[Date]))
var current_row_customer_number = MIN(your_table_name[Customer_number])
var current_row_sales_value = MIN(your_table_name[Sales_Volume])

var last_year_sales_volumne = 
CALCULATE(
    SUM(your_table_name[Sales_Volume]),
    FILTER(
        ALL(your_table_name),
        YEAR(your_table_name[Date]) = current_row_year - 1
        && MONTH(your_table_name[Date]) = current_row_month
        && your_table_name[Customer_number] = current_row_customer_number
    )
) + 0

RETURN IF(
    last_year_sales_volumne = 0,
    0,
    current_row_sales_value - last_year_sales_volumne
)

这是输出-

这是计算列:

Column = 
VAR _y = YEAR([Date])
VAR _m = MONTH([Date])
VAR _c = [Customer_number]
VAR _tbl_prev = FILTER(ALL('Table'), [Customer_number] = _c && MONTH([Date]) = _m && YEAR([Date]) = _y - 1)
VAR _tbl_curr = FILTER(ALL('Table'), [Customer_number] = _c && MONTH([Date]) = _m && YEAR([Date]) = _y)
VAR _result = SUMX(_tbl_curr, [Sales_Volume]) - SUMX(_tbl_prev, [Sales_Volume])
RETURN IF(COUNTROWS(_tbl_prev), _result, 0)