Forum Discussion

reddevil's avatar
reddevil
Icon for Helper II rankHelper II
1 year ago
Solved

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" )
)

 

 

  • Anonymous's avatar
    Anonymous
    1 year ago

    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.

7 Replies

  • 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.

    • reddevil's avatar
      reddevil
      Icon for Helper II rankHelper II

      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
  • Anonymous's avatar
    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.

    • reddevil's avatar
      reddevil
      Icon for Helper II rankHelper II
      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
  • Hi reddevil 

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