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

Register now to learn Fabric in free live sessions led by the best Microsoft experts. From Apr 16 to May 9, in English and Spanish.

Reply
Anonymous
Not applicable

Applying Multiple Filters To Dynamic Bin Chart Using Mean and Sum

I am currently trying to create a dynamic graph in power bi that is giving me a bit of trouble.  I currently have a data set with 4 columns - one is a value, and three are categorical.  Let's call them columns V, C1, C2, and C3.  The format of the data is as follows, with example dummy values:

Value (V)Category 1 (C1)Category 2 (C2)Category 3 (C3)
10111
10112
30113
10121
20122
30123
30131
10132
10133
30211
20212
10213
20221
20222
10223
20231
30232
30233

 

My eventual goal is to create a graph with two slicers - one for C2, and one for C3.  The slicers will trim the data set used in calculation to only include rows with the values selected by C2 and C3, and can contain any subset of unique category variables that occur in each column respectively.  On this graph, there will be one data point for each unique category value in C1.  The graph will be a bin chart, and each bin will have a value in it if the values encompassed by the set of values in V that match each unique category value in C1 fall into that amount after going through a series of calculations.

I know how to set up slicers, and how to bin data, but dynamically updating the table to do the proper bin calculations based on what is selected in the slicers is the part I am having trouble with.  I will walk through step by step how the calculations should work with the example dummy data above.  First, lets assume that we select to only include categories 1 and 2 for C2, and only categories 1 and 2 for C3.  That will then make the data that we are working with look like this:

Value (V)Category 1 (C1)Category 2 (C2)Category 3 (C3)
10111
10112
10121
20122
30211
20212
20221
20222

 

After this I would like to "compress" the table by effectively eliminating C3, then combining each row that contains the same values for both C1 and C2.  When combining these rows, I want this to average the values that are combined together.  After this step is done, the data will look like this for our example:

 

Value (V)Category 1 (C1)Category 2 (C2)
1011
1512
2521
2022

 

After this, I would then like to "compress" the table again by effectively eliminating C2 and combining rows that have the same values for C1, just like the previous step, but this time summing the values in the V column that get combined together.  The resulting data in our example will look like this:

Value (V)Category 1 (C1)
251
45

2

 

Finally, I would like to take each final calculated value in V that matches each unique category value in C1, and bin those.  For our example, lets say we have bins for values from 1-60 with a bin size of 10.  That would leave our final bin chart looking like this for our example:

 

Count      
2      
1  |||||||||| |||||||||| 
Value1-1011-2021-3031-4041-5051-60

 

If just calculating this one time, I could just make a set of queries to continuously trim the data down to what I want, then display it in the bin chart.  However, the difficult part is that I need to recalculate this and remake the graph each time the categories we are using in C2 and C3 are changed by the slicers.  So I assume I would probably need to do this in DAX, but after studying that for a while, I am still not sure how to do it there.  If anyone could provide a way for building this sort of graph in Power BI, or let me know if it is not currently supported, I would really appreciate that!  Thank you so much!

1 ACCEPTED SOLUTION
v-kelly-msft
Community Support
Community Support

Hi @Anonymous ,

 

First you need to create a measure as below:

 

 

Measure = SUMX(VALUES('Table'[Category 2 (C2)]),CALCULATE(AVERAGE('Table'[Value (V)])))

 

 

Then create a bin table as below:

 

 

Annotation 2020-02-28 172959.png

 

Then create a measure as below:

 

Measure 2 = var a = MAX('Table (2)'[Count])
var b =  CALCULATE([Measure],FILTER(ALLSELECTED('Table'),'Table'[Category 1 (C1)]= a))
var mid= SEARCH("-",MAX('Table (2)'[value]))
var mind = LEFT(MAX('Table (2)'[value]),mid-1)+0
var maxd = RIGHT(MAX('Table (2)'[value]),LEN(MAX('Table (2)'[value]))-mid)+0
return
IF(b>=mind && b<maxd,1,BLANK())

 

Finally you will see:

Annotation 2020-02-28 173516.png

 

For the related .pbix file,pls click here.

 

Best Regards,
Kelly
 
Did I answer your question? Mark my post as a solution!

 

 

 

 

View solution in original post

2 REPLIES 2
v-kelly-msft
Community Support
Community Support

Hi @Anonymous ,

 

First you need to create a measure as below:

 

 

Measure = SUMX(VALUES('Table'[Category 2 (C2)]),CALCULATE(AVERAGE('Table'[Value (V)])))

 

 

Then create a bin table as below:

 

 

Annotation 2020-02-28 172959.png

 

Then create a measure as below:

 

Measure 2 = var a = MAX('Table (2)'[Count])
var b =  CALCULATE([Measure],FILTER(ALLSELECTED('Table'),'Table'[Category 1 (C1)]= a))
var mid= SEARCH("-",MAX('Table (2)'[value]))
var mind = LEFT(MAX('Table (2)'[value]),mid-1)+0
var maxd = RIGHT(MAX('Table (2)'[value]),LEN(MAX('Table (2)'[value]))-mid)+0
return
IF(b>=mind && b<maxd,1,BLANK())

 

Finally you will see:

Annotation 2020-02-28 173516.png

 

For the related .pbix file,pls click here.

 

Best Regards,
Kelly
 
Did I answer your question? Mark my post as a solution!

 

 

 

 

Anonymous
Not applicable

Thank you very much - this is quite useful!  The bit on displaying the bins in the table is a little bit buggy when switched over to my original data set instead of just the testing one, but I should be able to work out those out on my own.  You really helped me a lot!

Helpful resources

Announcements
Microsoft Fabric Learn Together

Microsoft Fabric Learn Together

Covering the world! 9:00-10:30 AM Sydney, 4:00-5:30 PM CET (Paris/Berlin), 7:00-8:30 PM Mexico City

PBI_APRIL_CAROUSEL1

Power BI Monthly Update - April 2024

Check out the April 2024 Power BI update to learn about new features.

April Fabric Community Update

Fabric Community Update - April 2024

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