Power BI: create an interactive comparison chart

I will explain how to create a chart that shows the difference between 2 values but:

  • Option 1: 1 of the values is based on the selection done by someone with 1 filter
  • Option 2: both values are based on the selection done by someone with 2 filters
power bi

The chart will allow me to compare 2 values based on the interaction of my audience. I have this data:

power bi

This measure is used for both options and it is to calculate in percentage the ticket based on the number:


var blank0=COUNT('table1'[argument1])/CALCULATE(COUNT('table1'[argument1]),ALL('table1'[argument2]))
return
IF(ISBLANK(blank0),0,blank0)
              
power bi

NOTE:

  • Change table1 and argument1/argument2 by yours
  • Don’t forget to format in percentage

From now, I will create other measures according to the option.

Option 1

I will create this measure to calculate in percentage how many tickets with number between 0 and 6:

CALCULATE(CALCULATE(COUNT('table1'[argument1]),'table1'[argument2]>=xx1 && 'table1'[argument2]<=xx2)/COUNT('table1'[argument1]),ALL('table1'[argument2]))

power bi

NOTE:

  • Change xx1 and xx2 by yours
  • Don’t forget to format in percentage
  • Result will not change, no matter what I will select on the filter

I will create a clustered column chart and a filter:

power bi power bi

By using the filter, the result of the “measure” will change and since “measure 2” is stable, I can compare easily both results:

power bi power bi

To make my life easier, I will add a variance line so I don’t need to calculate the difference. For that, I will adapt the option 2 of my article Power BI: display a variance line in a chart (the article makes reference to columns instead of measures) so my dynamic measure:

power bi power bi

var xdiff=[measure1]-[measure2]
var result=
SWITCH(
    TRUE(),
    xdiff=0,FORMAT(xdiff,"0%"),
    xdiff>=0,"▲ +"&((FORMAT([measure1],"0.00")*100)-(FORMAT([measure2],"0.00")*100))&"%",
    xdiff<0,"▼ "&((FORMAT([measure1],"0.00")*100)-(FORMAT([measure2],"0.00")*100))&"%"
)
Return
""""&result              
              

NOTE:

  • Change measure1 and measure2 by yours
  • Using the formula above, it will show a “variance line” but if I want to display a “monthly variance” (just above the month), use this formula:
    IF(VALUE([measure1])>VALUE([measure2]),VALUE([measure1])*1.2,VALUE([measure2])*1.2)
power bi power bi

And my coloring measure:


var xdiff=[measure1]-[measure2]
Return
SWITCH(
    TRUE(),
    xdiff>=0,"#1AAB40",
    xdiff<0,"#D64554"
)             
              
power bi

I will change my chart to a line and clustered column:

power bi

Then I will configure the “y-axis” and the “second y-axis” (skip this configuration if you will use the “monthly variance”):

power bi power bi

This is the result:

power bi

Option 2

I will have to upload the same data then I will create an inactive relationship:

power bi

I will create a measure:


var blank0=CALCULATE(COUNT('table1'[argument1])/CALCULATE(COUNT('table1'[argument1]),ALL('table2'[argument2])),USERELATIONSHIP('table1'[argument1],'table2'[argument1]))
return
IF(ISBLANK(blank0),0,blank0)
              
power bi

NOTE: don’t forget to format in percentage

My dynamic measure:

power bi power bi

NOTE: as for option 1, if I want the “monthly variance” (to display them just above the month), I will use the same formula:

power bi

My coloring measure:

power bi

I will create my line and clustered column chart and 2 slicers:

power bi power bi power bi

Then configure the “y-axis” and the “second y-axis” like in option 1 (again, skip this configuration if you will use the “monthly variance”). This is the result:

power bi power bi

Interesting Topics