Forum Discussion

Anonymous's avatar
Anonymous
Not applicable
1 year ago

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

  • Hi Anonymous

     

    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. 

    • Anonymous's avatar
      Anonymous
      Not applicable

      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?

  • Anonymous 

     

    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. 

    • Anonymous's avatar
      Anonymous
      Not applicable

      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.

       

       

      • Selva-Salimi's avatar
        Selva-Salimi
        Solution Sage

        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])

  • Anonymous's avatar
    Anonymous
    Not applicable

    Hi Anonymous 

    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