10 Replies Latest reply on Aug 31, 2018 10:17 AM by Jonathan Basirico

# 'What If?' analysis using Monte Carlo method

I haven't seen any templates or discussions regarding Monte Carlo usage in Tableau which is a shame!

I really nifty tool is to combine R (or SPSS or SAS) with Tableau to produce a predictive model, which enables users to perform advanced what if-scenarios.

I have created a Tableau template and a R script to populate the Tableau workbook that I would be glad to share if there are any interest.

Regards,

//Mårten

• ###### 1. Re: 'What If?' analysis using Monte Carlo method

I'd love to see it. There is a lot of interest in adding R support to Tableau. You might vote on the feature and add your use case example here: Integrate with R Statistics | Tableau Support Community.

Tableau 8 also allows you to create / edit Tableau extracts with Python (or C or Java), which might be another workaround to get advanced stats in Tableau. Maybe someone could write an R library in C for R to read, write extracts.

• ###### 2. Re: 'What If?' analysis using Monte Carlo method

Alright, I'll just generalize the code and I'll upload it in the next few days.

• ###### 3. Re: 'What If?' analysis using Monte Carlo method

Marten,

Please do!  I'm sure allot of us will find this useful.  If the community adapts it, Tableau will follow.

-Gerardo

• ###### 4. Re: 'What If?' analysis using Monte Carlo method

Hi Marten!

I am trying to create a Monte Carlo simulation in Tableau as well. Would you be able to share your template with me?

Thanks!

Saken

• ###### 5. Re: 'What If?' analysis using Monte Carlo method

Marten, do you still have the workbook you could share? Would highly appreciate it!

• ###### 6. Re: 'What If?' analysis using Monte Carlo method

Hello Marten:

Any chance you still have the workbook available to share? It would be greatly appreciated, as I'm trying to get a monte carlo simulation running in Tableau currently.

• ###### 7. Re: 'What If?' analysis using Monte Carlo method

Hi Marten,

It would great if you post your Tableau template, because I am trying to create a Monte Carlo Simulation within Tableau.

• ###### 8. Re: 'What If?' analysis using Monte Carlo method

Hi All

I am trying to create a Monte Carlo simulation in Tableau as well.

Would you be able to share your template with me?

In my case I have output as - "Market Size in Million" and inputs are - "Avg Price , Penetration , Reuse , Units "

and relation is - Market Size in Million = Avg Price *  Penetration * Reuse * Units

What i tried as of now is I have created "What if analysis" . In which I have used parameter in which use can set value say - What will be Market in size for 5% increase in Avg Price. eg Avg Price = 100 after 5% increment it will be 105.

But I want use correct approach to apply simulations on this.

Kindly help and let me know your thoughts.

Sumeet

• ###### 9. Re: 'What If?' analysis using Monte Carlo method

Hi Marten,

Are you able to share you workbook that you developed?  Would love to see it in practice.

Cheers

Drake

• ###### 10. Re: 'What If?' analysis using Monte Carlo method

I have come up with a way to do a Monte Carlo simulation in R using Tableau. There are really only two main issues:

1.) The script call to R from Tableau only returns a single value so any Monte Carlo outcomes are reduced to a single value (e.g., mean of the outcomes).

2.) The random Monte Carlo variables will be regenerated each time the call to R is made. For example, if I wanted to compare the mean of the Monte Carlo outcomes to the max of the Monte Carlo outcomes using two different calculated fields in Tableau, then I would actually be comparing two different simulations rather than the mean and max of the same simulation. This is because two separate calls to R are being made, and each script has a separate simulation.

The way to address those issues is to:

Step 1: Use the Tableau calculation to check if Monte Carlo random variables have been created. (Use the "exists()" R code).

Step 2: If the random variables are not yet created, then create/store them in an R variable.

Step 3: If the random variables are already created, perform the calculation on those variables.

Step 4: Include this check in every Tableau calculation that refers to the Monte Carlo random variables.

Example Tableau calculation (.arg1, .arg2, and .arg3 are parameters for n, mean, and sd):

script_real(

'if (exists("tab_sim")==FALSE) {

tab_sim = rnorm(.arg1,mean = .arg2,sd=.arg3)

mean(tab_sim)

} else {

mean(tab_sim)

}'

, attr([n]),attr([mean]), attr([sd])

)

Jonathan