Forum Discussion

croine's avatar
croine
Frequent Visitor
4 years ago
Solved

Average Values with multiple criteria

Hi all,

I have just started learning Power Bi and I got stuck almost at the begging. Not sure how complicated this is, but I can`t seem to get to an answer by myself. 

 

I need to infill the average of a column based on the last 7 values that are not blank and if this can`t be done then base the average on the whole column.

 

Thank you very much in advance! 

  • Hi, croine 
    I got your result:
    There are some things that could be optimized but if you need it to work in the first place, it should be all you need:
    I started with creating some "Ranking" columns in order to evaluate 7 ABOVE rows, I used simply Index Column in Power Query and worked with it like that.
    Then I added two columns to check, whether the column is empty or not (This could be optimized later)
    Finally, two more columns that actually fill the missing info based on your criteria (you can't fill existing columns in DAX)

    Here is the code:

    DailyAverageBlankCheck = 
    var BlankCheck = IF(ISBLANK('Table'[Daily Average]),0,1)
    
    return BlankCheck
    
    FixedHourBlankCheck = 
    var BlankCheck = IF(ISBLANK('Table'[Fixed hour]),0,1)
    
    return BlankCheck
    
    DailyAverageFixed = 
    var currentRank = 'Table'[Index]
    var sevenRowsAbove = FILTER('Table', currentRank >= 'Table'[Index])
    var TableForAverage = TOPN(8,sevenRowsAbove,'Table'[Index])
    var Top7Check = CALCULATE(SUM('Table'[DailyAverageBlankCheck]),TOPN(7,sevenRowsAbove,'Table'[Index]))
    var DailyAverage = CALCULATE(AVERAGEX('Table','Table'[Daily Average]),FILTER('Table','Table'[DailyAverageBlankCheck]=1))
    
    var Result = SWITCH(TRUE(),
        NOT(ISBLANK('Table'[Daily Average])), 'Table'[Daily Average],
        'Table'[DailyAverageBlankCheck]=0 && Top7Check=6, AVERAGEX(TableForAverage,'Table'[Daily Average]),
        DailyAverage)
    
    return Result
    
    FixedHourFixed = 
    var currentRank = 'Table'[Index]
    var sevenRowsAbove = FILTER('Table', currentRank >= 'Table'[Index])
    var TableForAverage = TOPN(8,sevenRowsAbove,'Table'[Index])
    var Top7Check = CALCULATE(SUM('Table'[FixedHourBlankCheck]),TOPN(7,sevenRowsAbove,'Table'[Index]))
    var HoursAverage = CALCULATE(AVERAGEX('Table','Table'[Fixed hour]),FILTER('Table','Table'[FixedHourBlankCheck]=1))
    
    var Result = SWITCH(TRUE(),
        NOT(ISBLANK('Table'[Fixed hour])), 'Table'[Fixed hour],
        'Table'[FixedHourBlankCheck]=0 && Top7Check=6, AVERAGEX(TableForAverage,'Table'[Fixed hour]),
        HoursAverage)
    
    return Result

     
    Sample data screen:

     

8 Replies

    • croine's avatar
      croine
      Frequent Visitor

      Hi, 

      Sure, sorry I forgot to add that. I need to apply the averages for the [Fixed Hour] and [Daily Average] when point counts dont meet a criteria previously set up.

       

      • vojtechsima's avatar
        vojtechsima
        Super User

        croine 
        No problem, could you please paste it here as a Table, so I can copy it and come with a tailor-made solution? 
        thanks