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

Join us at FabCon Atlanta from March 16 - 20, 2026, for the ultimate Fabric, Power BI, AI and SQL community-led event. Save $200 with code FABCOMM. Register now.

Reply
Sri57
Frequent Visitor

DAX Query

I have created a dax query to get count of names, considering both the current fiscal year and potentially the previous fiscal year if no data exists for the current year.please find the following dax.

Adequate_FYTD_Audit =
VAR CurrentDate = MAX('Date'[Fiscal Year ID])
VAR result=
         CALCULATE(CALCULATE(
            COUNT('Audit'[ENGAGEMENT_NAME]),
                FILTER(
                    ALLEXCEPT('Audit','Audit'[ENGAGEMENT_NAME]),
                   'Audit'[RATING]="Adequate"&&
                'Audit'[FISCAL_YEAR_ID] = (CurrentDate)
        )),
        CROSSFILTER('Date'[Date],'Audit'[ENGAGEMENT_PLANNED_START],None))  
VAR max_notblank=CALCULATE(CALCULATE(MAX('Audit'[FISCAL_YEAR_ID]),FILTER(ALLSELECTED(Audit),'Audit'[FISCAL_YEAR_ID]<CurrentDate&&'Audit'[FISCAL_YEAR_ID]<>BLANK()))
,CROSSFILTER('Date'[Date],Audit[ENGAGEMENT_PLANNED_START],None))
VAR result1=CALCULATE(
        CALCULATE(
                COUNT('Audit'[ENGAGEMENT_NAME]),
                    FILTER(
                        ALLEXCEPT('Audit','Audit'[ENGAGEMENT_NAME]),
                    Audit[RATING]="Adequate"&&
                    'Audit'[FISCAL_YEAR_ID] =max_notblank                   
        )),CROSSFILTER('Date'[Date],Audit[ENGAGEMENT_PLANNED_START],none))
RETURN
      IF(result=BLANK(),
      result1,result)
Also we have Session column in Audit and Date tables, it contains Session1, Session2 and Session3.I use Seesion column as a slicer from Date table, here when I select Session1 and Fiscal year 2023/24 and there is no data for Session1 in 2023/24, I want most recent year of selected year and selected session i.e., if there is data foe 2022/23 in session 1 then it should show count of engagement_nmae where rating is "Adequate".
Sameway if I select Session2 from year 2022/23 and there is no data to display, then it should check for previous year same session i.e., session2 year 2021/22, if there is no data then check for session2 2020/21 like wise check untill there is data for session 2 and retrieve count of engagement_name.
I have written the below logic for session selection, but I'm not getting count if there is no data. I'm not getting where exactly I missed.
Adequate_FYTD_Audit _Session=
VAR CurrentDate = MAX('Date'[Fiscal Year ID])
VAR SelectedSession = SELECTEDVALUE('Audit'[Session])

VAR result=
         CALCULATE(CALCULATE(
            COUNT('Audit'[ENGAGEMENT_NAME]),
                FILTER(
                    ALLEXCEPT('Audit','Audit'[ENGAGEMENT_NAME]),
                   'Audit'[RATING]="Adequate"&& 
'Audit'[Session] = (SelectedSession)&&
                'Audit'[FISCAL_YEAR_ID] = (CurrentDate)
        )),
        CROSSFILTER('Date'[Date],'Audit'[ENGAGEMENT_PLANNED_START],None))  
VAR max_notblank=CALCULATE(CALCULATE(MAX('Audit'[FISCAL_YEAR_ID]),FILTER(ALLSELECTED(Audit),'Audit'[FISCAL_YEAR_ID]<CurrentDate&&'Audit'[Session] = (SelectedSession)&&'Audit'[FISCAL_YEAR_ID]<>BLANK()))
,CROSSFILTER('Date'[Date],Audit[ENGAGEMENT_PLANNED_START],None))
VAR result1=CALCULATE(
        CALCULATE(
                COUNT('Audit'[ENGAGEMENT_NAME]),
                    FILTER(
                        ALLEXCEPT('Audit','Audit'[ENGAGEMENT_NAME]),
                    Audit[RATING]="Adequate"&&
                    'Audit'[FISCAL_YEAR_ID] =max_notblank                   
        )),CROSSFILTER('Date'[Date],Audit[ENGAGEMENT_PLANNED_START],none))
RETURN
      IF(result=BLANK(),
      result1,result)
Please help me by correcting my dax.

TIA
1 ACCEPTED SOLUTION
Anonymous
Not applicable

Hi  @Sri57 ,

 

Try to modify your formula like below:

 

Adequate_FYTD_Audit_Session = 
VAR CurrentDate = MAX('Date'[Fiscal Year ID])
VAR SelectedSession = SELECTEDVALUE('Date'[Session])

VAR result = 
    CALCULATE(
        COUNT('Audit'[ENGAGEMENT_NAME]),
        'Audit'[RATING] = "Adequate" && 
        'Audit'[Session] = SelectedSession && 
        'Audit'[FISCAL_YEAR_ID] = CurrentDate
    )

VAR max_notblank = 
    CALCULATE(
        MAX('Audit'[FISCAL_YEAR_ID]),
        FILTER(
            ALL('Audit'), 
            'Audit'[FISCAL_YEAR_ID] < CurrentDate && 
            'Audit'[Session] = SelectedSession && 
            NOT ISBLANK('Audit'[FISCAL_YEAR_ID])
        )
    )

VAR result1 = 
    CALCULATE(
        COUNT('Audit'[ENGAGEMENT_NAME]),
        'Audit'[RATING] = "Adequate" && 
        'Audit'[Session] = SelectedSession && 
        'Audit'[FISCAL_YEAR_ID] = max_notblank
    )

RETURN
    IF(ISBLANK(result), result1, result)

vkongfanfmsft_1-1720405825758.png

vkongfanfmsft_0-1720405806680.png

 

Best Regards,
Adamk Kong

 

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

2 REPLIES 2
Anonymous
Not applicable

Hi  @Sri57 ,

 

Try to modify your formula like below:

 

Adequate_FYTD_Audit_Session = 
VAR CurrentDate = MAX('Date'[Fiscal Year ID])
VAR SelectedSession = SELECTEDVALUE('Date'[Session])

VAR result = 
    CALCULATE(
        COUNT('Audit'[ENGAGEMENT_NAME]),
        'Audit'[RATING] = "Adequate" && 
        'Audit'[Session] = SelectedSession && 
        'Audit'[FISCAL_YEAR_ID] = CurrentDate
    )

VAR max_notblank = 
    CALCULATE(
        MAX('Audit'[FISCAL_YEAR_ID]),
        FILTER(
            ALL('Audit'), 
            'Audit'[FISCAL_YEAR_ID] < CurrentDate && 
            'Audit'[Session] = SelectedSession && 
            NOT ISBLANK('Audit'[FISCAL_YEAR_ID])
        )
    )

VAR result1 = 
    CALCULATE(
        COUNT('Audit'[ENGAGEMENT_NAME]),
        'Audit'[RATING] = "Adequate" && 
        'Audit'[Session] = SelectedSession && 
        'Audit'[FISCAL_YEAR_ID] = max_notblank
    )

RETURN
    IF(ISBLANK(result), result1, result)

vkongfanfmsft_1-1720405825758.png

vkongfanfmsft_0-1720405806680.png

 

Best Regards,
Adamk Kong

 

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

Sahir_Maharaj
Super User
Super User

Hello @Sri57,

 

Can you please try the following:

 

Adequate_FYTD_Audit

Adequate_FYTD_Audit =
VAR CurrentDate = MAX('Date'[Fiscal Year ID])
VAR result =
    CALCULATE(
        COUNT('Audit'[ENGAGEMENT_NAME]),
        FILTER(
            ALLEXCEPT('Audit', 'Audit'[ENGAGEMENT_NAME]),
            'Audit'[RATING] = "Adequate" &&
            'Audit'[FISCAL_YEAR_ID] = CurrentDate
        ),
        CROSSFILTER('Date'[Date], 'Audit'[ENGAGEMENT_PLANNED_START], None)
    )
VAR max_notblank =
    CALCULATE(
        MAX('Audit'[FISCAL_YEAR_ID]),
        FILTER(
            ALLSELECTED('Audit'),
            'Audit'[FISCAL_YEAR_ID] < CurrentDate &&
            NOT(ISBLANK('Audit'[FISCAL_YEAR_ID]))
        ),
        CROSSFILTER('Date'[Date], 'Audit'[ENGAGEMENT_PLANNED_START], None)
    )
VAR result1 =
    CALCULATE(
        COUNT('Audit'[ENGAGEMENT_NAME]),
        FILTER(
            ALLEXCEPT('Audit', 'Audit'[ENGAGEMENT_NAME]),
            'Audit'[RATING] = "Adequate" &&
            'Audit'[FISCAL_YEAR_ID] = max_notblank
        ),
        CROSSFILTER('Date'[Date], 'Audit'[ENGAGEMENT_PLANNED_START], None)
    )
RETURN
    IF(ISBLANK(result), result1, result)

 

Adequate_FYTD_Audit_Session

Adequate_FYTD_Audit_Session =
VAR CurrentDate = MAX('Date'[Fiscal Year ID])
VAR SelectedSession = SELECTEDVALUE('Date'[Session])
VAR result =
    CALCULATE(
        COUNT('Audit'[ENGAGEMENT_NAME]),
        FILTER(
            ALLEXCEPT('Audit', 'Audit'[ENGAGEMENT_NAME]),
            'Audit'[RATING] = "Adequate" &&
            'Audit'[Session] = SelectedSession &&
            'Audit'[FISCAL_YEAR_ID] = CurrentDate
        ),
        CROSSFILTER('Date'[Date], 'Audit'[ENGAGEMENT_PLANNED_START], None)
    )
VAR max_notblank =
    CALCULATE(
        MAX('Audit'[FISCAL_YEAR_ID]),
        FILTER(
            ALLSELECTED('Audit'),
            'Audit'[FISCAL_YEAR_ID] < CurrentDate &&
            'Audit'[Session] = SelectedSession &&
            NOT(ISBLANK('Audit'[FISCAL_YEAR_ID]))
        ),
        CROSSFILTER('Date'[Date], 'Audit'[ENGAGEMENT_PLANNED_START], None)
    )
VAR result1 =
    CALCULATE(
        COUNT('Audit'[ENGAGEMENT_NAME]),
        FILTER(
            ALLEXCEPT('Audit', 'Audit'[ENGAGEMENT_NAME]),
            'Audit'[RATING] = "Adequate" &&
            'Audit'[Session] = SelectedSession &&
            'Audit'[FISCAL_YEAR_ID] = max_notblank
        ),
        CROSSFILTER('Date'[Date], 'Audit'[ENGAGEMENT_PLANNED_START], None)
    )
RETURN
    IF(ISBLANK(result), result1, result)

Hope this helps!


Did I answer your question? Mark my post as a solution, this will help others!

If my response(s) assisted you in any way, don't forget to drop me a "Kudos" 🙂

Kind Regards,
Sahir Maharaj
Data Scientist | Data Engineer | Data Analyst | AI Engineer
P.S. Want me to build your Power BI solution? (Yes, its FREE!)
➤ Lets connect on LinkedIn: Join my network of 15K+ professionals
➤ Join my free newsletter: Data Driven: From 0 to 100
➤ Website: https://sahirmaharaj.com
➤ About: https://sahirmaharaj.com/about.html
➤ Email: sahir@sahirmaharaj.com
➤ Want me to build your Power BI solution? Lets chat about how I can assist!
➤ Join my Medium community of 30K readers! Sharing my knowledge about data science and artificial intelligence
➤ Explore my latest project (350K+ views): Wordlit.net
➤ 100+ FREE Power BI Themes: Download Now
LinkedIn Top Voice in Artificial Intelligence, Data Science and Machine Learning

Helpful resources

Announcements
October Power BI Update Carousel

Power BI Monthly Update - October 2025

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

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.