Forum Discussion

amikm's avatar
amikm
Helper V
3 years ago
Solved

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 ...
  • Anonymous's avatar
    Anonymous
    3 years ago

    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.