Sunday, June 28, 2020

Dynamic grouping of values in Power BI [Banding]

Sometimes there are needs when you would want to Power BI to give user the ability to choose the bands for a particular dimension and update the charts dynamically. In this example we are going to discuss one such example.

Business Case: User wants to see the count of customers within different age groups where he wants to change the age groupings dynamically and does not want fixed groups.

Lets Start:
We can start by looking into how we can produce a table where age groupings are defined:

Age Band = GENERATESERIES(0,120,1)

This will generate a table with all possible age values between 0 & 120.
We can simply connect it with our dimension table and have age from Age Group table as the X-axis to filter the count of customers in the dimension table.

Dynamic: Now this is static, we will dive deeper and make it dynamic.

The measure/trick is:

Count of Customers in each group - by size =
var _minAge=[Min Age]
var _maxAge=[Max Age]
var _bandSize=[What is Band Size Value]
var _AgeBandTable=GENERATESERIES(_minAge,_maxAge,_bandSize)
var _currAge=SELECTEDVALUE('Age Band'[Age])
var _bandHead=MAXX(
    FILTER(
    _AgeBandTable,
    [Value]<=_currAge),[Value])
var _bandTail=_bandHead+_bandSize
return
if(_currAge=_bandHead,
CALCULATE(
    COUNTROWS(DimCustomer),
'Age Band'[Age]>=_bandHead && 'Age Band'[Age]<_bandTail
))


Finally you can use the measure to dynamically update your visual.

Regards

No comments:

Post a Comment