在 Power BI 中使用 DAX 计算运行平均值 - 逐行

时间:2021-05-10 04:29:44

标签: dax powerbi-desktop

我正在做类似的事情:Calculate Running Average Using DAX in Power BI

唯一的区别如下:

  • 我现在使用日期表,而不是使用非日期表中日期列中的列。
  • 这为我提供了总列中的运行平均值,但我希望它逐行显示。

期望的输出:

date             sales_amount       desired_running_average
04/01/2021       100.00             100.00
04/02/2021       300.00             200.00
04/05/2021       500.00             300.00

目前在做什么:

date             sales_amount       desired_running_average
04/01/2021       100.00             100.00
04/02/2021       300.00             300.00
04/05/2021       500.00             500.00

Total desired_running_average: 300.00 
I'm not sure why my code gives it to me on the total instead of row by row.

我有以下代码:

running_average_code_I_am_using = 

VAR FirstVisibleDate = CALCULATE(MIN('d_Date'[Date]),ALLSELECTED('d_Date'[Date]))
    
VAR LastVisibleDate = CALCULATE(MAX('d_Date'[Date]),ALLSELECTED('d_Date'[Date]))

VAR LastDateWithSales = 
    CALCULATE(
    CALCULATE(MAX(Table_1[date]),ALLSELECTED(Table_1[date]))
    ,REMOVEFILTERS()
    )

VAR Result = 
    IF (
        FirstVisibleDate <= LastDateWithSales,
        CALCULATE(
            AVERAGE(Table_1[sales_amount])
            ,Table_1[date] <= LastVisibleDate
        )
                
    )
    

RETURN
    Result

0 个答案:

没有答案