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

Register now to learn Fabric in free live sessions led by the best Microsoft experts. From Apr 16 to May 9, in English and Spanish.

Reply
croine
Frequent Visitor

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! 

1 ACCEPTED SOLUTION

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:

vojtechsima_0-1635532831217.png

 

View solution in original post

8 REPLIES 8
vojtechsima
Memorable Member
Memorable Member

@croine 
Hi, could you please provide sample data for your request?

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.

croine_1-1635506129427.png

 

@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

Hope I am doing this right 

CPPsDateFixed hourDaily AverageDaily pressure point countFixed hour point count
CPP_9983_PRESSURE_R01/04/2019 00:00  00
CPP_9984_PRESSURE_R01/04/2019 00:00  00
CPP_9985_PRESSURE_R01/04/2019 00:00  00
CPP_9986_PRESSURE_R01/04/2019 00:00  00
CPP_9987_PRESSURE_R01/04/2019 00:00  00
CPP_9988_PRESSURE_R01/04/2019 00:00  00
CPP_9989_PRESSURE_R01/04/2019 00:00  00
CPP_9990_PRESSURE_R01/04/2019 00:00  00
CPP_9992_PRESSURE_R01/04/2019 00:00  00
CPP_9993_PRESSURE_R01/04/2019 00:00  00
CPP_9994_PRESSURE_R01/04/2019 00:00  00
CPP_9995_PRESSURE_R01/04/2019 00:00  00
CPP_9996_PRESSURE_R01/04/2019 00:00  00
CPP_9997_PRESSURE_R01/04/2019 00:00  00
CPP_9998_PRESSURE_R01/04/2019 00:00  00
CPP_293_PRESSURE_R01/04/2019 00:000.987411176 00
CPP_6999_PRESSURE_R01/04/2019 00:000.995377362 00
CPP_335_PRESSURE_R01/04/2019 00:001.2000000481.237500012964
CPP_6213_PRESSURE_R01/04/2019 00:001.2522.8260417964
CPP_6798_PRESSURE_R01/04/2019 00:004.01321194 00
CPP_2385_PRESSURE_R01/04/2019 00:004.454083887 00
CPP_5296_PRESSURE_R01/04/2019 00:007.110839356 00
CPP_7305_PRESSURE_R01/04/2019 00:007.175306121 00
CPP_1395_PRESSURE_R01/04/2019 00:0011.36093381 00
CPP_2394_PRESSURE_R01/04/2019 00:0011.46102538 00
CPP_344_PRESSURE_R01/04/2019 00:001312.24479165964
CPP_71_PRESSURE_R01/04/2019 00:001413.72812501964
CPP_7699_PRESSURE_R01/04/2019 00:0015.6833332422.07604166964
CPP_7197_PRESSURE_R01/04/2019 00:0016.3666661617.08541661964
CPP_33_PRESSURE_R01/04/2019 00:0016.466666712.13854173964
CPP_5393_PRESSURE_R01/04/2019 00:0017.23829804 00
CPP_6797_PRESSURE_R01/04/2019 00:0017.5333334619.00729179964
CPP_6611_PRESSURE_R01/04/2019 00:0017.9833335917.01874997964
CPP_5114_PRESSURE_R01/04/2019 00:0018.0833336515.80625001964
CPP_4508_PRESSURE_R01/04/2019 00:0018.3833325717.81458324964
CPP_2698_PRESSURE_R01/04/2019 00:0018.44015744 00
CPP_0288_PRESSURE_R01/04/2019 00:0018.5333334619.79374989964
CPP_1398_PRESSURE_R01/04/2019 00:0018.6000003817.42395832964
CPP_7078_PRESSURE_R01/04/2019 00:0018.6166671118.56875012964
CPP_5298_PRESSURE_R01/04/2019 00:0018.683333419.79062496964
CPP_7055_PRESSURE_R01/04/2019 00:0018.9333330817.82708327964
CPP_338_PRESSURE_R01/04/2019 00:0018.933333419.31770839964
CPP_317_PRESSURE_R01/04/2019 00:001917.75833324964
CPP_191_PRESSURE_R01/04/2019 00:0019.06728143 00
CPP_327_PRESSURE_R01/04/2019 00:0019.2000007619.09687535964
CPP_4188_PRESSURE_R01/04/2019 00:0019.2833334621.37291666964
CPP_5299_PRESSURE_R01/04/2019 00:0019.3333330219.73020832964
CPP_346_PRESSURE_R01/04/2019 00:0019.4499998119.05000013964
CPP_347_PRESSURE_R01/04/2019 00:0019.4666665419.36979151964
CPP_73_PRESSURE_R01/04/2019 00:0019.4833332720.53684219954
CPP_6297_PRESSURE_R01/04/2019 00:0019.521.16947361954
CPP_2899_PRESSURE_R01/04/2019 00:0019.5499998718.62916658964
CPP_6159_PRESSURE_R01/04/2019 00:0019.6000003820.69270826964
CPP_2053_PRESSURE_R01/04/2019 00:0019.6336809920.1000003810
CPP_2885_PRESSURE_R01/04/2019 00:0019.7000001319.76041661964
CPP_591_PRESSURE_R01/04/2019 00:0019.8499997524.22604163964
CPP_5113_PRESSURE_R01/04/2019 00:0019.85633098 00
CPP_64_PRESSURE_R01/04/2019 00:0019.8666661618.82291664964
CPP_353_PRESSURE_R01/04/2019 00:0019.9166669818.89270829964
CPP_2993_PRESSURE_R01/04/2019 00:0020.1333335221.2208333964
CPP_599_PRESSURE_R01/04/2019 00:0020.1999994923.09062499964
CPP_3391_PRESSURE_R01/04/2019 00:0020.2520.20937494964
CPP_3589_PRESSURE_R01/04/2019 00:0020.2833331420.93333334964
CPP_322_PRESSURE_R01/04/2019 00:0020.4333330819.92916663964
CPP_2803_PRESSURE_R01/04/2019 00:0020.5833330220.53854165964
CPP_492_PRESSURE_R01/04/2019 00:0020.6333335226.60312506964
CPP_342_PRESSURE_R01/04/2019 00:0020.7333332721.04583339964
CPP_692_PRESSURE_R01/04/2019 00:0020.7833328220.79999989964
CPP_7054_PRESSURE_R01/04/2019 00:0020.7999992420.29999977964
CPP_501_PRESSURE_R01/04/2019 00:0020.933333421.74687505964
CPP_28_PRESSURE_R01/04/2019 00:0020.93571162 00

@croine 
Thank you for your input,
Can you please again clarify what you need? I am not sure I get your points. Which Columns to fill? 7 latest values in what column and based on what criteria? 

I need to infill Fixed Hour and Daily Average's missing data with available values in the columns. First by averaging the last 7 values above the blank and if those are not available, then infill with a column average.

Or I can create 2 new columns, which I was trying to do and failed.

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:

vojtechsima_0-1635532831217.png

 

Thanks a lot! I`ve learned so many things from this solution only, much appreciated!

 

Helpful resources

Announcements
Microsoft Fabric Learn Together

Microsoft Fabric Learn Together

Covering the world! 9:00-10:30 AM Sydney, 4:00-5:30 PM CET (Paris/Berlin), 7:00-8:30 PM Mexico City

PBI_APRIL_CAROUSEL1

Power BI Monthly Update - April 2024

Check out the April 2024 Power BI update to learn about new features.

April Fabric Community Update

Fabric Community Update - April 2024

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