Skip to main content
cancel
Showing results for 
Search instead for 
Did you mean: 

The Power BI Data Visualization World Championships is back! Get ahead of the game and start preparing now! Learn more

Reply
hfyjchkl
Microsoft Employee
Microsoft Employee

get a average of sum in the past 30 days

here is a table like

date,       count

02-12-11  1

02-12-11  1

02-12-11  1

02-12-11  1

02-12-12 1

02-12-13 1

 

so we can aggregate it likes

02-12-11  4

02-12-12 1

02-12-13 1

 

now i want to know the average of last 2 day's sum should be

(4+1)/2 = 2.5 for the 02-12-12

(1+1)/2 = 1  for the 02-12-13

 

so can i get a measure to get the average of the sum in the past 30 days

 

 

1 ACCEPTED SOLUTION

@hfyjchkl 

Maybe you have more dates than selected in the chart. Not sure if you have any other filters. You can try the following code with and with the REMOVEFILTERS part

 

30 Days Moving Average =
VAR FisrtVisibleDate =
    CALCULATE ( MIN ( Sheet1[date] ), ALLSELECTED ( Sheet1 ) )
VAR CurrentDate =
    MAX ( Sheet1[date] )
VAR Result =
    CALCULATE (
        SUM ( Sheet1[count] ),
        Sheet1[date] > FisrtVisibleDate,
        Sheet1[date] <= CurrentDate,
        Sheet1[date] >= CurrentDate - 29
    )
RETURN
    DIVIDE ( Result, 30 )

 

View solution in original post

9 REPLIES 9
v-xiaotang
Community Support
Community Support

Hi  @hfyjchkl 

Thanks for reaching out to us.

please try this measure,

Measure = 
var _day=30
var _start=MIN('Table'[date])-30
var _sum=CALCULATE(SUM('Table'[count]),FILTER(ALL('Table'),'Table'[date]>=_start && 'Table'[date]<MIN('Table'[date])))
return DIVIDE(_sum,_day)

Then you can use both types of visual,

vxiaotang_0-1661740482711.png

vxiaotang_1-1661740542862.png

 

Best Regards,

Community Support Team _Tang

If this post helps, please consider Accept it as the solution to help the other members find it more quickly.

@v-xiaotang can we avoid the front 30 day's gap. we don't have enought data to support divide 30 in the front of 30 day, So, how could we do it like, divide the day of number until the day to be 30 day. or cut the front 30 day's graph

hfyjchkl
Microsoft Employee
Microsoft Employee

let me calrify more the requestment; this is original table.

date,       count

02-12-11  1

02-12-11  1

02-12-11  1

02-12-11  1

02-12-12 1

02-12-13 1
i use the sum for past 30 day.

hfyjchkl_0-1661359092564.png

now i hope could have the 30d moving average. likes below:

hfyjchkl_1-1661359149258.png

i want to draw a red dotted line for the first picture

Hi @hfyjchkl 

please try changing the code 1 to 29 and 2 to 30

if it did not work please share a relevant sample of set of row data that I can work with. 

hfyjchkl
Microsoft Employee
Microsoft Employee

THAT'S WHAT I DO:

hfyjchkl_0-1661362191702.png

LOOKS IT IS NOT RIGHT

@hfyjchkl 

What is the code of the measure used in this chart?

84797B32-D0CB-422E-88C3-37AD794A775D.png

hfyjchkl
Microsoft Employee
Microsoft Employee

hfyjchkl_0-1661364423339.png

it just sum,

hfyjchkl_1-1661364504186.png

 

@hfyjchkl 

Maybe you have more dates than selected in the chart. Not sure if you have any other filters. You can try the following code with and with the REMOVEFILTERS part

 

30 Days Moving Average =
VAR FisrtVisibleDate =
    CALCULATE ( MIN ( Sheet1[date] ), ALLSELECTED ( Sheet1 ) )
VAR CurrentDate =
    MAX ( Sheet1[date] )
VAR Result =
    CALCULATE (
        SUM ( Sheet1[count] ),
        Sheet1[date] > FisrtVisibleDate,
        Sheet1[date] <= CurrentDate,
        Sheet1[date] >= CurrentDate - 29
    )
RETURN
    DIVIDE ( Result, 30 )

 

tamerj1
Super User
Super User

Hi @hfyjchkl 
Please try

1.png

2 Days Moving Average = 
VAR FisrtVisibleDate = CALCULATE ( MIN ( Sheet1[date] ), ALLSELECTED ( Sheet1 ) )
VAR CurrentDate = MAX ( Sheet1[date] )
VAR Result =
    IF ( 
        CurrentDate <> FisrtVisibleDate,
        CALCULATE ( 
            SUM ( Sheet1[count] ),
            Sheet1[date] <= CurrentDate,
            Sheet1[date] >= CurrentDate - 1,
            REMOVEFILTERS ( Sheet1 )
        )
    )
RETURN
    DIVIDE ( Result, 2 )

Helpful resources

Announcements
Power BI DataViz World Championships

Power BI Dataviz World Championships

The Power BI Data Visualization World Championships is back! Get ahead of the game and start preparing now!

December 2025 Power BI Update Carousel

Power BI Monthly Update - December 2025

Check out the December 2025 Power BI Holiday Recap!

FabCon Atlanta 2026 carousel

FabCon Atlanta 2026

Join us at FabCon Atlanta, March 16-20, for the ultimate Fabric, Power BI, AI and SQL community-led event. Save $200 with code FABCOMM.

Users online (5,828)