Forum Discussion

Sam_Zeus's avatar
Sam_Zeus
Frequent Visitor
4 years ago

4 week average calculation having missing values on bar chart because of lack of events in 1 week

This may be a confusing issue seeing it's quite specific so apologies in advance.

 

I have a calculation that tracks the 4 week average of an event's occurance, grouped by weeks. This event only occurs 0 to 3 times a week at a given place.

The calculation works fine on weeks that have had events (e.g. Week 6 in later posted screenshot), even calculating correctly when the 4-week calculation includes the 0 event week (Week 5), but returns blank when based on a week that did not have an event but has a non-zero 4 week average (e.g. Week 5).

 

Here's my calculation:

 

4WkAvSiteGraph = 
VAR _LastProdWeek = MAX('Date'[ProdWeekOrder])-1
VAR _Duration = 4
VAR _CalculationPeriod =
    FILTER(
        ALL('Date'),
        AND('Date'[ProdWeekOrder] > _LastProdWeek - _Duration,
            'Date'[ProdWeekOrder] <= _LastProdWeek)
    )

VAR _EventsInSpan =
    IF(
        COUNTROWS(_CalculationPeriod) >= _Duration,
        CALCULATE(SUM('DataTable'[EventCounter]), _CalculationPeriod)
    )

RETURN _EventsInSpan/_Duration

 

 

Here's the bar chart visual of the 4 week moving average, note the missing y-axis values like Week 5, with a line chart underneath it showing the basic count of events that have occured in the same weeks at the same location:

 

Why does the calculation return a blank for these eventless weeks that have a non-zero moving average, and how do I modify the calculation to stop this from occuring?

2 Replies

  • Sam_Zeus's avatar
    Sam_Zeus
    Frequent Visitor

    This option doesn't change anything, and if I use the "+0" trick (return (_EventsInSpan/_Duration)+0) at the end of the calculation we can see that the calculation is returning blanks when it shouldn't:

     

    The 4 week average for week 5 is (1+3+1+0)/4 = 1.25 for the values of week 2, 3, 4, 5 respectively. However because week 5 had 0 incidents the calculation is returning a blank.
    I'm suspicious that the issue is the date logic (aka the weeks being recorded in _CalculationPeriod) is being filtered by the location slicer as there are no values connect these 3: site -> data -> date. If I figure out the issue I'll reply to this and mark my method as a solution but otherwise I would appreciate any help.