Forum Discussion

Anonymous's avatar
Anonymous
Not applicable
5 years ago
Solved

SQL to DAX convertion

Hi, everybody!

Need help from experienced DAX users in converting SQL to DAX. 

SQL query provided below.

I'm just starting learning DAX, so far this looks incredibly confusing to me(

 

WITH CTE_ALL_OOS AS
(
SELECT

OOS.INN,
OOS.MONTH,
sum(OOS.ACB) as sum_ACB,
sum(OOS.oosales1) as sum_oosales1,
sum(OOS.oosales2) as sum_oosales2,
sum(OOS.oosales3) as sum_oosales3,
sum(OOS.oosales4) as sum_oosales4

FROM

VW_F_CRPT_OOSALES as OOS
GROUP BY OOS.INN,
OOS.MONTH

)

SELECT

OOS.INN,
OOS.MONTH,
case

when

OOS.sum_ACB > 0

or

OOS.sum_oosales1 > 0

or

OOS.sum_oosales2 > 0

or

sum_oosales3 > 0 then 0

when

sum_oosales4 > 0 then 1

else 0
end as oosales4

FROM

CTE_ALL_OOS as OOS

 

Thanks for your help!

  • Hi Anonymous

     

    Here is the output:

     

    The pbix file is attached, please try it.

     

    Best Regards,

    Link

     

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

5 Replies

  • DouweMeer's avatar
    DouweMeer
    Impactful Individual

    What I prefer to use is the expression SELECTCOLUMNS.

    SELECT
    A.Field1
    A.Field2
    FROM
    Table A
    WHERE
    A.Field1 = 'A'

    Becomes 

    SELECTCOLUMNS
       filter ( 'table'
          'table'[Field1] = "A"
          )
       , "Field1" , [Field1]
       , "Field2" , [Field2]
       )

     You can use a combination of Union, Generate and Naturalinnerjoin to create a different context.

  • v-xulin-mstf's avatar
    v-xulin-mstf
    Community Support

    Hi Anonymous,

     

    Could you provide sample data or screenshot of tables?

    So that i can sure about the names of tables and columns.

     

    Best Regard,

    Link

    • Anonymous's avatar
      Anonymous
      Not applicable

      Hi, v-xulin-mstf ,

      Thank you a lot for your interest. I am very grateful for all advice and help.
      Screenshot with sample raw data below.

       

       

      Kind Regards,

      Alina

      • v-xulin-mstf's avatar
        v-xulin-mstf
        Community Support

        Hi Anonymous

         

        Here is the output:

         

        The pbix file is attached, please try it.

         

        Best Regards,

        Link

         

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