Forum Discussion

Anonymous's avatar
Anonymous
Not applicable
6 years ago
Solved

Calculating yearly average from monthly average

Hi everyone,   I'm quite new to PowerBI, so my problem may be quite easy to solve, but I didn't really find a suitable answer yet.   So I have lots of geographical data from a 40 year long period...
  • Anonymous's avatar
    Anonymous
    6 years ago

    You are trying to create a measure that will be calculated differently on different levels of a hierarchy. Therefore this measure will be tightly connected to this hierarchy and should not be used with any other level - just: Date, Month, and Year. If you use it on a different level, the calculation will be wrong. In DAX, hierarchies are not a strong point. Here's the measure but you should not use it with any other level than just the 3 mentioned. If you want to calculate on any other level (for instance, for all years as the average of the measure for the years), you have to make changes to it.

    // Hidden measure
    [_AverageInOneDay] =
    	AVERAGE( Data[Measurement] )
    
    // Hidden measure
    [_AverageInOneMonth] =
    	AVERAGEX(
    		VALUES( Calendar[Date] ),
    		[_AverageInOneDay]
    	)
    	
    // Hidden measure
    [_AverageInOneYear] =
    	AVERAGEX(
    		VALUES( Calendar[Year Month] ),
    		[_AverageInOneMonth]
    	)
    	
    // Final measure exposed
    [Measurement] =
    SWITCH( true(),
    	ISINSCOPE( Calendar[Date] ),
    		[_AverageInOneDay],
    	ISINSCOPE( Calendar[Year Month] ),
    		[_AverageInOneMonth],
    	ISINSCOPE( Calendar[Year] ),
    		[_AverageInOneYear]
    )

     

    Best

    D