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

Compete to become Power BI Data Viz World Champion! First round ends August 18th. Get started.

Reply
Sawyer_House
New Member

Calculate Average Based on Two Categories

I have two tables-- One with category types and one facts table that includes values.  I want to calculate a column with the average of the values in the facts table based on two categories in the other table.  What Dax expression would be best for this?  The tables are connected by a many:many relationship using the "Library" column because the category types and the facts table have multiple instances of each library (for multiple years of data).

LibraryLibrary Visits Per Capita
Alpha43.01
Beta27.11
Gamma32.08
Delta14.13
Epsilon9.29
Zeta50.39
Eta22.42
Theta12.13
Iota14.03
Kappa12.18
Lambda47.04
Mu14.4
Nu39.23
Pi2.11
Rho47.27
Sigma47.3
Tau32.38
Upsilon1.16
Phi16.14
Chi31.01
Psi27.11
Omega17.05

 

LibraryPopulationRevenue
Alpha20,000 and Under10,000 and Under
Beta100,000 and Up10,000 and Under
Gamma20,000 and Under50,000 to 100,000
Delta20,000 and Under50,000 to 100,000
Epsilon100,000 and Up50,000 to 100,000
Zeta20,000 and Under50,000 to 100,000
Eta20,000 and Under100,000 and Up
Theta20,000 to 100,000100,000 and Up
Iota20,000 to 100,000100,000 and Up
Kappa20,000 to 100,000100,000 and Up
Lambda20,000 and Under10,000 and Under
Mu20,000 and Under10,000 and Under
Nu20,000 to 100,00010,000 and Under
Pi20,000 to 100,00050,000 to 100,000
Rho100,000 and Up50,000 to 100,000
Sigma100,000 and Up100,000 and Up
Tau100,000 and Up100,000 and Up
Upsilon20,000 and Under10,000 and Under
Phi20,000 and Under50,000 to 100,000
Chi20,000 to 100,000100,000 and Up
Psi20,000 and Under50,000 to 100,000
Omega20,000 and Under50,000 to 100,000
14 REPLIES 14
Anonymous
Not applicable

Hi @Sawyer_House 

Has your problem been resolved? If so, could you mark the corresponding reply as the solution so that others with similar issues can benefit from it?

 

 

 


Best Regards,

Jayleny

 

Selva-Salimi
Super User
Super User

@Sawyer_House 

 

you can create a column in second table as follows :

_visitpercapita =lookupvalue( 'library' [library visit per capita]) ,  'library' [library] , 'your_table' [library])

 

and then create another column as follows:

avg = calculate (average(_visitpercapita) , filter ('your_table' , 'your_table' [population] = earlier ('your_table' [population])

 

If this post helps, then I would appreciate a thumbs up and mark it as the solution to help the other members find it more quickly. 

I tried this and received the following error message:

 

Filtered Visits Per Capita = lookupvalue(Facts[library visits per capita],Facts[Library], 'Filters'[Library])

 

A single value for column 'Library' in table 'Filters' cannot be determined. This can happen when a measure formula refers to a column that contains many values without specifying an aggregation such as min, max, count, or sum to get a single result.

 

 

that is not correct, you should create this column in your fact table but use the library table in lookup. I mean this....

 lookupvalue('Filters'[library visits per capita],'Filters'[Library]'Fact'[Library])

Okay-- the "library visits per capita" values are in the facts table, not the filters table.  That formula did not work.

 

I have tried something different, I used a formula to concatenate the population and revenue brackets so that now there is only one dimension I would need to focus on:

Would this make the calculation easier?

LibraryPopulationRevenuePopulation and RevenueMean Visits Per Capita by Population and Revenue
Beta100,000 and Up10,000 and Under100,000 and Up & 10,000 and Under27.11
Sigma100,000 and Up100,000 and Up100,000 and Up & 100,000 and Up39.84
Tau100,000 and Up100,000 and Up100,000 and Up & 100,000 and Up39.84
Epsilon100,000 and Up50,000 to 100,000100,000 and Up & 50,000 to 100,00028.28
Rho100,000 and Up50,000 to 100,000100,000 and Up & 50,000 to 100,00028.28
Alpha20,000 and Under10,000 and Under20,000 and Under & 10,000 and Under26.4025
Lambda20,000 and Under10,000 and Under20,000 and Under & 10,000 and Under26.4025
Mu20,000 and Under10,000 and Under20,000 and Under & 10,000 and Under26.4025
Upsilon20,000 and Under10,000 and Under20,000 and Under & 10,000 and Under26.4025
Eta20,000 and Under100,000 and Up20,000 and Under & 100,000 and Up22.42
Gamma20,000 and Under50,000 to 100,00020,000 and Under & 50,000 to 100,00026.15
Delta20,000 and Under50,000 to 100,00020,000 and Under & 50,000 to 100,00026.15
Zeta20,000 and Under50,000 to 100,00020,000 and Under & 50,000 to 100,00026.15
Phi20,000 and Under50,000 to 100,00020,000 and Under & 50,000 to 100,00026.15
Psi20,000 and Under50,000 to 100,00020,000 and Under & 50,000 to 100,00026.15
Omega20,000 and Under50,000 to 100,00020,000 and Under & 50,000 to 100,00026.15
Nu20,000 to 100,00010,000 and Under20,000 to 100,000 & 10,000 and Under39.23
Theta20,000 to 100,000100,000 and Up20,000 to 100,000 & 100,000 and Up17.3375
Iota20,000 to 100,000100,000 and Up20,000 to 100,000 & 100,000 and Up17.3375
Kappa20,000 to 100,000100,000 and Up20,000 to 100,000 & 100,000 and Up17.3375
Chi20,000 to 100,000100,000 and Up20,000 to 100,000 & 100,000 and Up17.3375
Pi20,000 to 100,00050,000 to 100,00020,000 to 100,000 & 50,000 to 100,0002.11

so why did you need library table?! all data seems to be in fact table. am I right?!

 

isn't it your library table?..

LibraryLibrary Visits Per Capita
Alpha43.01
Beta27.11

 

where the average should be calculated based on these values in this table?

I have a facts table and a filters table:

 

Filters Table--

LibraryPopulationRevenuePopulation and Revenue
Beta100,000 and Up10,000 and Under100,000 and Up & 10,000 and Under
Sigma100,000 and Up100,000 and Up100,000 and Up & 100,000 and Up
Tau100,000 and Up100,000 and Up100,000 and Up & 100,000 and Up
Epsilon100,000 and Up50,000 to 100,000100,000 and Up & 50,000 to 100,000
Rho100,000 and Up50,000 to 100,000100,000 and Up & 50,000 to 100,000
Alpha20,000 and Under10,000 and Under20,000 and Under & 10,000 and Under
Lambda20,000 and Under10,000 and Under20,000 and Under & 10,000 and Under
Mu20,000 and Under10,000 and Under20,000 and Under & 10,000 and Under
Upsilon20,000 and Under10,000 and Under20,000 and Under & 10,000 and Under
Eta20,000 and Under100,000 and Up20,000 and Under & 100,000 and Up
Gamma20,000 and Under50,000 to 100,00020,000 and Under & 50,000 to 100,000
Delta20,000 and Under50,000 to 100,00020,000 and Under & 50,000 to 100,000
Zeta20,000 and Under50,000 to 100,00020,000 and Under & 50,000 to 100,000
Phi20,000 and Under50,000 to 100,00020,000 and Under & 50,000 to 100,000
Psi20,000 and Under50,000 to 100,00020,000 and Under & 50,000 to 100,000
Omega20,000 and Under50,000 to 100,00020,000 and Under & 50,000 to 100,000
Nu20,000 to 100,00010,000 and Under20,000 to 100,000 & 10,000 and Under
Theta20,000 to 100,000100,000 and Up20,000 to 100,000 & 100,000 and Up
Iota20,000 to 100,000100,000 and Up20,000 to 100,000 & 100,000 and Up
Kappa20,000 to 100,000100,000 and Up20,000 to 100,000 & 100,000 and Up
Chi20,000 to 100,000100,000 and Up20,000 to 100,000 & 100,000 and Up
Pi20,000 to 100,00050,000 to 100,00020,000 to 100,000 & 50,000 to 100,000

 

Facts Table:

LibraryLibrary Visits Per Capita
Alpha43.01
Beta27.11
Gamma32.08
Delta14.13
Epsilon9.29
Zeta50.39
Eta22.42
Theta12.13
Iota14.03
Kappa12.18
Lambda47.04
Mu14.4
Nu39.23
Pi2.11
Rho47.27
Sigma47.3
Tau32.38
Upsilon1.16
Phi16.14
Chi31.01
Psi27.11
Omega17.05

 

I want to find a formula/calculation that will automatically calculate the Mean Visits Per Capita by Population and Revenue values that I provided above.

Ok, based on your first description the fact table repeated per year so first of all write this and let me know if it works...

Filtered Visits Per Capita = lookupvalue(Facts[library visits per capita],Facts[Library]'Filters'[Library] , Facts[year] , filters[year]) --add any other column that you think need to make it unique 

This does not seem to be working--it just returns the value that is listed in the facts table.  Unless you are planning on using this new column with another formula?

 

Also--I was not able to use the formula in the facts table, only in the filters table.

As I said before, it was the first step. the second step is to write another column as follows: 

 

avg = calculate (average(_visitpercapita) , filter ('filter' , 'filter' [population] = earlier ('filer' [population] && 'filters' [Revenue]= earlier ('filters' [revenue])))

 

** _visitpercapita is the name of column you calculated in the previous step **

 

If this post helps, then I would appreciate a thumbs up and mark it as the solution to help the other members find it more quickly. 

I used the above function and received this error:

 

The AVERAGE function only accepts a column reference as an argument.

 

Sawyer_House_0-1726253393329.png

 

@Sawyer_House 

as I said before, _visitpercapita is the name of column you calculated in the previous step.

in the image you shared the name is [filtered visit per capita], so replace it! 

Selva-Salimi
Super User
Super User

Hi @Sawyer_House

 

in fact it is not that much clear what is your expectation, but based on my understanding you can write a measure as follows:

 

measure _avg := var _category = values (category_table [category])

return

calculate (average (value ) , filter ( fact, 'fact' [category] in _actegory))

 

if it doesn't work please share more details or some example about your expectation.

 

If this post helps, then I would appreciate a thumbs up and mark it as the solution to help the other members find it more quickly. 

I want the average of the value by two categories.  I have manually calculated what I am trying to do in this table:

LibraryPopulationRevenueMean Visits Per Capita by Population and Revenue
Beta100,000 and Up10,000 and Under27.11
Sigma100,000 and Up100,000 and Up39.84
Tau100,000 and Up100,000 and Up39.84
Epsilon100,000 and Up50,000 to 100,00028.28
Rho100,000 and Up50,000 to 100,00028.28
Alpha20,000 and Under10,000 and Under26.4025
Lambda20,000 and Under10,000 and Under26.4025
Mu20,000 and Under10,000 and Under26.4025
Upsilon20,000 and Under10,000 and Under26.4025
Eta20,000 and Under100,000 and Up22.42
Gamma20,000 and Under50,000 to 100,00026.15
Delta20,000 and Under50,000 to 100,00026.15
Zeta20,000 and Under50,000 to 100,00026.15
Phi20,000 and Under50,000 to 100,00026.15
Psi20,000 and Under50,000 to 100,00026.15
Omega20,000 and Under50,000 to 100,00026.15
Nu20,000 to 100,00010,000 and Under39.23
Theta20,000 to 100,000100,000 and Up17.3375
Iota20,000 to 100,000100,000 and Up17.3375
Kappa20,000 to 100,000100,000 and Up17.3375
Chi20,000 to 100,000100,000 and Up17.3375
Pi20,000 to 100,00050,000 to 100,0002.11

 

In this table the "Mean visits per Capita by Population and Revenue" shows the average for libraries that have the same population category and the same population category.  So Beta does not match other libraries in population or revenue so its average visits per capita is the same as the average visits per capita by population and revenue.  However, Sigma and Tau have the same population and Revenue category--I want the average of their visits per capita based on the shared population and revenue.  Therefore, the average by population and revenue  for just those two libraries is

 

sum(library visits per capita)/count(libraries that meet the population and revenue requirements)

 

thus

 

[47.3 (#Sigma) + 32.38 (#Tau)]/2 (#the count of libraries) = 39.84

 

I want these values to calculate in a column in PowerBi--Is this possible?

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.

Top Solution Authors