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 September 15. Request your voucher.

Reply
Jrose
Frequent Visitor

Average with condition from another table

Hello,

I have spent all day trying various solutions to this and still can't get anything to work.  If there is another post with a solution already, please point me to it.

 

I've got two tables (examples below), one by person and year code with some other columns.  The other is a look-up table, one record per year code with additional codes and descriptors.  These are in the model, linked many-to-one by the year code.

 

What I need is the average count of person_id per year_part.  The person count is NOT distinct, it really is how many total.  The year_part seems to group ok by dropping it in the Group part of the visualization.

 

This almost works

mAvgYrPart = divide(count('table1'[person_id]),count('table2'[year_part]))
 
What I want to get, using the example, is average = 2
so that the count of person_id where table1.year_part=10  is 4  (year_part is actually coming through the relationship to table2 via year_code)

count of year_part where table2.year_part = 10 is 2
so average is 4/2 = 2

 

The problem is that I would get an average of 1, because the look-up table contains future years so there are actually four records where year_part = 10 and the calculation becomes 4/4 = 1.

 

How can it count table2.year_part where table2.year_code is less than or equal to max(table1.year_code)?  Oh, year_code is a number so MAX should work.  Year_part is text, just a label.

 

Seems it should be easy, but I've tried lookupvalue to pull the year_part to table1 first which kept giving errors, Related gave an error about ambiguity,  various combinations using Calculate, Summarize, average, quick measures keep giving errors or simply don't work correctly.  Started using DAX last week so not sure what to try next - any suggestions are appreciated.

 

 

 

table 1

person_idyear_codefact1fact2
1201810AA
1201810AB
1201820AB
2201810AC
2201820AA
2201820AB
2201910BA

 

table 2

year_codeyear_part
20181010
20182020
20183030
20191010
20192020
20201010
20202020
20211010
20212020
1 ACCEPTED SOLUTION
wdx223_Daniel
Super User
Super User

2 REPLIES 2
wdx223_Daniel
Super User
Super User

wdx223_Daniel_0-1627518933546.png

 

Awesome! Thank you so much.  

I used the exact formula you showed replaced with actual table and field names and it works perfectly, even with additional groups and filters on the visual.

 

FYI - the output you show isn't what I needed so I'm not sure how that happened.  But as said, it is doing what was needed with the real data, so thanks.

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.