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

Score big with last-minute savings on the final tickets to FabCon Vienna. Secure your discount

Reply
zacharylwy
Frequent Visitor

21-day Moving Average on a Measure

I am trying to create a 21-day moving average on a measure called 'Total Voy Intake'.
I have a data set of export volumes called 'guinea'.
The main table has the structure:

Load DateLoad PortVoy IntakeVessel NameVessel DWTCommodityVessel Type


Each row in the data set represents a vessel loading, so if there are 10 vessels on 1st Jan 2020, then there are 10 rows with 'Load Date' as 01-Jan-2020. Some days, there are 0 loadings but my program auto-fills these dates with a row that has 0 for 'Voy Intake'.
A sample of a 0 row is:

2019-11-03Port Kamsar0.0Unknown0.0n/aUnknown


I created a measure called 'Total Voy Intake' and I use this:

 

 

 

Guinea Total Voy Intake = 
COALESCE(
    SUM(Guinea[Voy Intake]),
    0
)

 

 

 

I think this helps me sum up the total voy intake for each day. (Please correct me if wrong)

'Load Date' has a relationship to the 'Date' column in my DateTable:

zacharylwy_0-1737023258334.png

I then want to create a 21-day moving average on the Total Voy Intake.

 

 

 

Guinea MA = 
AVERAGEX(
        DATESINPERIOD(
            DateTable[Date],
            MAX(DateTable[Date]),
            -21,
            DAY
        ),
        COALESCE([Guinea Total Voy Intake], 0)  -- Treat blank data as 0
    )

 

It is calculating right for every row but whenever the Total Voy Intake row is 0, the calculation goes wrong.
Here is a screengrab of what I am seeing now, with the right-most column to be what I EXPECT TO SEE:

zacharylwy_0-1737022830393.png

For some reason, whenever Total Voy Intake is 0, the 21day-MA calculates to the same value of 205,380.
Why does this happen and what can I change to get a proper Moving Average on a Measure?

1 ACCEPTED SOLUTION
zacharylwy
Frequent Visitor

Hi all,

Found the issue and fixed it:
When consolidating the data, I enter "0" rows for dates that do not have any intakes.
Then on Power BI I use the COALESCE function with 0 again.
The PowerBI step actually can handle dates that do not have data without requiring the "0" rows.

The fix was I do not manually populate empty dates with "0" rows. I pull raw data as it is, import it into BI, and stick to the same Total Voy Intake measure as above. 

View solution in original post

3 REPLIES 3
zacharylwy
Frequent Visitor

Hi all,

Found the issue and fixed it:
When consolidating the data, I enter "0" rows for dates that do not have any intakes.
Then on Power BI I use the COALESCE function with 0 again.
The PowerBI step actually can handle dates that do not have data without requiring the "0" rows.

The fix was I do not manually populate empty dates with "0" rows. I pull raw data as it is, import it into BI, and stick to the same Total Voy Intake measure as above. 

Anonymous
Not applicable

Thanks for the reply from johnt75 , please allow me to provide another insight: 
Hi  @zacharylwy ,

 

You can try the following dax:

Guinea MA  =
var _21day=
MAX('DateTable'[Date])-21
return
AVERAGEX(
    FILTER(ALLSELECTED('DateTable'),
    'DateTable'[Date]>=_21day&&'DateTable'[Date]<=MAX('DateTable'[Date])),[Guinea Total Voy Intake])

 

 

Best Regards,

Liu Yang

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

johnt75
Super User
Super User

I think values of 0 are automatically stripped out. Try

Guinea MA =
VAR NumDays = 21
VAR DatesToUse =
    DATESINPERIOD ( DateTable[Date], MAX ( DateTable[Date] ), - NumDays, DAY )
VAR Result =
    DIVIDE ( CALCULATE ( [Guinea Total Voy Intake], DatesToUse ), NumDays )
RETURN
    Result

Helpful resources

Announcements
August Power BI Update Carousel

Power BI Monthly Update - August 2025

Check out the August 2025 Power BI update to learn about new features.

August 2025 community update carousel

Fabric Community Update - August 2025

Find out what's new and trending in the Fabric community.