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

Join us for an expert-led overview of the tools and concepts you'll need to become a Certified Power BI Data Analyst and pass exam PL-300. Register now.

Reply
anwarbi
Helper III
Helper III

Calculate Average of distinct count of values by filtered column

I am looking to calculate average of distinct count of responses by location (locations need to be filtered lets say location A, Location B and Location C

 

I am not looking to aggregare the filtered locations but want to have the distict count of responses for each location and then have an average of each category. Any help would be appreciated. 

1 ACCEPTED SOLUTION

Hi @anwarbi ,

 

Here it is without building a separate table.


Let me know if you have any questions.

If this solves your issues, please mark it as the solution, so that others can find it easily. Kudos 👍are nice too.
Nathaniel

Average Value 2 =
VAR _peers = [Number of Peers]
VAR _summarizeTable12 =
    SUMMARIZECOLUMNS (
        Table1[Partner],
        FILTER ( Table1, Table1[If] = 1 ),
        "Max", MAX ( Table1[Distinct Count col] )
    )
VAR _sumMax =
    GROUPBY (
        _summarizeTable12,
        Table1[Partner],
        "check", SUMX ( CURRENTGROUP (), [Max] )
    )
VAR _TEST =
    SUMX ( _sumMax, [check] )
RETURN
    DIVIDE ( _TEST, _peers )

 





Did I answer your question? Mark my post as a solution!

Proud to be a Super User!




View solution in original post

6 REPLIES 6
v-angzheng-msft
Community Support
Community Support

Hi, @anwarbi 

 

@Nathaniel_C 's solution seems to work, but it looks a bit complicated for me.
I came up with another method that might be helpful.

 

distinct count =
CALCULATE (
    DISTINCTCOUNT ( Table1[Respondent ID] ),
    FILTER ( ALL ( 'Table1' ), [partner] = MAX ( 'Table2'[Peers] ) )
)
average of disctinct count =
AVERAGEX ( ALL ( Table2 ), [distinct count] )

 

Result:

vangzhengmsft_0-1636431860254.png

 

I tried to use just one measure to get the result, but I'm not successful yet, maybe someone can get it

 

Please refer to the attachment below for details. Hope this helps.

 

Best Regards,
Community Support Team _ Zeon Zheng


If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

Nathaniel_C
Community Champion
Community Champion

Hi @anwarbi ,
Next I created  2 columns in Table1, then a table and then a measure to give the average value. I am working on possibly a better solution, but this works.

Distinct Count col =
VAR _getPartner =
    MAX ( Table1[Partner] )
VAR _calc =
    CALCULATE (
        DISTINCTCOUNT ( Table1[Respondent ID] ),
        FILTER ( ALL ( Table1[Respondent ID] ), MAX ( Table1[Partner] ) = _getPartner )
    )
RETURN
    _calc
If = IF(Table1[Partner] in VALUES(Table2[Peers]), 1,0)

 

 

Average Value = 
var _peers = [Number of Peers]

var _sumMax = SUM('Summarize Table1'[Max]  )

return DIVIDE(_sumMax,_peers)

 

 

Summarize Table1 =
SUMMARIZECOLUMNS (
    Table1[Partner],
    FILTER ( Table1, Table1[If] = 1 ),
    "Max", MAX ( Table1[Distinct Count col] )
)

 

average value.PNG

 


Let me know if you have any questions.

If this solves your issues, please mark it as the solution, so that others can find it easily. Kudos 👍are nice too.
Nathaniel





Did I answer your question? Mark my post as a solution!

Proud to be a Super User!




anwarbi
Helper III
Helper III

Hi @Nathaniel_C 

 

Here is the dummy data, I have two tables in power bi.

Table 1 is a master table with all the data fields and table 2 only have 'Peers' name which I have created the relation with 'partner' field in table 1.

 

I am looking to create a measure that calculates a disctinct count of respondent ID (from table 1) for the peers (in table 2) .

 

And then calculate the average of disctinct count of respondents for each peer.

 

e.g. the output should be: Peer (A) has 2 unique responses, Peer (B) has three unique responses.

so the average will be 2.5 for both peers.   

 

I hope this gives enough, data points to calculate. Thanks. 

 

Table 1

partnerRespondent ID
AA1212
BB1212
AA1212
DC1212
EB1212
BA1212
AB1212
HD1212
BB1212
BD1212

 

 

Table 2

Peers
A
B

 

Hi @anwarbi ,
Thank you for the set up data. Below you will find a measure that can either be added to Table 2 or to Table 1 to give you the distinct count of the locations which may be attributed to the Peers in Table 2. Will get you the rest shortly. Because you linked the two tables, we can drop the Peers column from Table 2 onto the visual with Table 1 to filter it.

 

Please let me know if this is proceeding on the right path. 


Let me know if you have any questions.

If this solves your issues, please mark it as the solution, so that others can find it easily. Kudos 👍are nice too.
Nathaniel

 

Distinct Count =
VAR _getPartner =
    MAX ( Table1[Partner] )
VAR _calc =
    CALCULATE (
        DISTINCTCOUNT ( Table1[Respondent ID] ),
        FILTER ( ALL ( Table1[Respondent ID] ), MAX ( Table1[Partner] ) = _getPartner )
    )
RETURN
    _calc

 



Filter by Table2.PNGTable2.PNGTable1.PNG





Did I answer your question? Mark my post as a solution!

Proud to be a Super User!




Hi @anwarbi ,

 

Here it is without building a separate table.


Let me know if you have any questions.

If this solves your issues, please mark it as the solution, so that others can find it easily. Kudos 👍are nice too.
Nathaniel

Average Value 2 =
VAR _peers = [Number of Peers]
VAR _summarizeTable12 =
    SUMMARIZECOLUMNS (
        Table1[Partner],
        FILTER ( Table1, Table1[If] = 1 ),
        "Max", MAX ( Table1[Distinct Count col] )
    )
VAR _sumMax =
    GROUPBY (
        _summarizeTable12,
        Table1[Partner],
        "check", SUMX ( CURRENTGROUP (), [Max] )
    )
VAR _TEST =
    SUMX ( _sumMax, [check] )
RETURN
    DIVIDE ( _TEST, _peers )

 





Did I answer your question? Mark my post as a solution!

Proud to be a Super User!




Nathaniel_C
Community Champion
Community Champion

Hi @anwarbi ,
Please give us more information to be able to help you.  Perhaps a mocked up table done in Excel, or a pbix with a table.
If you would share your pbix, or dummy up some values in Excel both for current and expected data. Please copy and paste them into your post, rather than doing a picture, we may be able to help you. 

Please read this post to get your question answered more quickly:

https://community.powerbi.com/t5/Community-Blog/How-to-Get-Your-Question-Answered-Quickly/ba-p/38490
Thank you,
Nathaniel





Did I answer your question? Mark my post as a solution!

Proud to be a Super User!




Helpful resources

Announcements
Join our Fabric User Panel

Join our Fabric User Panel

This is your chance to engage directly with the engineering team behind Fabric and Power BI. Share your experiences and shape the future.

June 2025 Power BI Update Carousel

Power BI Monthly Update - June 2025

Check out the June 2025 Power BI update to learn about new features.

June 2025 community update carousel

Fabric Community Update - June 2025

Find out what's new and trending in the Fabric community.