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

Register now to learn Fabric in free live sessions led by the best Microsoft experts. From Apr 16 to May 9, in English and Spanish.

Reply
Hyuna_8000
Frequent Visitor

Derive open defects from the past using Dax

Hi,

I have a requirement to calculate open defects in the past months based on the following sample datasets. Notes that all three tables are pulled from SQL server DB, "All defect" table is the original table, "New" and "Closed" defect count tables are summarised tables based on "All defect" table using power query.

 

All defects (existing table)

Defect NumberDate LoggedClosed DateStatus
Def0014/01/2022 Work in progress
Def00210/01/2022 Ready
Def00311/01/202229/03/2022Closed
Def00411/01/20221/03/2022Closed
Def00512/01/2022 Work in progress
Def00613/01/202231/01/2022Closed
Def0071/02/202228/04/2022Closed
Def0082/02/2022 Ready
Def0092/02/2022 Ready
Def0103/02/20224/04/2022Closed
Def0114/02/2022 Work in progress
Def0127/02/2022 Assigned
Def0138/02/202228/02/2022Closed
Def0141/03/20225/04/2022Closed
Def0151/03/202228/04/2022Closed
Def0162/03/2022 Ready
Def01723/03/202224/03/2022Closed
Def01824/03/2022 Ready
Def01925/03/202227/04/2022Closed
Def0204/04/202213/04/2022Closed
Def0215/04/2022 Ready
Def02211/04/202226/04/2022Closed
Def02312/04/202229/04/2022Closed
Def02429/04/2022 Ready
Def02529/04/2022 Ready
Def0262/05/2022 Work in progress
Def0273/05/2022 Ready
Def0284/05/2022 Ready
Def0295/05/20226/05/2022Closed
Def0309/05/2022 Ready

 

Count of new defects every month (existing table, I use end of month to represent the count from the entire month)

Date LoggedNew Defect Count
31/01/20226
28/02/20227
31/03/20226
30/04/20226
10/05/20225

 

Count of closed defects every month (existing table)

Closed DateClosed Defect Count
31/01/20221
28/02/20221
31/03/20223
30/04/20228
31/05/20221

 

In this case, the definition of "Open defects" means any defects that is still in "Assigned" or "Work in progress" or "Ready" status. Based on this, I can calculate the current open defects as of now, which is 16 according to the sample data.

 

And from here, I want to go backward and calculate open defect from past months. The logic of deriving open defects from past months should be:

 

Current open defects (as of today 10th May 2022) =16

Open defects in Apr 2022= 16 + Closed defects in May 2022 - New defects in May 2022

Open defects in Mar 2022= Open defects in Apr 2022 + Closed defects in Apr 2022 - New defects in Apr 2022

Open defects in Feb 2022= Open defects in Mar 2022 + Closed defects in Mar 2022 - New defects in Mar 2022

Open defects in Jan 2022= Open defects in Feb 2022 + Closed defects in Feb 2022 - New defects in Feb 2022

Open defects in Dec 2021= Open defects in Jan 2022 + Closed defects in Jan 2022 - New defects in Jan 2022

 

I would like to have a dynamic table using DAX, meaning although now is May, I am expecting in the future I can see open defects from May, Apr etcs in June, and open defects from June, May in July etcs..

 

Output: Open defects by months

MonthOpen Defect Counts
Current (as of 10052022)16
30/04/202212
31/03/202214
28/02/202211
31/01/20225
31/12/20210
....
....

 

Not sure how complicated this can be, I am still learning DAX and struggling, any ideas would be greatly appreciated. Thanks

2 ACCEPTED SOLUTIONS
Jihwan_Kim
Super User
Super User

Hi,

I am not sure if I understood your question correctly, but please check the below attached pbix file.

 

 

 

Count Open Defect: = 
IF (
    HASONEVALUE ( 'Calendar'[Month Name] ),
    IF (
        MIN ( 'Calendar'[Date] ) <= TODAY (),
        COUNTROWS (
            FILTER (
                Data,
                Data[Date Logged] <= MAX ( 'Calendar'[Date] )
                    && OR (
                        ISBLANK ( Data[Closed Date] ),
                        Data[Closed Date] > MAX ( 'Calendar'[Date] )
                    )
            )
        )
    )
)

 

 

If this post helps, then please consider accepting it as the solution to help other members find it faster, and give a big thumbs up.


Go to My LinkedIn Page


View solution in original post

tamerj1
Super User
Super User

HI @Hyuna_8000 
Other way of doing that using SUMX https://www.dropbox.com/t/Wdk5XtFV5jyu7cBd

Open Defect Counts = 
VAR LastDateInPeriod = MAX ( 'Date'[Date] )
RETURN
    SUMX (
        Defects,
        VAR StartDate = Defects[Date Logged]
        VAR EndDate = Defects[Closed Date]
        RETURN 
            IF ( 
                StartDate <= LastDateInPeriod 
                    && OR ( ISBLANK ( EndDate ), EndDate > LastDateInPeriod ),
                1
            )
    )

1.png

View solution in original post

3 REPLIES 3
Hyuna_8000
Frequent Visitor

Thanks @tamerj1 and @Jihwan_Kim . Both of the DAX formulas return the correct value.

tamerj1
Super User
Super User

HI @Hyuna_8000 
Other way of doing that using SUMX https://www.dropbox.com/t/Wdk5XtFV5jyu7cBd

Open Defect Counts = 
VAR LastDateInPeriod = MAX ( 'Date'[Date] )
RETURN
    SUMX (
        Defects,
        VAR StartDate = Defects[Date Logged]
        VAR EndDate = Defects[Closed Date]
        RETURN 
            IF ( 
                StartDate <= LastDateInPeriod 
                    && OR ( ISBLANK ( EndDate ), EndDate > LastDateInPeriod ),
                1
            )
    )

1.png

Jihwan_Kim
Super User
Super User

Hi,

I am not sure if I understood your question correctly, but please check the below attached pbix file.

 

 

 

Count Open Defect: = 
IF (
    HASONEVALUE ( 'Calendar'[Month Name] ),
    IF (
        MIN ( 'Calendar'[Date] ) <= TODAY (),
        COUNTROWS (
            FILTER (
                Data,
                Data[Date Logged] <= MAX ( 'Calendar'[Date] )
                    && OR (
                        ISBLANK ( Data[Closed Date] ),
                        Data[Closed Date] > MAX ( 'Calendar'[Date] )
                    )
            )
        )
    )
)

 

 

If this post helps, then please consider accepting it as the solution to help other members find it faster, and give a big thumbs up.


Go to My LinkedIn Page


Helpful resources

Announcements
Microsoft Fabric Learn Together

Microsoft Fabric Learn Together

Covering the world! 9:00-10:30 AM Sydney, 4:00-5:30 PM CET (Paris/Berlin), 7:00-8:30 PM Mexico City

PBI_APRIL_CAROUSEL1

Power BI Monthly Update - April 2024

Check out the April 2024 Power BI update to learn about new features.

April Fabric Community Update

Fabric Community Update - April 2024

Find out what's new and trending in the Fabric Community.

Top Solution Authors