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

July 28 - August 9 | Final Round of the Power BI Dataviz World Championships. This is your chance. Learn more

Reply
Anonymous
Not applicable

IF statement in Rscript / null variables in Rscript

Hi,

I want to perform a regression analysis with R script. I used mtcars dataset as test data. My regression formula is basically: lmod<-lm(mpg~cyl+wt+hp)

 

Capture.PNGAlso wanted to enable users to choose which variables to use as y (predicted) variable. To do so created a parameter as YVariable which works on a list query consists of column (variable) names. So predicted variable is set via parameter.

Note: I put a null row in list query. 

 

If YVVariable(mpg) formula is;

 

lmod<-lm("&Text.From(YVariable)&"~cyl+wt+hp,mtcars)

lmod<-lm(mpg~cyl+wt+hp,mtcars)

 

And it works!

 

 

My next challange is to do the same thing for independent variables (such as cyl, wt, hp ...)

To do so I created parameters called Independent1,2,3...

 

lmod<-lm("&Text.From(YVariable)&"~"&Text.From(Independent1)&"+"&Text.From(Independent2)&"+"&Text.From(Independent3)&",mtcars)

 

In this case if 

Independent1(hp)

Independent1(wt)

Independent1(cyl) formula will be lmod<-lm(mpg~cyl+wt+hp)

 

But I want to be able to set the formula as lmod<-lm(mpg~cyl+wt) (without hp) In this case Independent3() should be null.

 

But if one of the Independent parameters is null - Independent1() - BI returns following error,

Expression.Error: We cannot convert the value null to type Text.
Details:
Value=
Type=TypeCapture3.PNG

 

What could be Done? Any ideas?

Thanks!

1 REPLY 1
mwegener
Most Valuable Professional
Most Valuable Professional

Hi @Anonymous ,

 

can you provide your pbix file?

Did I answer your question?
Please mark my post as solution, this will also help others.
Please give Kudos for support.

Marcus Wegener works as Full Stack Power BI Engineer at BI or DIE.
His mission is clear: "Get the most out of data, with Power BI."
twitter - LinkedIn - YouTube - website - podcast - Power BI Tutorials


Helpful resources

Announcements
FabCon and SQLCon Barcelona 2026

FabCon & SQLCon – Barcelona 2026

Join us in Barcelona for FabCon and SQLCon, the Fabric, Power BI, SQL, and AI community event. Save €200 with code FABCMTY200.

Fabric Community Sticker Design Challenge Barcelona Carousel

Fabric Community Sticker Challenge - Barcelona 2026

If you love stickers, then you will definitely want to check out our community sticker challenge, Barcelona edition!

July Power BI Update Carousel

Power BI Monthly Update - July 2026

Check out the July 2026 Power BI update to learn about new features.

Power BI DataViz World Championships carousel

Power BI DataViz World Championships - June 2026

A new Power BI DataViz World Championship is coming this June! Don't miss out on submitting your entry.

Top Solution Authors