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

Earn the coveted Fabric Analytics Engineer certification. 100% off your exam for a limited time only!

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
v-stephen-msft
Community Support
Community Support

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

4 REPLIES 4
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.

v-stephen-msft
Community Support
Community Support

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

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
April AMA free

Microsoft Fabric AMA Livestream

Join us Tuesday, April 09, 9:00 – 10:00 AM PST for a live, expert-led Q&A session on all things Microsoft Fabric!

March Fabric Community Update

Fabric Community Update - March 2024

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