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
vini_udenia
Helper I
Helper I

Sub Query - How to write DAX or handle in Power Query

I have the requirement to exclude certain schools from a measure calcuation. I have been able to define the logic in a sub-query, not sure how can this be converted to a DAX. 

SELECT DISTINCT SE.[YEAR], SE.[MONTH], SC.[CD_SCH], COUNT (F.[SK_CAND]) #Registration

FROM [dbo].[TBL_DM_FCT_SUB_COMP] F, [dbo].[TBL_DM_DIM_SCH] SC,[dbo].[TBL_DM_DIM_SESS] SE, [dbo].[TBL_DM_DIM_SUB] SU

WHERE F.[SK_SCH] = SC.[SK_SCH]

                AND F.[SK_SESS] = SE.[SK_SESS]

                AND F.[SK_SUB] = SU.[SK_SUB]

               AND SU.[TXT_SUB] like '%PERS P%'

               AND F.[WITHDRAWN_IND] = 'N' 

-- Excludes any schools found by the code below

                AND not exists

                (                                           SELECT DISTINCT SC2.[CD_SCH]

                                           FROM [dbo].[TBL_DM_FCT_SUB_COMP] F2, [dbo].[TBL_DM_DIM_SCH] SC2,[dbo].[TBL_DM_DIM_SESS] SE2, [dbo].[TBL_DM_DIM_SUB] SU2

                                           WHERE F2.[SK_SCH] = SC2.[SK_SCH]

                                                            AND F2.[SK_SESS] = SE2.[SK_SESS]

                                                           AND F2.[SK_SUB] = SU2.[SK_SUB]

                                                           AND SU2.[TXT_SUB] not like '%PERS P%'  -- Subject registrations which aren’t personal project

                                                           AND F2.[WITHDRAWN_IND] = 'N'

                                                           AND (F2.[SK_SESS] = F.[SK_SESS] or F2.[SK_SESS] = F.[SK_SESS] -1 ) -- Where it is the same session as selected in the first part of the statement or is the previous session.

                                                          AND SC2.[CD_SCH] = SC.[CD_SCH]

                                           GROUP BY  SC2.[CD_SCH]

              )

GROUP BY SE.[YEAR], SE.[MONTH], SC.[CD_SCH]

2 REPLIES 2
MahyarTF
Memorable Member
Memorable Member

Hi,

From my point of view, it will be a bit complicated in Dax, but I usually create a separate query for the excluded part, then create a separate table as a Left Anti Join option :

1- Table A

 MahyarTF_0-1669365302942.png

2- Table B

MahyarTF_2-1669365351091.png

3- Merge table :

MahyarTF_3-1669365467801.png

Appreciate your Kudos and please make it as a solution if it helps

Mahyartf

Thank you for your advise, the Left Anti Join definitely solves part of the requirement. 
The 2nd part in which the subquery need to join based on the SK_SESS and SK_SESS -1 of the main query is still quite puzzling for me. 

 

Highlighted the portion: 

SELECT DISTINCT SE.[YEAR], SE.[MONTH], SC.[CD_SCH], COUNT (F.[SK_CAND]) #Registration
FROM [dbo].[TBL_DM_FCT_SUB_COMP] F, [dbo].[TBL_DM_DIM_SCH] SC,[dbo].[TBL_DM_DIM_SESS] SE, [dbo].[TBL_DM_DIM_SUB] SU
WHERE F.[SK_SCH] = SC.[SK_SCH]
                AND F.[SK_SESS] = SE.[SK_SESS]
                AND F.[SK_SUB] = SU.[SK_SUB]
               AND SU.[TXT_SUB] like '%PERS P%'
               AND F.[WITHDRAWN_IND] = 'N' 
-- Excludes any schools found by the code below
                AND not exists
                (                                           SELECT DISTINCT SC2.[CD_SCH]
                                           FROM [dbo].[TBL_DM_FCT_SUB_COMP] F2, [dbo].[TBL_DM_DIM_SCH] SC2,[dbo].[TBL_DM_DIM_SESS] SE2, [dbo].[TBL_DM_DIM_SUB] SU2
                                           WHERE F2.[SK_SCH] = SC2.[SK_SCH]
                                                            AND F2.[SK_SESS] = SE2.[SK_SESS]
                                                           AND F2.[SK_SUB] = SU2.[SK_SUB]
                                                           AND SU2.[TXT_SUB] not like '%PERS P%'  -- Subject registrations which aren’t personal project
                                                           AND F2.[WITHDRAWN_IND] = 'N'
                                                           AND (F2.[SK_SESS] = F.[SK_SESS] or F2.[SK_SESS] = F.[SK_SESS] -1 ) -- Where it is the same session as selected in the first part of the statement or is the previous session.
                                                          AND SC2.[CD_SCH] = SC.[CD_SCH]
                                           GROUP BY  SC2.[CD_SCH]
              )
GROUP BY SE.[YEAR], SE.[MONTH], SC.[CD_SCH]

Helpful resources

Announcements
November Power BI Update Carousel

Power BI Monthly Update - November 2025

Check out the November 2025 Power BI update to learn about new features.

Fabric Data Days Carousel

Fabric Data Days

Advance your Data & AI career with 50 days of live learning, contests, hands-on challenges, study groups & certifications and more!

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.

Top Kudoed Authors