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
Anonymous
Not applicable

How to write this SQL query in DAX?

Hi,

Please can someone provide the DAX equivalent for the below SQL query?  Thanks in advance.

SELECT sv.packet_id
            , sv.syllabus
            , sv.session_id
            , COUNT(candidate_number) AS CountOfCandidates
            , SUM(
                      CASE sv.status
                               WHEN 'validated' THEN 1
                               WHEN 'validated_rejected' THEN 1
                               ELSE 0
                      END
                      ) AS Completed
            , SUM(
                      CASE sv.status
                               WHEN 'validated' THEN 0
                               WHEN 'validated_rejected' THEN 0
                               ELSE 1
                      END
                      ) AS ToBeCompleted
FROM SCRIPT_VALIDATION AS sv
GROUP BY sv.packet_id, sv.syllabus, sv.session_id

2 ACCEPTED SOLUTIONS
Anonymous
Not applicable

Thanks. That put me in the right direction.
In case anyone is interested to know how what the DAX equivalent for the SQL was, see below. I had slightly altered my requirement so the SQL and DAX won't be 100% matching but close enough for you to follow.

 

tblPacketsWithCandidatesBySession = SUMMARIZE(
                                                                                         refSCRIPT_VALIDATION,
                                                                                         refSCRIPT_VALIDATION[session_id],
                                                                                         refSCRIPT_VALIDATION[syllabus],
                                                                                         refSCRIPT_VALIDATION[component],
                                                                                         refSCRIPT_VALIDATION[packet_id],
                                                                                         "CountOfCandidates", COUNT( refSCRIPT_VALIDATION[id] ),
                                                                                         "CompletedCandidates", CALCULATE(
                                                                                                                                    COUNT( refSCRIPT_VALIDATION[id] ),
                                                                                                                                    FILTER( refSCRIPT_VALIDATION,
                                                                                                                                    refSCRIPT_VALIDATION[status] = "validated"
                                                                                                                                   || refSCRIPT_VALIDATION[status] =                                                                                                                                                                                                                           "validated_rejected")
                                                                                                                                                        ),
                                                                                          "ToBeCompletedCandidates", CALCULATE(
                                                                                                                                             COUNT( refSCRIPT_VALIDATION[id] ),
                                                                                                                                             FILTER( refSCRIPT_VALIDATION,
                                                                                                                                             refSCRIPT_VALIDATION[status] <> "validated"
                                                                                                                      && refSCRIPT_VALIDATION[status] <> "validated_rejected")
                                                                                                                                                        )
                                                                                         )

View solution in original post

Hardik
Continued Contributor
Continued Contributor

hey !! please accept the solution provided,if it helped you !

Thanks  

View solution in original post

3 REPLIES 3
Hardik
Continued Contributor
Continued Contributor

Anonymous
Not applicable

Thanks. That put me in the right direction.
In case anyone is interested to know how what the DAX equivalent for the SQL was, see below. I had slightly altered my requirement so the SQL and DAX won't be 100% matching but close enough for you to follow.

 

tblPacketsWithCandidatesBySession = SUMMARIZE(
                                                                                         refSCRIPT_VALIDATION,
                                                                                         refSCRIPT_VALIDATION[session_id],
                                                                                         refSCRIPT_VALIDATION[syllabus],
                                                                                         refSCRIPT_VALIDATION[component],
                                                                                         refSCRIPT_VALIDATION[packet_id],
                                                                                         "CountOfCandidates", COUNT( refSCRIPT_VALIDATION[id] ),
                                                                                         "CompletedCandidates", CALCULATE(
                                                                                                                                    COUNT( refSCRIPT_VALIDATION[id] ),
                                                                                                                                    FILTER( refSCRIPT_VALIDATION,
                                                                                                                                    refSCRIPT_VALIDATION[status] = "validated"
                                                                                                                                   || refSCRIPT_VALIDATION[status] =                                                                                                                                                                                                                           "validated_rejected")
                                                                                                                                                        ),
                                                                                          "ToBeCompletedCandidates", CALCULATE(
                                                                                                                                             COUNT( refSCRIPT_VALIDATION[id] ),
                                                                                                                                             FILTER( refSCRIPT_VALIDATION,
                                                                                                                                             refSCRIPT_VALIDATION[status] <> "validated"
                                                                                                                      && refSCRIPT_VALIDATION[status] <> "validated_rejected")
                                                                                                                                                        )
                                                                                         )

Hardik
Continued Contributor
Continued Contributor

hey !! please accept the solution provided,if it helped you !

Thanks  

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.