Re: Decision Trees

Power BI Team Sharon
Power BI Team

Decision Trees



With decision trees, you can visualize the probability of something you want to estimate, based on decision criteria from the historic data.

The decision tree classifier automatically finds the important decision criteria to consider.


Prerequisites (The sample .pbix files will not work without these prerequites completed)

1. Install R Engine

Power BI Desktop does not include, deploy or install the R engine. To run R scripts in Power BI Desktop, you must separately installR on your local computer. You can download and install R for free from many locations, including the Revolution Open download page, and the CRAN Repository.


2. Install the required R packages.

Download the R script attached to this message and run it to install all required packages on your local machine. 


Required R packages:

rpart, rpart.plot, RColorBrewer


Tested on:

CRAN 3.3.1 R, MRO 3.3.0, ,


Legal Disclaimers:

Terms of Service and Third Party Programs.



TimKroemer Frequent Visitor
Frequent Visitor

Re: Decision Trees

Dear Sharon


This is really excellent - my only question is whether there is a way to format (i) the colors, to make the output better fit in with the existing report and (ii) the formating of numbers in nodes? I am working on a loan portfolio to estimate the likelihood of default - but the loan amounts are presented in ln (XeY) format - it would be really helpful to amend these.


Regardless, this is an excellent contribution!





ironryan77 Member

Re: Decision Trees

This visual is excellent. 

  1. How can I convert the row counts from scientific notation to numeric? 
  2. How can I convert my dataset to method="Class"? 
  3. Is it possible to add Tooltips to these nodes using R-script?


Most of my code is from your sample PBIX file. 

Here is my screenshot.

decision tree2.jpg

Moderator boefraty

Re: Decision Trees

Hi TimKroemer ,

Sorry for the delay in reply: 

(i)the colors.

Answer: look for "default.palettes"  in R script and change it as you like 


(ii) the formating of numbers in nodes?
Answer:  prp {rpart.plot}  function has plenty of parameters.


Moderator boefraty

Re: Decision Trees

Hi ironryan77

1) Format it via parameters of prp function, may be you will need to reload "format" function 

2) Option 1: use characters instead of "numbers" for 1st column , Option 2: specify it explicitely as rpart(..., method ="class",...)

3) This visual is image, so tooltips are not possible. (We suport such functionality in  HTML-based R-powered Custom Visuals)

Elizabeth24 Frequent Visitor
Frequent Visitor

Re: Decision Trees

Hello Sharon,


I am just starting in the power bi world and I am trying to build a decision tree, my problem is the one in the file attached. The tree doesnt appear, it says "The tree depth is zero".


Am I doing something wrong?


Thanks in advance,



Elizabeth24 Frequent Visitor
Frequent Visitor

Re: Decision Trees

Hello Sharon,


I am having some troubles on trying to build a decision tree on power bi, I am quite new here so I think it should be something easy...


The error says : "The tree depth is zero"


Thanks in advance for your help,

Moderator boefraty

Re: Decision Trees

Hi @Elizabeth24


Sometimes algorithm "decides" that adding branches to root of the tree is not useful.

For example, if you have 90% negatives 10% positives in your data. The accuracy of root node is 0.9 and adding branches may not improve it much.  


You may try to disable cross validation. 

You may try to make changes in data. 

You may try to change some parameters, for example:


complexity   =  1e-05 #change  to 1e-10

minBucket = 2# change to 1

minRows  =  10 # change to 5

maxNumAttempts  =  10   #change to 50 



realexander Frequent Visitor
Frequent Visitor

Re: Decision Trees

Hello Sharon, 


Thank you for sharing this example. I have a question, I just tried the following short code for plotting a decision tree in Power BI: 



fit <- rpart(Kyphosis~Age+Number+Start,method="class",data=kyphosis)




That worked very well and the plot appeared to be OK in PBI Desktop, but once I published the file on Power BI service the plot changed a lot (attached file). 


Then I tried with your template and the plot is perfect on desktop and on PBI service. I noted some differences in the code like the function replaceFancyRpartPlot(). But I don't know exactly why the visual changed so much between desktop and the version published on the web. 


Can you give me a hand with this? 


Thanks in advance. 






Moderator boefraty

Re: Decision Trees

Hi @realexander

The differences in performance for  PBI desktop and service are due to different R engine (and packages) vesrions. 

Unfortunatly, we still have R3.2.2 in service, it is about to be upgraded next month.