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

Get Fabric Certified for FREE during Fabric Data Days. Don't miss your chance! Request now

Reply
gingerbread
Frequent Visitor

Measure to dynamically calculate clusters based on report filters and condition

Hi,

I have a table "Data" and it consists of the following columns: [Customer], [Active] and [Score in seconds]. Here is an example of the values:

 

CustomerActiveScore in seconds
A10,00
A00,02
A10,90
A11,00
A12,00
B10,20
B01,00
B12,30
B12,40
C01,30
C12,10
C12,50
C12,50
C13,50

 

I would like to create a measure in DAX, which will cluster customer values on the following conditions:

  • Group customers by name
  • Group scores, with less then 1 second apart, whithin customer group
  • I would like to be able to see how many clusters per customer I have and be able to flexibly filter on the Active = 0 or Active =1. My original tabel has many other filers, I will need for analysis.

So the end result should have a measure which would count number of clusters, per customer. The [Range (Min Score)] measure is a nice to have result:

 

 

Customer Total Clusters
Range (Min Score)
A30,00
A11,00
A12,00
B20,20
B22,30
C21,30
C22,50
C13,50

 

Your help is very much appreciated!


Thank you!

5 REPLIES 5
v-yanjiang-msft
Community Support
Community Support

Hi @gingerbread ,

According to your description, here's my solution.

Create two measures.

Cluster =
VAR Result =
    ADDCOLUMNS (
        ALLSELECTED ( 'Data' ),
        "Cluster",
            RANKX (
                FILTER (
                    ALLSELECTED ( 'Data' ),
                    [Customer] = EARLIER ( [Customer] )
                        && ABS ( 'Data'[Score in seconds] - EARLIER ( 'Data'[Score in seconds] ) ) >= 1
                ),
                'Data'[Score in seconds],
                ,
                ASC,
                DENSE
            )
    )
VAR ClusteredData =
    GROUPBY (
        Result,
        [Customer],
        [Cluster],
        "Range(Min Score)", MINX ( CURRENTGROUP (), 'Data'[Score in seconds] )
    )
RETURN
    IF (
        CONTAINS ( ClusteredData, [Range(Min Score)], MAX ( 'Data'[Score in seconds] ) )
            && MAX ( 'Data'[Customer] )
                = MAXX (
                    FILTER ( ClusteredData, [Range(Min Score)] = MAX ( 'Data'[Score in seconds] ) ),
                    [Customer]
                ),
        1,
        0
    )
Total Clusters =
VAR _Nest =
    MINX (
        FILTER (
            ALLEXCEPT ( 'Data', 'Data'[Active] ),
            [Cluster] = 1
                && 'Data'[Customer] = MAX ( 'Data'[Customer] )
                && 'Data'[Score in seconds] > MAX ( 'Data'[Score in seconds] )
        ),
        'Data'[Score in seconds]
    )
RETURN
    IF (
        ISFILTERED ( Data[Active] ),
        COUNTROWS (
            FILTER (
                ALL ( 'Data' ),
                'Data'[Active] = SELECTEDVALUE ( Data[Active] )
                    && 'Data'[Customer] = MAX ( 'Data'[Customer] )
                    && 'Data'[Score in seconds] >= MAX ( 'Data'[Score in seconds] )
                    && 'Data'[Score in seconds]
                        < IF (
                            _Nest <> BLANK (),
                            _Nest,
                            MAXX ( ALL ( 'Data' ), 'Data'[Score in seconds] ) + 1
                        )
            )
        ),
        COUNTROWS (
            FILTER (
                ALL ( 'Data' ),
                'Data'[Customer] = MAX ( 'Data'[Customer] )
                    && 'Data'[Score in seconds] >= MAX ( 'Data'[Score in seconds] )
                    && 'Data'[Score in seconds]
                        < IF (
                            _Nest <> BLANK (),
                            _Nest,
                            MAXX ( ALL ( 'Data' ), 'Data'[Score in seconds] ) + 1
                        )
            )
        )
    )

Put Customer, Score in seconds, Total Clusters measure in visual, and Cluster in visual filter and select "Show item when the value is 1", get the correct result. It also works for the Active slicer.

vyanjiangmsft_0-1692598624308.png

I attach my sample below for your reference.

 

Best Regards,
Community Support Team _ kalyj

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

Hey @v-yanjiang-msft thank you very much for the quick answer. I have populated the data set with more values and new data. It seems to work very well for the first cluster whithin the group, but not the rest of the groups. See picture bellow eg:

gingerbread_0-1692631294491.png

Do you know what could be the issue and what needs to change?

 

Hi @gingerbread ,

Can't see the reason from the snapshot, it seems you use the original pbix I send to you, if the data is not sensitive, could you please send it back here, then I can dig into it.

 

Best regards,

Community Support Team_yanjiang

sure 🙂 , here is the file (link valid for 7 days): https://we.tl/t-M74Zj7MhN2

amitchandak
Super User
Super User

@gingerbread , I think you are looking for

Dynamic Segmentation Bucketing Binning
https://community.powerbi.com/t5/Quick-Measures-Gallery/Dynamic-Segmentation-Bucketing-Binning/m-p/1...


Dynamic Segmentation, Bucketing or Binning: https://youtu.be/CuczXPj0N-k

 

Dynamic segmentation -Measure to Dimension conversion: https://youtu.be/gzY40NWJpWQ

Share with Power BI Enthusiasts: Full Power BI Video (20 Hours) YouTube
Microsoft Fabric Series 60+ Videos YouTube
Microsoft Fabric Hindi End to End YouTube

Helpful resources

Announcements
Fabric Data Days Carousel

Fabric Data Days

Advance your Data & AI career with 50 days of live learning, contests, hands-on challenges, study groups & certifications and more!

October Power BI Update Carousel

Power BI Monthly Update - October 2025

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

FabCon Atlanta 2026 carousel

FabCon Atlanta 2026

Join us at FabCon Atlanta, March 16-20, for the ultimate Fabric, Power BI, AI and SQL community-led event. Save $200 with code FABCOMM.

Top Solution Authors