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

July 7 - July 17 | Round 2 of the Power BI Dataviz World Championships. Don't miss your chance! Learn more

Reply
Anonymous
Not applicable

SQL query to PowerQuery M language/code

Hello! I am looking to achieve similar SQL query in PowerQuery M language/code

 

I know its simple in DAX using 'CALCULATE' function but all the data needs to be imported to report

I am dealing with the huge amount of 'Factless Fact Table data' and can't really efford to load everything to the report which is exceeding the PowerBI PRO size limits

 

Tried DirectQuery but facing performance issues (my source is Azure MSSQL DB General Purpose: Gen5, 2 vCores). Any suggestions on performance tuning is also welcome

 

But I really need help on PowerQuery, appreciate in advance

 

EXMAPLE QUERY-1
SELECT
COUNT( DISTINCT Id ) AS Assigned,
COUNT( DISTINCT Id + IIF( Response=1, '1', null )) AS Processed,
COUNT( DISTINCT Id + IIF( Response=0 AND IsDuplicate=1, '1', null )) AS Duplicate
FROM TABLE WITH (NOLOCK)
WHERE Category='xyz' AND CustomerId=123 AND Type='I' AND Status IN (1,2,3) AND [Date] BETWEEN '2021-06-01' and '2021-06-02'

EXMAPLE QUERY-2
SELECT
COUNT( DISTINCT Id ) AS Assigned,
COUNT( DISTINCT CASE WHEN Response=1 THEN Id ELSE NULL END) AS Processed,
COUNT( DISTINCT CASE WHEN Response=0 AND IsDuplicate=1 THEN Id ELSE NULL END) AS Duplicate
FROM TABLE WITH (NOLOCK)
WHERE Category='xyz' AND CustomerId=123 AND Type='I' AND Status IN (1,2,3) AND [Date] BETWEEN '2021-06-01' and '2021-06-02'

 

Lastly, I have tried most of the Azure performance techniques and PowerQuery community for M code but am unlucky.

5 REPLIES 5
parry2k
Super User
Super User

@Anonymous I really don't know what you are trying to achieve and why PQ? Didn't @selimovd  reply solved that you run these queries directly with fixed value? Am I missing something in your question?



Subscribe to the @PowerBIHowTo YT channel for an upcoming video on List and Record functions in Power Query!!

Learn Power BI and Fabric - subscribe to our YT channel - Click here: @PowerBIHowTo

If my solution proved useful, I'd be delighted to receive Kudos. When you put effort into asking a question, it's equally thoughtful to acknowledge and give Kudos to the individual who helped you solve the problem. It's a small gesture that shows appreciation and encouragement! ❤


Did I answer your question? Mark my post as a solution. Proud to be a Super User! Appreciate your Kudos 🙂
Feel free to email me with any of your BI needs.

parry2k
Super User
Super User

@Anonymous or you can explore dynamic PQ Parameters that way you don't have to live with the fixed value data.

 

Dynamic M query parameters in Power BI Desktop (preview) - Power BI | Microsoft Docs

 

Check my latest blog post Comparing Selected Client With Other Top N Clients | PeryTUS  I would ❤ Kudos if my solution helped. 👉 If you can spend time posting the question, you can also make efforts to give Kudos to whoever helped to solve your problem. It is a token of appreciation!

Visit us at https://perytus.com, your one-stop-shop for Power BI-related projects/training/consultancy.



Subscribe to the @PowerBIHowTo YT channel for an upcoming video on List and Record functions in Power Query!!

Learn Power BI and Fabric - subscribe to our YT channel - Click here: @PowerBIHowTo

If my solution proved useful, I'd be delighted to receive Kudos. When you put effort into asking a question, it's equally thoughtful to acknowledge and give Kudos to the individual who helped you solve the problem. It's a small gesture that shows appreciation and encouragement! ❤


Did I answer your question? Mark my post as a solution. Proud to be a Super User! Appreciate your Kudos 🙂
Feel free to email me with any of your BI needs.

Anonymous
Not applicable

Thanks @parry2k , its a good option totally fits my situation and I am exploring it.

 

And I am still crazy to achieve this in PowerQuery M code for my knowledge, any help here? 🙂

selimovd
Most Valuable Professional
Most Valuable Professional

@Anonymous you can also just select your table without SQL statement and try to do the transformations in PowerQuery if you like the challenge

selimovd
Most Valuable Professional
Most Valuable Professional

Hey @Anonymous ,

 

why don't you use the SQL statement in DirectQuery mode?

selimovd_0-1624547335625.png

 

If you need any help please let me know.
If I answered your question I would be happy if you could mark my post as a solution ✔️ and give it a thumbs up 👍
 
Best regards
Denis
 

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.

60 days of Data Days Carousel

Data Days 2026

Join Data Days 2026: 60 days of free live/on-demand sessions, challenges, study groups, and certification opportunities.

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