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 August 31st. Request your voucher.

Reply
JGarland
Regular Visitor

Show a Distinct Count of IDs with an average falling into a value range

Looking for help with creating a measure(s) to support a visual. *Sample data at the bottom.

There are a couple different layers to get to the data needed. In the base data we have a primary key with multiple scores, one score per category, that need to be averaged at the primary key level. The scores are all whole number values from 1-5. It is possible and expected for there to be null scores included in the data. The average needs to be able to update when the score categories are filtered.

Along with the average there are 5 score buckets. The result of the average would determine which of 5 score buckets the primary key would fall into. The score buckets are:

Range NameRangeMinRangeMaxSortVal
0-1011
1-2122
2-3233
3-4344
4-5455

 

The visual would show the distinct count of primary keys per bucket. The counts would need to be able to update based on the average changing due to filtering by score category. There are some other categories that would be used as filters for this visual but those filters would only include/exclude primary keys and would change counts not scores.

End output would look like this, with the score ranges along the top, the values are the distinct counts of IDs falling in the ranges, and the row headers would be any related subcategory. I used segment since its on the sampe data.

Segment0-11-22-33-44-5
1112214352256118
2139235350238126
3247317301280156
498147271185135
     


Sample Data:

IDSegmentScore_CategoryScore_valueRegion
981002439
981002559
981002639
981002739
981002949
981003059
981003129
981003349
981003649
981003739
981003839
1251000557
1251000657
1251000757
1251000857
1251001157
1251001257
1251001437
1251001557
1251002057
1251002257
1251002457
1251002857
1251002917
1251003537
1251003657
1251003737
1251003837
48101000116
48101000216
48101000316
48101000416
48101000516
1 ACCEPTED SOLUTION

@JGarland,

 

Try this measure. It calculates the average score for each ID, and then counts the distinct ID within each bucket.

 

Average Distinct Count = 
VAR vRangeMin =
    MAX ( ScoreBucket[RangeMin] )
VAR vRangeMax =
    MAX ( ScoreBucket[RangeMax] )
VAR vTable =
    ADDCOLUMNS (
        VALUES ( FactTable[ID] ),
        "@Amount", CALCULATE ( AVERAGE ( FactTable[SCORE_VALUE] ) )
    )
VAR vResult =
    CALCULATE (
        DISTINCTCOUNT ( FactTable[ID] ),
        FILTER ( vTable, [@Amount] >= vRangeMin && [@Amount] <= vRangeMax )
    )
RETURN
    vResult

 

DataInsights_0-1646351582607.png

 





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

Proud to be a Super User!




View solution in original post

5 REPLIES 5
JGarland
Regular Visitor

@DataInsights 
Thank you for your response. I tried the measure you provided and ran it though a validation. The measure did a good job of placing scores under the correct bucket but the counts are too high. It looks like the measure is counting every score record per ID under a bucket rather than averaging all scores for an ID into a single value then counting that under a bucket. Validation measures below, I am also linking a de-identified version of my full dataset with NULL scores removed for space.

A distinct count of my data shows there are 988 unique IDs. Here I’ve got the results from the measure compared to a distinct count of ID per score value. In desired state the sum of the counts from each bucket would match the total distinct count of IDs. Achieved by averaging all scores per provider, while still letting that rollup average value change if a user filters the score_category. Is that possible?

JGarland_0-1646338897997.png

 

https://docs.google.com/spreadsheets/d/18iUvZr2OE71OOhyEE0eg5-93yQSA28JQ/edit?usp=drivesdk&ouid=1063...

Thank you!

@JGarland,

 

Try this measure. It calculates the average score for each ID, and then counts the distinct ID within each bucket.

 

Average Distinct Count = 
VAR vRangeMin =
    MAX ( ScoreBucket[RangeMin] )
VAR vRangeMax =
    MAX ( ScoreBucket[RangeMax] )
VAR vTable =
    ADDCOLUMNS (
        VALUES ( FactTable[ID] ),
        "@Amount", CALCULATE ( AVERAGE ( FactTable[SCORE_VALUE] ) )
    )
VAR vResult =
    CALCULATE (
        DISTINCTCOUNT ( FactTable[ID] ),
        FILTER ( vTable, [@Amount] >= vRangeMin && [@Amount] <= vRangeMax )
    )
RETURN
    vResult

 

DataInsights_0-1646351582607.png

 





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

Proud to be a Super User!




Thank you so much, that works perfectly. And you've given me my next topic to learn! Using variables seems really powerful.

@JGarland,

 

Glad to hear that works. Yes, variables are really powerful. They make coding and troubleshooting easier, especially when multiple levels of logic are involved.

 

I tweaked the measure in order to handle boundary cases, and it now totals 988. An average of 1, for example, was previously getting included in both the "0-1" and "1-2" buckets. Notice the filter now uses ">" instead of ">=":

 

Average Distinct Count = 
VAR vRangeMin =
    MAX ( ScoreBucket[RangeMin] )
VAR vRangeMax =
    MAX ( ScoreBucket[RangeMax] )
VAR vTable =
    ADDCOLUMNS (
        VALUES ( FactTable[ID] ),
        "@Amount", CALCULATE ( AVERAGE ( FactTable[SCORE_VALUE] ) )
    )
VAR vResult =
    CALCULATE (
        DISTINCTCOUNT ( FactTable[ID] ),
        FILTER ( vTable, [@Amount] > vRangeMin && [@Amount] <= vRangeMax )
    )
RETURN
    vResult

DataInsights_0-1646407405808.png

 





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

Proud to be a Super User!




DataInsights
Super User
Super User

@JGarland,

 

Try this measure:

 

Distinct Count ID =
VAR vRangeMin =
    MAX ( ScoreBucket[RangeMin] )
VAR vRangeMax =
    MAX ( ScoreBucket[RangeMax] )
VAR vResult =
    CALCULATE (
        DISTINCTCOUNT ( FactTable[ID] ),
        FactTable[Score_value] >= vRangeMin,
        FactTable[Score_value] <= vRangeMax
    )
RETURN
    vResult

 

I added an additional row of data for Segment 10 (different ID) to ensure it calculates properly:

 

DataInsights_0-1646315016605.png

 





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

Proud to be a Super User!




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.