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

Calling all Data Engineers! Fabric Data Engineer (Exam DP-700) live sessions are back! Starting October 16th. Sign up.

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
FabCon Global Hackathon Carousel

FabCon Global Hackathon

Join the Fabric FabCon Global Hackathon—running virtually through Nov 3. Open to all skill levels. $10,000 in prizes!

FabCon Atlanta 2026 carousel

FabCon Atlanta 2026

Join us at FabCon Atlanta, March 16-20, for the ultimate Fabric, Power BI, AI and SQL community-led event. Save $200 with code FABCOMM.

Top Solution Authors
Top Kudoed Authors