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

Enhance your career with this limited time 50% discount on Fabric and Power BI exams. Ends August 31st. Request your voucher.

Reply
jacinto
Frequent Visitor

Filter table by current row value then take either average or current value

I spent quite a lot on this but still I am struggling. Here is what I want to achieve, I am happy with either measure or column that gives me accurate result. Filter List table by current Item then if CountryDS="GLO"  avg will be current value, otherwise avg is average of filtered table.  For example when we filter List table by Item=Carbs, there "GLO" in the CoutnryDS thus avg will be 2 for all Carbs. However for Fatty acid since there is no GLO, average is average of the values. I get an error stating temp can't be used and it needs base table.  Any suggestions is greatily appreciated!

 

avg =
VAR temp =
SUMMARIZE (
List,
List[Item],
List[CountryDs],
"avgFP", AVERAGE ( List[value] )
)
VAR GloValueCC =
CALCULATE (
AVERAGE ( temp[value] ),
FILTER ( temp, temp[CountryDs = "GLO" )
)
VAR country_avg =
AVERAGE ( temp[value)] )
RETURN
IF (
ISBLANK ( COUNTROWS ( CALCULATETABLE ( temp, temp[CountryS] = "GLO" ) ) ),
country_avg,
GloValueCC
)
ItemCountryDsValueavg
Fatty acidRoW1.22.6
Fatty acidRER3.12.6
Fatty acidRoW3.72.6
Fatty acidRER42.6
Fatty acidRoW12.6
CarbsRER52
CarbsRoW1.52
CarbsGLO22
CarbsRER92
1 ACCEPTED SOLUTION
amitchandak
Super User
Super User

@jacinto , Try a new measure like

 

var _1 = calculate( Average(Table[Value]), filter( allselected(Table) , Table[Item] = max(Table[Item])))
var _2 = calculate( Average(Table[Value]), filter( allselected(Table) , Table[Item] = max(Table[Item]) && Table[CountryS] = "GLO" ))
return
if(isblank(_2), _1, _2)

Share with Power BI Enthusiasts: Full Power BI Video (20 Hours) YouTube
Microsoft Fabric Series 60+ Videos YouTube
Microsoft Fabric Hindi End to End YouTube

View solution in original post

3 REPLIES 3
jacinto
Frequent Visitor

Thank you very much! Sorry for late reply, it works like a charm!

johnt75
Super User
Super User

If you know that there is at most one value of GLO for each item then you can try

avg =
var GLOValue = CALCULATETABLE(SELECTCOLUMNS( 'List', "@val", 'List'[Value]), 
   ALLEXCEPT('List', 'List'[Item])
)
return IF( NOT( ISBLANK( GLOValue)), GLOValue,
   CALCULATE( AVERAGE('List'[Value]), ALLEXCEPT('List'[Item]))
)
amitchandak
Super User
Super User

@jacinto , Try a new measure like

 

var _1 = calculate( Average(Table[Value]), filter( allselected(Table) , Table[Item] = max(Table[Item])))
var _2 = calculate( Average(Table[Value]), filter( allselected(Table) , Table[Item] = max(Table[Item]) && Table[CountryS] = "GLO" ))
return
if(isblank(_2), _1, _2)

Share with Power BI Enthusiasts: Full Power BI Video (20 Hours) YouTube
Microsoft Fabric Series 60+ Videos YouTube
Microsoft Fabric Hindi End to End YouTube

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.