简体   繁体   中英

DAX Rolling Average based on count of number of rows per day

I have a table (TableA), one of the columns is called DateAdded which is a Date timestamp, the table has multiple rows entered into it per day. In PowerBI, I have set this table up with a relationship to a date table (Dates). If I add TableA to a simple list view and place a date range slicer on the page, all works as expected (TableA gets filtered down based on date selection) - this proves the relationship works.

What I would like to do is add a rolling average in a chart based off the number of rows added per day.

TableA

Date...............| Product...............


2021-07-10 | Reference 1
2021-07-10 | Reference 2
2021-07-10 | Reference 3
2021-07-11 | Reference 4
2021-07-11 | Reference 5
2021-07-12 | Reference 6
2021-07-13 | Reference 7
2021-07-13 | Reference 8
2021-07-13 | Reference 9
2021-07-13 | Reference 10

The expected output is

Date...............| Rolling Average...............


2021-07-10 | 3
2021-07-11 | 2.5
2021-07-12 | 2
2021-07-13 | 2.5

Here is my current DAX, but it is returning exactly the same as the regular count, as opposed to a rolling average.

Rolling Average = 
  AVERAGEX(
   FILTER( ALLSELECTED(Dates),
      Dates[Date] <= MAX(Dates[Date])),
      COUNT('TableA'[Product])
)

Where am I going wrong?

Thanks.

I assumed you have a DM like following DM

解决方案

_date = MAX(_date[date])
_Count = COUNTROWS(_fact)
_rollingCount = 
VAR _1 = MAX(_date[date])
VAR _2 = CALCULATE(CALCULATE([_Count], FILTER(_date,_date[date]<=_1)),ALL(_date[date]))
RETURN _2
_rank = RANKX(ALLSELECTED(_date),[_date],,ASC)
_rollingAverage = DIVIDE([_rollingCount],[_rank])

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM