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

The Power BI Data Visualization World Championships is back! Get ahead of the game and start preparing now! Learn more

Reply
Anonymous
Not applicable

Pass Sample size in SQL Query

I use the below SQL query from Power BI. Instead of manually giving in the sample size as 30, is there a way to dynamically pass the user input as the sample size.
for example, if I have a parameter, wher user can give in the number of rows, Can I pass this value inside the Advanced Editor as the sample size?

SELECT DISTINCT event_id, event_date

FROM my_table TABLESAMPLE(30 ROWS) REPEATABLE(1)

1 ACCEPTED SOLUTION

@Anonymous Sure, create a Text parameter. In my case I called mine Rows. Make sure it is Text. Then you can edit your query like the following:

let
    Rows = Rows,
    Source = Sql.Database("localhost\MSSQLSERVERDEV", "AdventureWorksDW2019", [Query="SELECT DISTINCT AccountKey, ParentAccountKey FROM DimAccount TABLESAMPLE(" & Rows & " ROWS) REPEATABLE(1)"])
in
    Source

So, the first line grabs the Rows parameter and put it in the query as a value called Rows. That is then used in the SQL Query by appending it inside the query.

 



Follow on LinkedIn
@ me in replies or I'll lose your thread!!!
Instead of a Kudo, please vote for this idea
Become an expert!: Enterprise DNA
External Tools: MSHGQM
YouTube Channel!: Microsoft Hates Greg
Latest book!:
DAX For Humans

DAX is easy, CALCULATE makes DAX hard...

View solution in original post

3 REPLIES 3
Greg_Deckler
Community Champion
Community Champion

@Anonymous Yes, you can use a parameter for that.



Follow on LinkedIn
@ me in replies or I'll lose your thread!!!
Instead of a Kudo, please vote for this idea
Become an expert!: Enterprise DNA
External Tools: MSHGQM
YouTube Channel!: Microsoft Hates Greg
Latest book!:
DAX For Humans

DAX is easy, CALCULATE makes DAX hard...
Anonymous
Not applicable

@Greg_Deckler  Could you please show how the parameter can be passed inside the SELECT query?

@Anonymous Sure, create a Text parameter. In my case I called mine Rows. Make sure it is Text. Then you can edit your query like the following:

let
    Rows = Rows,
    Source = Sql.Database("localhost\MSSQLSERVERDEV", "AdventureWorksDW2019", [Query="SELECT DISTINCT AccountKey, ParentAccountKey FROM DimAccount TABLESAMPLE(" & Rows & " ROWS) REPEATABLE(1)"])
in
    Source

So, the first line grabs the Rows parameter and put it in the query as a value called Rows. That is then used in the SQL Query by appending it inside the query.

 



Follow on LinkedIn
@ me in replies or I'll lose your thread!!!
Instead of a Kudo, please vote for this idea
Become an expert!: Enterprise DNA
External Tools: MSHGQM
YouTube Channel!: Microsoft Hates Greg
Latest book!:
DAX For Humans

DAX is easy, CALCULATE makes DAX hard...

Helpful resources

Announcements
Power BI DataViz World Championships

Power BI Dataviz World Championships

The Power BI Data Visualization World Championships is back! Get ahead of the game and start preparing now!

December 2025 Power BI Update Carousel

Power BI Monthly Update - December 2025

Check out the December 2025 Power BI Holiday Recap!

FabCon Atlanta 2026 carousel

FabCon Atlanta 2026

Join us at FabCon Atlanta, March 16-20, for the ultimate Fabric, Power BI, AI and SQL community-led event. Save $200 with code FABCOMM.