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
eddysern
Frequent Visitor

Modify self-generated SQL Query by visual components

Good morning, everybody.

In a DirectQuery report, using Performance Analyzer, the SQL statement (query) that PowerBI sends to the database to display the visuals has been captured. This query contains certain statements that affect performance and response time.

For example, for a stacked bar chart, use the CAST and TOPfunctions. If I test the query by running it directly in the database without modifying anything, it lasts approximately 2.55 seconds. If I modify the query by removing the CAST and TOP functions, the same dataset is obtained, but in a time of 0.37 seconds (almost 7 times faster).

Is it possible to modify the statement (or query SQL) that Power BI autogenerates and sends to the database to get the dataset and build the visual component?.

Thank you in advance for your feedback.

This is the query that self-generates. Runs in 2.55 seconds. Use TOP and CAST.
Select
TOP 1000001 "t1"." estado_cliente",
CAST(COUNT("t1"." codigo_cliente") AS BIGINT)
AS "a0"
FROM
(

SELECT
CAST("codigo_cliente" AS VARCHAR(4000))
AS "codigo_cliente",
CAST("estado_cliente" AS VARCHAR(4000))
AS "estado_cliente"
FROM
(
(select "$Table"." apellidos_cliente" as "apellidos_cliente",
"$Table"." nombres_cliente" as "nombres_cliente",
"$Table"." direccion_cliente" as "direccion_cliente"
from "Base_datos"." tabla_clientes" "$Table")
)
AS "t1"
)
AS "t1"
GROUP BY "t1"." estado_cliente"


If I remove the CAST and TOP functions. Runs in 0.37 seconds. Removing TOP and CAST.
Select
"t1"." estado_cliente",
COUNT("t1"." codigo_cliente")
AS "a0"
FROM
(

SELECT
"codigo_cliente"
AS "codigo_cliente",
"estado_cliente"
AS "estado_cliente"
FROM
(
(select "$Table"." apellidos_cliente" as "apellidos_cliente",
"$Table"." nombres_cliente" as "nombres_cliente",
"$Table"." direccion_cliente" as "direccion_cliente"
from "Base_datos"." tabla_clientes" "$Table")
)
AS "t1"
)
AS "t1"
GROUP BY "t1"." estado_cliente"

1 ACCEPTED SOLUTION
lbendlin
Super User
Super User

Raise this as an Idea on the ideas page.  Not currently possible to impact any of the folded queries.

View solution in original post

2 REPLIES 2
lbendlin
Super User
Super User

Raise this as an Idea on the ideas page.  Not currently possible to impact any of the folded queries.

Thank you very much @lbendlin . I will do so.

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.