Power BI: display a variance line in a chart

For some charts, I like to see a straight line that calculate the difference between 2 values and showing the increase or decrease:

power bi

I will explain 2 ways to do it, although both versions give the same result, option 1 will show an error message but in spite of that, it works. This article explains how to do it with columns but if you are looking with measures, read Power BI: create an interactive comparison chart.

Option 1

First I will create a column with this formula:

IF(MAX([argument1])>MAX([argument2]),MAX([argument1])*1.3,MAX([argument2])*1.3)

power bi

NOTE:

  • Format it into “whole number”
  • Change “argument” by yours and for the formula below, also “table”
  • Change 1.3 if you want the line higher or lower

And 2 measures. The first one:


var xnum1=SUM('table'[argument1])
var xnum2=SUM('table'[argument2])
var xdiff=xnum2-xnum1
Return
SWITCH(
    TRUE(),
    xdiff=0,IF(NOT(ISBLANK(xnum2)),xdiff),
    xdiff>0,IF(NOT(ISBLANK(xnum2)),"▲ +"&xdiff),
    xdiff<=0,IF(NOT(ISBLANK(xnum2)),"▼ "&xdiff)
)
              
power bi

The second one:


var xnum1=SUM('Table'[argument1])
var xnum2=SUM('Table'[argument2])
var xdiff=xnum2-xnum1
Return
SWITCH(
    TRUE(),
    xdiff>0,"#1AAB40",
    xdiff<=0,"#D64554"
)
              

NOTE: optional, change the color code #1AAB40 (green) and #D64554 (red) by yours

power bi

I will create my line and clustered column chart:

power bi power bi

I will configure it, first, in “secondary y-axis”, click 2 times on “values”, it will make disappear the information on the right of the chart:

power bi power bi

NOTE: it is “off” but in fact it is “on”, a kind of bug that may be corrected in the next update

I will remove the line by turning “off”:

power bi

I will turn on the “data labels”:

  • In “apply settings to”, select my column and in “position (line)”, select “above”
    power bi
  • In “value”, click on the “field” to select the first “measure” and click on the “fx” of “color” to select the second “measure 2”
power bi power bi power bi

Once done, the chart will look like this:

power bi

Everything look OK but if I select my chart, I can see this icon with this message:

power bi

In spite of that, as I said in the beginning, it works but if it is annoyed you, use the option 2.

Option 2

Everything is the same except for the first “measure”, since I will not use it, I need to create a new one:

MAX('table'[argument])

power bi

In the “format” field, change “general” to “dynamic” and in the formula bar, put this formula for “format”:


var xnum1=SUM('table'[argument1])
var xnum2=SUM('table'[argument2])
var xdiff=xnum2-xnum1
var result=
SWITCH(
    TRUE(),
    xdiff=0,IF(NOT(ISBLANK(xnum2)),xdiff),
    xdiff>0,IF(NOT(ISBLANK(xnum2)),"▲ +"&xdiff),
    xdiff<=0,IF(NOT(ISBLANK(xnum2)),"▼ "&xdiff)
)
Return
""""&result
              
power bi

NOTE: by clicking on “format”, I can switch with “measure”

Now I will create my chart but this time, instead to use “column” for the line, I will use the new measure “measure 3” (left picture) and in “value”, I will just click on the “fx” color to select “measure 2” as in option 1 (right picture):

power bi power bi
power bi

Before to end, if you prefer to see this variance just above the corresponding column like that:

power bi

For option 1, the “column” formula:

IF([argument1]>[argument2],[ argument1]*1.3,[argument2]*1.3)

power bi

For option 2, the “measure 3” formula:


var xnum1=SUM('Table'[argument1])
var xnum2=SUM('Table'[argument2])
var xvalue=MAX(xnum1,xnum2)
Return
xvalue*1.3
              
power bi

NOTE: “format” of “measure 3” is the same formula as option 1, only “measure” of “measure 3” needs to be changed

Interesting Topics