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
amikm
Helper V
Helper V

Convert SQL into DAX

I am trying to convert the below SQL code to DAX , I am not able to get the same result as per SQL. Can we do this in single measure or do we need two measures and then subtract.

Also I am not able to use EXCEPT function in this case

SELECT Count(DISTINCT( A.AccountNumber)) FROM factTable F
JOIN dimAccount A ON F.AccountKey = A.AccountKey and F.DateSk = A.DateSk
JOIN [dimDate] D on F.DateSk = D.DateSk
WHERE D.IsLastDayOfMonth = 1 and D.DateKey > '2020-11-30' and D.DateKey <= '2021-11-30'
and ISNULL(F.Sale ,0) = 0
and F.IsDeletetd = 'False'
and A.IsDeletetd = 'False'

and A.AccountNumber
NOT IN(
SELECT DISTINCT(A.AccountNumber) FROM fact F
JOIN dimAccount A ON F.AccountKey = A.AccountKey and F.DateSk = A.DateSk
JOIN [dimDate] D on F.DateSk = D.DateSk
WHERE D.IsLastDayOfMonth = 1 and D.DateKey > '2020-11-30' and D.DateKey <= '2021-11-30'
and ISNULL(F.Sale ,0) > 0
and F.IsDeleted = 'False'
and A.Deleted = 'False'
)

1 ACCEPTED SOLUTION
Anonymous
Not applicable

Hi @amikm ,

 

To convert the SQL code to DAX, you can try the following approach:

 

Replace the FROM and JOIN clauses with a FILTER function that filters the rows in the relevant tables based on the conditions in the WHERE clause.

Replace the SELECT and COUNT clauses with a CALCULATE function that counts the distinct AccountNumber values using the DISTINCTCOUNT function.

Here's the resulting DAX measure:

 

 

Count of Distinct Account Numbers =

CALCULATE(

DISTINCTCOUNT(dimAccount[AccountNumber]),

FILTER(

factTable,

factTable[DateSk] = dimDate[DateSk]

&& dimDate[IsLastDayOfMonth] = 1

&& dimDate[DateKey] > "2020-11-30"

&& dimDate[DateKey] <= "2021-11-30"

&& ISBLANK(factTable[Sale])

&& factTable[IsDeleted] = FALSE

&& dimAccount[IsDeleted] = FALSE

),

FILTER(

dimAccount,

dimAccount[AccountKey] = factTable[AccountKey]

&& dimAccount[DateSk] = factTable[DateSk]

)

)

This measure should give you the same result as the SQL query.

 

Note: I replaced ISNULL(F.Sale ,0) = 0 with ISBLANK(factTable[Sale]) since DAX doesn't have an ISNULL function. In DAX, you can use the ISBLANK function to check if a value is NULL or an empty string.

 

I hope this helps! Let me know if you have any questions or if you need further assistance.

 

 

Best Regards,

Stephen Tao

 

If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

View solution in original post

5 REPLIES 5
donald-abhishek
New Member

Can You Please help me converting the following SQL to a DAX query, it will be a great help! 

``` 

WITH course_lesson_counts AS (
SELECT
cl.course_id,
COUNT(cl.lesson_id) AS total_lessons
FROM course_lessons cl
JOIN lessons l ON cl.lesson_id = l.id
WHERE cl.publish_map_id IS NOT NULL AND l.publish_map_id IS NOT NULL
GROUP BY cl.course_id
),
halfway_users AS (
SELECT
ucp.course_id,
ucp.user_id,
COUNT(clp.lesson_id) AS completed_lessons
FROM user_course_progress ucp
JOIN course_lesson_progress clp ON ucp.user_id = clp.user_id AND ucp.course_id IN (
SELECT course_id FROM course_lesson_counts
)
WHERE clp.progress_status IN ('in_progress', 'completed')
GROUP BY ucp.course_id, ucp.user_id
)
SELECT
c.id AS course_id,
c.name AS course_name,
COUNT(DISTINCT hu.user_id) AS halfway_user_count
FROM courses c
JOIN course_lesson_counts clc ON c.id = clc.course_id
LEFT JOIN halfway_users hu ON c.id = hu.course_id AND hu.completed_lessons >= (clc.total_lessons / 2)
WHERE c.publish_map_id IS NOT NULL
AND c.paid_type = 'paid'
GROUP BY c.id, c.name;
FSMS
Frequent Visitor

select m.id, count(distinct a.id) total_calls
from moment m
join moment_keywords k on k.moment_id = m.id
join moment_mapping mp on mp.moment_keyword_id = k.id
join transcription t on t.id = mp.transcription_id
join audio a on a.id = t.audio_id
where m.id = 215
group by m.id

can someone help me convert this into dax . I have a total of 5 tables starting from moment table until audio table and has many to one relationships between all of them.
im looking to find distinct count of callids where a single moment id=215 for a card visual.

Anonymous
Not applicable

Hi @amikm ,

 

To convert the SQL code to DAX, you can try the following approach:

 

Replace the FROM and JOIN clauses with a FILTER function that filters the rows in the relevant tables based on the conditions in the WHERE clause.

Replace the SELECT and COUNT clauses with a CALCULATE function that counts the distinct AccountNumber values using the DISTINCTCOUNT function.

Here's the resulting DAX measure:

 

 

Count of Distinct Account Numbers =

CALCULATE(

DISTINCTCOUNT(dimAccount[AccountNumber]),

FILTER(

factTable,

factTable[DateSk] = dimDate[DateSk]

&& dimDate[IsLastDayOfMonth] = 1

&& dimDate[DateKey] > "2020-11-30"

&& dimDate[DateKey] <= "2021-11-30"

&& ISBLANK(factTable[Sale])

&& factTable[IsDeleted] = FALSE

&& dimAccount[IsDeleted] = FALSE

),

FILTER(

dimAccount,

dimAccount[AccountKey] = factTable[AccountKey]

&& dimAccount[DateSk] = factTable[DateSk]

)

)

This measure should give you the same result as the SQL query.

 

Note: I replaced ISNULL(F.Sale ,0) = 0 with ISBLANK(factTable[Sale]) since DAX doesn't have an ISNULL function. In DAX, you can use the ISBLANK function to check if a value is NULL or an empty string.

 

I hope this helps! Let me know if you have any questions or if you need further assistance.

 

 

Best Regards,

Stephen Tao

 

If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

Ashish_Mathur
Super User
Super User

Hi,

Share some data, descibe the question and show the expected result.


Regards,
Ashish Mathur
http://www.ashishmathur.com
https://www.linkedin.com/in/excelenthusiasts/
ivana_tomekova
Advocate I
Advocate I

Do I understand it correctly, that you would like to have Distinct count of Account numbers, where there was no sale at the end of each month?

To give you any advice, it would be good if you would provide some more detailed information like: are the sales data in your fact table per day (so you need to summ them up to the last day of month)? What would be the filtering context in your report - data displayed per month?

In principle, it is always better to have a calculation done as early as possible... so if you can calculate it via SQL directly on e.g. SQL server level, it is better (from performance point of view) then having it calculated by DAX.

I think it would not be easy to give you a single reply... especially if I think that also the SQL does not make too much sense (e.g. Why you need to put the last WHERE condition (A.AccountNumber NOT IN( ...) ?...isn't ISNULL(F.Sale ,0) = 0 condition sufficient?)

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.