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

Join us for an expert-led overview of the tools and concepts you'll need to become a Certified Power BI Data Analyst and pass exam PL-300. Register now.

Reply
Anonymous
Not applicable

Include Parameters within Power BI to Query a SQL Data Source

Hi,

 

I have a SQL database I'm using as a datasource.  The database is large and I would like to include two separate parameters within Power BI in an effort to query the database down to size.  I need these parameters because the SQL database is pulling in records for many different lines of business.  The parameters I create in Power BI will allow me to specify what lines of business I'm including in Power BI.  This will allow me to reuse what I create in Power BI over and over again for different business lines.

 

I've already linked the SQL database properly to Power BI and I have data.

I've already created two separate parameters in Power BI, 1) Underwriter # and 2) Group #:

 

1.PNG

 

I am struggling to incorporate these two parameters in with the existing 'Get Data' script/syntax Power BI generated:

 

2.PNG

 

I do not know where I should include my two Where clauses...where I can specify the specific 1) Underwriter #(SQL) and 2) Group #(SQL) criterias.  Both fields in SQL are set to text and I made both Parameters are set to recognize text as well.

 

I think the alteration/syntax edit should include something like this:  WHERE PUNBR = '"&Underwriter #(SQL)&'" AND GRNBR = '"&Group #(SQL)&"'

 

...but I'm not really sure.  I've never attempted to do this before.

 

In case it helps: 

1) PUNBR is the field in the SQL Database I've referred to as Underwriter #(SQL)

2) GRNBR is the field in the SQL Database I've referred to as Group #(SQL)

 

 

 

Thank you for the help!

1 ACCEPTED SOLUTION
smpa01
Super User
Super User

@Anonymous 

 

let's suppose your

server = x

database = y

table name = z

sample sql query is (as you would put it on SSMS)

 

select *
from z
WHERE [period_number]=2
AND [fiscal_year]=2020

 

when you put this on a M

 

it becomes this

 

 

let
    Source = Sql.Database("x", "y", [Query="select *#(lf)from zs#(lf)WHERE [period_number]=2#(lf)AND [fiscal_year]=2020"])
in
    Source

 

 

 

Now if you want to pass two parameters to the where clauses as P1 and P2, (P1=2020, P2=2)make sure they are text first

you can create a custom query as below

 

 

(x as text, y as text)=>let
    Source = Sql.Database("x", "y", [Query="select *#(lf)from z#(lf)WHERE [period_number]="&P2&"#(lf)AND [fiscal_year]="&P1&""])
in
    Source

 

 

 

Did I answer your question? Mark my post as a solution!
Proud to be a Super User!
My custom visualization projects
Plotting Live Sound: Viz1
Beautiful News:Viz1, Viz2, Viz3
Visual Capitalist: Working Hrs

View solution in original post

10 REPLIES 10
Anonymous
Not applicable

Solved the issue:

 

Correct M:

 

let
Source = Sql.Database("D01PSQLENTA01", "ECI",
[Query="SELECT * #(lf)FROM [NovaDailyEligibilityExtract] #(lf)WHERE PUNBR = '"&UnderwriterSQL&"'#(lf)AND GRNBR = '"&GroupSQL&"'"])
in Source

great ...GRNBR has probably varchar as the column data type in native DB.

 

you would normally query in the native DB as

AND GBNBR]='06N'

I am glad you managed to adapt it to your solution.

Did I answer your question? Mark my post as a solution!
Proud to be a Super User!
My custom visualization projects
Plotting Live Sound: Viz1
Beautiful News:Viz1, Viz2, Viz3
Visual Capitalist: Working Hrs
smpa01
Super User
Super User

@Anonymous 

 

let's suppose your

server = x

database = y

table name = z

sample sql query is (as you would put it on SSMS)

 

select *
from z
WHERE [period_number]=2
AND [fiscal_year]=2020

 

when you put this on a M

 

it becomes this

 

 

let
    Source = Sql.Database("x", "y", [Query="select *#(lf)from zs#(lf)WHERE [period_number]=2#(lf)AND [fiscal_year]=2020"])
in
    Source

 

 

 

Now if you want to pass two parameters to the where clauses as P1 and P2, (P1=2020, P2=2)make sure they are text first

you can create a custom query as below

 

 

(x as text, y as text)=>let
    Source = Sql.Database("x", "y", [Query="select *#(lf)from z#(lf)WHERE [period_number]="&P2&"#(lf)AND [fiscal_year]="&P1&""])
in
    Source

 

 

 

Did I answer your question? Mark my post as a solution!
Proud to be a Super User!
My custom visualization projects
Plotting Live Sound: Viz1
Beautiful News:Viz1, Viz2, Viz3
Visual Capitalist: Working Hrs
Anonymous
Not applicable

Thank you for the help.

 

I tried implementing what you suggested and updated my M accordingly but it errored out.  Here is my current M that is erroring out:

 

33333.PNG

 

(D01PSQLENTA01 as text, ECI as text)=>let
Source = Sql.Database("D01PSQLENTA01", "ECI",
[Query="select *#(lf)from NovaDailyEligibilityExtract#(lf)WHERE [PUNBR]="&Underwriter #(SQL)&"#(lf)AND [GRNBR]="&Group #(SQL)&""]
in
dbo_NovaDailyEligibilityExtract

 

Error:

4444.PNG

 

Both of my created parameters are set as text:

 

555.PNG   6666.PNG

 

I'm sure my M is incorrect here.

Anonymous
Not applicable

I noticed I had several issues with the M code.  I made those small changes but still have an error.

 

Current M:

(D01PSQLENTA01 as text, ECI as text)=>let
Source = Sql.Database("D01PSQLENTA01", "ECI",
[Query="select *#(lf)from dbo_NovaDailyEligibilityExtract#(lf)WHERE [PUNBR]="&UnderwriterSQL&"#(lf)AND [GRNBR]="&GroupSQL&""])
in
dbo_NovaDailyEligibilityExtract

 

888.PNG

 

I also updated the names of my parameters to UnderwriterSQL and GroupSQL.

Change last line --- in Source

Did I answer your question? Mark my post as a solution!
Proud to be a Super User!
My custom visualization projects
Plotting Live Sound: Viz1
Beautiful News:Viz1, Viz2, Viz3
Visual Capitalist: Working Hrs
Anonymous
Not applicable

Alrighty.  Making progress here and thank you for walking me through this.

 

So with the last edit you mentioned it ran but I made one change.  I changed the =>let line from:

 

(D01PSQLENTA01 as text, ECI as text)=>let

 

to

 

(UnderwriterSQL as text, GroupSQL as text)=>let

 

I think this is correct?  We were previously listing the served and database as text.  We are now listing both perameters as text.

 

With that out of the way (I think), my new M is as follows:

 

(UnderwriterSQL as text, GroupSQL as text)=>let
Source = Sql.Database("D01PSQLENTA01", "ECI",
[Query="select *#(lf)from dbo_NovaDailyEligibilityExtract#(lf)WHERE [PUNBR]="&UnderwriterSQL&"#(lf)AND [GRNBR]="&GroupSQL&""])
in Source

 

I now have this as the interface:  

 

1313.PNG

 

However, when I enter 201 for UnderwriterSQL and 06N for GroupSQL I now get this error:

 

14.PNG

 

16.PNG

 

Am I setting the correct fields as text in the (UnderwriterSQL as text, GroupSQL as text)=>let line?  Is that the issue?  

 

 

 

Can you change the parameter name to parameter1 or parameter#1 from parameter # 1
Did I answer your question? Mark my post as a solution!
Proud to be a Super User!
My custom visualization projects
Plotting Live Sound: Viz1
Beautiful News:Viz1, Viz2, Viz3
Visual Capitalist: Working Hrs
pdelia
Advocate I
Advocate I

The simplest way to do this is to use the UI to filter the your fields and select any single value. The resulting Filter step can then easily be edited, where you replace the string of the single value you selected with your parameter. The step would look something like if you use the UI to filter a field.

 

Table.SelectRows(dbo_NovaDailyEligibilityExtract, each [PUNBR] = "123" )

 

And you would alter it to

 

Table.SelectRows(dbo_NovaDailyEligibilityExtract, each [PUNBR] = #"Underwriter #(#)(SQL) (201)")

 

Power Query will convert this into SQL statements on the backend, you can right click on the step and click "View Native Query" to see this.

Anonymous
Not applicable

I don't know what you mean by "use the UI to filter".  I googled it and I've gone in to settings and "Enabled it.":

 

3.PNG

 

Do you mean to simply go in to Edit Query and add a step where I filter for the two criterias I need?

 

I do not know what the 'UI' is you are referring to??

Helpful resources

Announcements
Join our Fabric User Panel

Join our Fabric User Panel

This is your chance to engage directly with the engineering team behind Fabric and Power BI. Share your experiences and shape the future.

June 2025 Power BI Update Carousel

Power BI Monthly Update - June 2025

Check out the June 2025 Power BI update to learn about new features.

June 2025 community update carousel

Fabric Community Update - June 2025

Find out what's new and trending in the Fabric community.