Forum Discussion
Measure to dynamically calculate clusters based on report filters and condition
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.
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:
Do you know what could be the issue and what needs to change?
- v-yanjiang-msft3 years ago
Community Support
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
- gingerbread3 years agoFrequent Visitor
sure 🙂 , here is the file (link valid for 7 days): https://we.tl/t-M74Zj7MhN2