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

Powerquery seemingly merging lines by itself when using SQL connection

Hello all, I've run into a weird issue and I have no clue if it is a bug or some other config I messed up.

 

I have my PBI connected to a DB using ODBC, everything looks fine, however the past 7 days I've noticed some of my numbers were off and then I started searching for the culprit. I've realized that for some reason PBI is merging some lines into a single one, seemingly at random and the numbers do not add up so that is causing the issue I had observed.

 

To make things clearer here is an exerpt of my sales table inside my DB 

talleslessa_0-1615233694377.png

Highlighted, on 2021-02-19 I have 7 sales of the same item from different stores (the columns that matter from left to right are salesdate; store_id; internalproductid; salesquantity; grosssale;netsales;grosscost) being 2 sales made from the same store ( 04 0048).

 

However after importing the data to PBI Here is what I end up with.

talleslessa_2-1615234048415.png

 

Just 4 lines for some reason  two of the sales (54.99) merged into one sale of (109.98) and the quantity (qtdven) merged as well being shown as 2 units sold. Aside from that I have 3 other sales merging into a huge (141.98) sale, however, the quantity this time stayed as a single item. 

 

I am not well versed into SQL but I believe the SQL statement ( used in powerquery to import the data) has nothing out of the ordinary to cause this behavior

 

SELECT
R.salesdate AS data,
R.internalproductid AS CODPRO,
R.salesquantity AS QTDVEN,
R.grosscost AS CUSTO,
R.returnquantity AS DEVOLUCAO_CUSTO,
sum(R.netsales - R.netsalereturn) AS VENDA_LIQ,
sum(R.grosssale - R.grosssalereturn) AS VENDA_BRUT
FROM resultado_vendas_pbi R
where 1=1
AND R.segment IN ('1000','9000','10000')
GROUP BY --R.CODLOJA,TO_DATE(R.MES,'YYYY-MM-DD'),B1_PROC,R.CODPRO
R.salesdate,
R.internalproductid,
R.salesquantity,
R.grosscost,
R.returnquantity
ORDER BY
R.salesdate

 

And it happens to other items as well, the sales always add up however the quantity sold does not (as we could see from the example above).

 

Does any one have a clue on why/ what is happening ?

 

 

 

 

1 ACCEPTED SOLUTION
edhans
Super User
Super User

You may want to ask this in a SQL forum since you are using a SQL statement directly. I recommend against using direct SQL statements in that advanced window. Instead I recommend:

  1. Do your transformations in Power Query. It will fold many of the SQL statements back to the server so the server does the work anyway.
  2. Create a view in SQL and connect to that. Power Query treats tables and views the same, so you can easily continue to fold statements after the final code in the View.

Power Query isn't doing any summarizing of your data unless you are asking it to. Missing records might be suspect, but records added together, correctly from a math standpoint, would be something your code is asking it to do.



Did I answer your question? Mark my post as a solution!
Did my answers help arrive at a solution? Give it a kudos by clicking the Thumbs Up!

DAX is for Analysis. Power Query is for Data Modeling


Proud to be a Super User!

MCSA: BI Reporting

View solution in original post

2 REPLIES 2
Anonymous
Not applicable

Indeed the SQL statment used in powerquery was grouping some values and causing the issue.

edhans
Super User
Super User

You may want to ask this in a SQL forum since you are using a SQL statement directly. I recommend against using direct SQL statements in that advanced window. Instead I recommend:

  1. Do your transformations in Power Query. It will fold many of the SQL statements back to the server so the server does the work anyway.
  2. Create a view in SQL and connect to that. Power Query treats tables and views the same, so you can easily continue to fold statements after the final code in the View.

Power Query isn't doing any summarizing of your data unless you are asking it to. Missing records might be suspect, but records added together, correctly from a math standpoint, would be something your code is asking it to do.



Did I answer your question? Mark my post as a solution!
Did my answers help arrive at a solution? Give it a kudos by clicking the Thumbs Up!

DAX is for Analysis. Power Query is for Data Modeling


Proud to be a Super User!

MCSA: BI Reporting

Helpful resources

Announcements
July PBI25 Carousel

Power BI Monthly Update - July 2025

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

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 community update carousel

Fabric Community Update - June 2025

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