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

Calling all Data Engineers! Fabric Data Engineer (Exam DP-700) live sessions are back! Starting October 16th. Sign up.

Reply
reddevil
Helper II
Helper II

Datediff with multiple criteria

Hello Everyone,

 

I have a scenario where I want to calculate counts where dates and one of the column called Test Frequency is "Annually", "Biannieal"etc

Below is my dax which gives me incorrect counts. Please suggest where does the below measure go wrong.

 

Expected output below:

 

Test FrequencyTotal Overdue countIDLast Test Date
Annual?22 
Biennial?5558 
Quarterly?543252 
Every 18 Months?45245 

 

 

Total Overdue count = 
var startdate = MAXX(Test,(Test[Last Test Date]))
var enddate = TODAY()
RETURN
CALCULATE (
    IF (
        MAX( Test[Test Frequency] ) = "Annually"
            || DATEDIFF (  startdate , enddate, MONTH ) > -12,
        DISTINCTCOUNT ( Test[ID] ),
        IF (
            MAX( Test[Test Frequency] ) = "Biennial"
                || DATEDIFF ( startdate , enddate, MONTH ) > -24,
            DISTINCTCOUNT ( Test[ID] ),
            IF (
                MAX( Test[Test Frequency] ) = "Six Monthly"
                    || DATEDIFF (  startdate , enddate, MONTH ) > -6,
                DISTINCTCOUNT ( Test[ID] ),
                IF (
                    MAX( Test[Test Frequency] ) = "Quarterly"
                        || DATEDIFF (  startdate , enddate, MONTH) > -3,
                    DISTINCTCOUNT (Test[ID] ),
                    IF (
                        MAX( Test[Test Frequency] ) = "Every 18 Months"
                            || DATEDIFF (  startdate , enddate, MONTH ) > -18,
                        DISTINCTCOUNT ( Test[ID] ),
                        0
                    )
                )
            )
        )
    ),
    FILTER ( Test, Test[Status Name] = "Active" )
)

 

 

1 ACCEPTED SOLUTION
Anonymous
Not applicable

Hi @reddevil ,

Based on the information, try using the following DAX formula.

Total Overdue Count update = 
VAR StartDate = MAX(Test[Last Test Date])
VAR EndDate = TODAY()
VAR Frequency = MAX(Test[Test Frequency])
VAR MonthDiff = DATEDIFF(StartDate, EndDate, MONTH)
VAR OverdueThreshold =
    SWITCH(
        TRUE(),
        Frequency = "Annually", 12,
        Frequency = "Biennial", 24,
        Frequency = "Six Monthly", 6,
        Frequency = "Quarterly", 3,
        Frequency = "Every 18 Months", 18,
        BLANK()
    )
RETURN
    IF(
        NOT(ISBLANK( OverdueThreshold)) &&
        MonthDiff >= OverdueThreshold,
        DISTINCTCOUNT(Test[ID]),
        0
    )

Also, the example data you provided does not have the Test[ID] column and Test[Status Name] column in it.

 

Best Regards,

Wisdom Wu

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

7 REPLIES 7
Anonymous
Not applicable

Hi @reddevil ,

Based on the information, try using the following DAX formula.

Total Overdue Count update = 
VAR StartDate = MAX(Test[Last Test Date])
VAR EndDate = TODAY()
VAR Frequency = MAX(Test[Test Frequency])
VAR MonthDiff = DATEDIFF(StartDate, EndDate, MONTH)
VAR OverdueThreshold =
    SWITCH(
        TRUE(),
        Frequency = "Annually", 12,
        Frequency = "Biennial", 24,
        Frequency = "Six Monthly", 6,
        Frequency = "Quarterly", 3,
        Frequency = "Every 18 Months", 18,
        BLANK()
    )
RETURN
    IF(
        NOT(ISBLANK( OverdueThreshold)) &&
        MonthDiff >= OverdueThreshold,
        DISTINCTCOUNT(Test[ID]),
        0
    )

Also, the example data you provided does not have the Test[ID] column and Test[Status Name] column in it.

 

Best Regards,

Wisdom Wu

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

Thank you, this works.

danextian
Super User
Super User

Hi @reddevil 

Please provide a workable sample data (not an image), your expected result from the same sample data and your reasoning behind.





Dane Belarmino | Microsoft MVP | Proud to be a Super User!

Did I answer your question? Mark my post as a solution!


"Tell me and I’ll forget; show me and I may remember; involve me and I’ll understand."
Need Power BI consultation, get in touch with me on LinkedIn or hire me on UpWork.
Learn with me on YouTube @DAXJutsu or follow my page on Facebook @DAXJutsuPBI.
Ritaf1983
Super User
Super User

Hi @reddevil 

Please provide sample data that covers your issue or question completely, in a usable format (not as a screenshot).
https://community.powerbi.com/t5/Community-Blog/How-to-provide-sample-data-in-the-Power-BI-Forum/ba-...
Please show the expected outcome based on the sample data you provided.

https://community.powerbi.com/t5/Desktop/How-to-Get-Your-Question-Answered-Quickly/m-p/1447523

Regards,
Rita Fainshtein | Microsoft MVP
https://www.linkedin.com/in/rita-fainshtein/
Blog : https://www.madeiradata.com/profile/ritaf/profile

Test FrequencyTotal Overdue countLast Test DateNext Test DateNo of months old
Six Monthly116/12/2022 0:0016/05/2023 0:0026
Six Monthly421/08/2024 0:0031/07/2025 0:006
Six Monthly04/09/2024 0:0012/03/2025 0:005
Six Monthly020/11/2024 0:0020/05/2025 0:003
Quarterly44/06/2024 0:006/06/2025 0:008
Quarterly019/12/2024 0:0014/04/2025 0:002
Every 18 Months027/02/2024 0:0028/08/2025 0:0012
Biennial 1/08/2021 0:001/08/2023 0:0042
Biennial 30/09/2021 0:0030/09/2023 0:0041
Biennial 29/10/2021 0:0031/10/2023 0:0040
Biennial 31/10/2021 0:0031/10/2023 0:0040
Biennial 29/11/2021 0:00 39
Biennial 1/12/2021 0:001/12/2023 0:0038
Biennial 1/01/2022 0:001/01/2024 0:0037
Biennial 28/02/2022 0:0031/01/2024 0:0036
Biennial 28/02/2022 0:0028/02/2024 0:0036
Biennial23/03/2022 0:0028/02/2026 0:0035
Biennial 7/03/2022 0:0028/02/2024 0:0035
Biennial 10/03/2022 0:0028/03/2024 0:0035
Biennial 30/03/2022 0:0028/03/2024 0:0035
Biennial 31/03/2022 0:0031/03/2024 0:0035
Biennial 31/03/2022 0:001/04/2024 0:0035
Biennial231/03/2022 0:0031/03/2025 0:0035
rajendraongole1
Super User
Super User

Hi @reddevil - Please find the attached pbix file. i have modified the above formulae with correct datediff condition and max functions which is givng wrong results. check and confirm.





Did I answer your question? Mark my post as a solution!

Proud to be a Super User!





Thanks @rajendraongole1 Rajendra for responding, the below is the measure which doesnt display it correctly when I add the Date field. 

Test FrequencyTotal Overdue countLast Test DateNext Test DateNo of months old
Six Monthly116/12/2022 0:0016/05/2023 0:0026
Six Monthly421/08/2024 0:0031/07/2025 0:006
Six Monthly04/09/2024 0:0012/03/2025 0:005
Six Monthly020/11/2024 0:0020/05/2025 0:003
Quarterly44/06/2024 0:006/06/2025 0:008
Quarterly019/12/2024 0:0014/04/2025 0:002
Every 18 Months027/02/2024 0:0028/08/2025 0:0012
Biennial 1/08/2021 0:001/08/2023 0:0042
Biennial 30/09/2021 0:0030/09/2023 0:0041
Biennial 29/10/2021 0:0031/10/2023 0:0040
Biennial 31/10/2021 0:0031/10/2023 0:0040
Biennial 29/11/2021 0:00 39
Biennial 1/12/2021 0:001/12/2023 0:0038
Biennial 1/01/2022 0:001/01/2024 0:0037
Biennial 28/02/2022 0:0031/01/2024 0:0036
Biennial 28/02/2022 0:0028/02/2024 0:0036
Biennial23/03/2022 0:0028/02/2026 0:0035
Biennial 7/03/2022 0:0028/02/2024 0:0035
Biennial 10/03/2022 0:0028/03/2024 0:0035
Biennial 30/03/2022 0:0028/03/2024 0:0035
Biennial 31/03/2022 0:0031/03/2024 0:0035
Biennial 31/03/2022 0:001/04/2024 0:0035
Biennial231/03/2022 0:0031/03/2025 0:0035

Helpful resources

Announcements
FabCon Global Hackathon Carousel

FabCon Global Hackathon

Join the Fabric FabCon Global Hackathon—running virtually through Nov 3. Open to all skill levels. $10,000 in prizes!

September Power BI Update Carousel

Power BI Monthly Update - September 2025

Check out the September 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.

Top Solution Authors
Top Kudoed Authors