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
Chetanab
Microsoft Employee
Microsoft Employee

calculate active cases and slice it based on other columns

I have the below table. I need to  calculate active cases by end of month and have it work when I use a slicer with other columns ( for eg: term column)

IDCreateDateStatus change datechangeDateEOMStatusTerm
13-Jan-221/12/202231-Jan-22ActiveShort Term
25-Jan-221/13/202231-Jan-22ActiveShort Term
25-Jan-222/2/202228-Feb-22ResolvedMedium Term
35-Jan-221/5/202231-Jan-22ActiveShort Term
35-Jan-222/20/202228-Feb-22ResolvedShort Term
45-Jan-221/5/202231-Jan-22ActiveShort Term
45-Jan-222/3/202228-Feb-22ResolvedShort Term
45-Jan-223/3/202231-Mar-22ActiveLong Term
55-Jan-221/5/202231-Jan-22ActiveMedium Term
55-Jan-222/3/202228-Feb-22ResolvedMedium Term
55-Jan-223/3/202231-Mar-22ResolvedMedium Term
65-Jan-222/3/202228-Feb-22ActiveShort Term
65-Jan-223/3/202231-Mar-22ResolvedShort Term

 

I need to obtain the term value of these Active IDs as of end of month date - 

expected result

End of monthShort TermMedium TermLong Term
31-Jan-22510
28-Feb-22200
31-Mar-22101


as of 31-jan (all IDs were active)=> IDs 1,2,3,4,6 were short term and ID 5 was medium term
As of 28 feb=> Active IDs 1&6 are short term
as of 31 mar=> active IDs 1 was short term, 4 was long term

Please help. 

1 ACCEPTED SOLUTION
Anonymous
Not applicable

Hi @Chetanab ,

 

Here I have two ways to achieve your goal.

Way 1. Create a calculated table and then get result based on this new table.

Expand Table = 
VAR _BASIC =
    GENERATE (
        SUMMARIZE ( 'Table', 'Table'[ID], 'Table'[CreateDate] ),
        VALUES ( 'Table'[changeDateEOM] )
    )
VAR _ADD1 =
    ADDCOLUMNS (
        _BASIC,
        "Status change date",
            CALCULATE (
                MAX ( 'Table'[Status change date] ),
                FILTER (
                    'Table',
                    'Table'[ID] = EARLIER ( [ID] )
                        && YEAR ( 'Table'[Status change date] ) * 100
                            + MONTH ( 'Table'[Status change date] )
                            = YEAR ( EARLIER ( [changeDateEOM] ) ) * 100
                                + MONTH ( EARLIER ( [changeDateEOM] ) )
                )
            )
    )
VAR _ADD2 =
    ADDCOLUMNS (
        _ADD1,
        "Fill",
            VAR _MIN =
                MINX (
                    FILTER (
                        _ADD1,
                        [ID] = EARLIER ( [ID] )
                            && [changeDateEOM] > EARLIER ( [changeDateEOM] )
                    ),
                    [Status change date]
                )
            VAR _MAX =
                MAXX (
                    FILTER (
                        _ADD1,
                        [ID] = EARLIER ( [ID] )
                            && [changeDateEOM] < EARLIER ( [changeDateEOM] )
                    ),
                    [Status change date]
                )
            VAR _RESULT1 =
                IF ( ISBLANK ( [Status change date] ), _MIN, [Status change date] )
            RETURN
                IF ( _RESULT1 = BLANK (), _MAX, _RESULT1 )
    )
VAR _ADD3 =
    ADDCOLUMNS (
        _ADD2,
        "Status",
            CALCULATE (
                MAX ( 'Table'[Status] ),
                FILTER (
                    'Table',
                    'Table'[ID] = EARLIER ( [ID] )
                        && 'Table'[Status change date] = EARLIER ( [Fill] )
                )
            ),
        "Term",
            CALCULATE (
                MAX ( 'Table'[Term] ),
                FILTER (
                    'Table',
                    'Table'[ID] = EARLIER ( [ID] )
                        && 'Table'[Status change date] = EARLIER ( [Fill] )
                )
            )
    )
RETURN
    _ADD3

Measure:

Measure = 
CALCULATE(COUNT('Expand Table'[ID]),'Expand Table'[Status]<>"Resolved")

 

Way2. Create this table as vritual table in measure and then use dim Eom and Term table to get result.

Create two unrelated Dimtables.

Term = VALUES('Table'[Term])
EOM = VALUES('Table'[changeDateEOM])

Measure:

Measure 2 = 
VAR _BASIC =
    GENERATE (
        SUMMARIZE (ALL( 'Table'), 'Table'[ID], 'Table'[CreateDate] ),
        CALCULATETABLE( VALUES ( 'Table'[changeDateEOM] ),ALL('Table'))
    )
VAR _ADD1 =
    ADDCOLUMNS (
        _BASIC,
        "Status change date",
            CALCULATE (
                MAX ( 'Table'[Status change date] ),
                FILTER (
                    'Table',
                    'Table'[ID] = EARLIER ( [ID] )
                        && YEAR ( 'Table'[Status change date] ) * 100
                            + MONTH ( 'Table'[Status change date] )
                            = YEAR ( EARLIER ( [changeDateEOM] ) ) * 100
                                + MONTH ( EARLIER ( [changeDateEOM] ) )
                )
            )
    )
VAR _ADD2 =
    ADDCOLUMNS (
        _ADD1,
        "Fill",
            VAR _MIN =
                MINX (
                    FILTER (
                        _ADD1,
                        [ID] = EARLIER ( [ID] )
                            && [changeDateEOM] > EARLIER ( [changeDateEOM] )
                    ),
                    [Status change date]
                )
            VAR _MAX =
                MAXX (
                    FILTER (
                        _ADD1,
                        [ID] = EARLIER ( [ID] )
                            && [changeDateEOM] < EARLIER ( [changeDateEOM] )
                    ),
                    [Status change date]
                )
            VAR _RESULT1 =
                IF ( ISBLANK ( [Status change date] ), _MIN, [Status change date] )
            RETURN
                IF ( _RESULT1 = BLANK (), _MAX, _RESULT1 )
    )
VAR _ADD3 =
    ADDCOLUMNS (
        _ADD2,
        "Status",
            CALCULATE (
                MAX ( 'Table'[Status] ),
                FILTER (
                    'Table',
                    'Table'[ID] = EARLIER ( [ID] )
                        && 'Table'[Status change date] = EARLIER ( [Fill] )
                )
            ),
        "Term",
            CALCULATE (
                MAX ( 'Table'[Term] ),
                FILTER (
                    'Table',
                    'Table'[ID] = EARLIER ( [ID] )
                        && 'Table'[Status change date] = EARLIER ( [Fill] )
                )
            )
    )
RETURN
COUNTAX(FILTER(_ADD3,[changeDateEOM] = MAX(EOM[changeDateEOM]) && [Term] = MAX(Term[Term]) && [Status] <>"Resolved"),[ID])

Result is as below.

RicoZhou_0-1651744482766.png

 

Best Regards,
Rico Zhou

 

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

5 REPLIES 5
philouduv
Resolver III
Resolver III

Hey @Chetanab ,

An other has this problem : 

Solved: counting rows based on multiple dates - Microsoft Power BI Community

The context is a bit different but tell me if you can adapt it

If not I will be more than happy to help.

Best regards

ps: Do not hesitate to create a table for with each month and add the column long short and medium term for the expect output

yes, the context is different. Also the terms are not defined. An ID can have any of the 3 term values. Can you please look into the below DAX I have and suggest where I am going wrong?

# Active =
VAR __eom = MAXEoM[Date] )
Var _st =
COUNTROWS(
FILTER(
CALCULATETABLEVALUESSheet1[ID] ), Sheet1[CreateDate] <= __eom ),
CALCULATE(
LASTNONBLANKVALUESheet1[Status change date]MAXSheet1[Status] ) ),
Sheet1[Status change date] <= __eom,ALLEXCEPT(sheet1,Sheet1[Term])
) <> "Resolved"
)
)
return _st
Anonymous
Not applicable

Hi @Chetanab ,

 

Here I have two ways to achieve your goal.

Way 1. Create a calculated table and then get result based on this new table.

Expand Table = 
VAR _BASIC =
    GENERATE (
        SUMMARIZE ( 'Table', 'Table'[ID], 'Table'[CreateDate] ),
        VALUES ( 'Table'[changeDateEOM] )
    )
VAR _ADD1 =
    ADDCOLUMNS (
        _BASIC,
        "Status change date",
            CALCULATE (
                MAX ( 'Table'[Status change date] ),
                FILTER (
                    'Table',
                    'Table'[ID] = EARLIER ( [ID] )
                        && YEAR ( 'Table'[Status change date] ) * 100
                            + MONTH ( 'Table'[Status change date] )
                            = YEAR ( EARLIER ( [changeDateEOM] ) ) * 100
                                + MONTH ( EARLIER ( [changeDateEOM] ) )
                )
            )
    )
VAR _ADD2 =
    ADDCOLUMNS (
        _ADD1,
        "Fill",
            VAR _MIN =
                MINX (
                    FILTER (
                        _ADD1,
                        [ID] = EARLIER ( [ID] )
                            && [changeDateEOM] > EARLIER ( [changeDateEOM] )
                    ),
                    [Status change date]
                )
            VAR _MAX =
                MAXX (
                    FILTER (
                        _ADD1,
                        [ID] = EARLIER ( [ID] )
                            && [changeDateEOM] < EARLIER ( [changeDateEOM] )
                    ),
                    [Status change date]
                )
            VAR _RESULT1 =
                IF ( ISBLANK ( [Status change date] ), _MIN, [Status change date] )
            RETURN
                IF ( _RESULT1 = BLANK (), _MAX, _RESULT1 )
    )
VAR _ADD3 =
    ADDCOLUMNS (
        _ADD2,
        "Status",
            CALCULATE (
                MAX ( 'Table'[Status] ),
                FILTER (
                    'Table',
                    'Table'[ID] = EARLIER ( [ID] )
                        && 'Table'[Status change date] = EARLIER ( [Fill] )
                )
            ),
        "Term",
            CALCULATE (
                MAX ( 'Table'[Term] ),
                FILTER (
                    'Table',
                    'Table'[ID] = EARLIER ( [ID] )
                        && 'Table'[Status change date] = EARLIER ( [Fill] )
                )
            )
    )
RETURN
    _ADD3

Measure:

Measure = 
CALCULATE(COUNT('Expand Table'[ID]),'Expand Table'[Status]<>"Resolved")

 

Way2. Create this table as vritual table in measure and then use dim Eom and Term table to get result.

Create two unrelated Dimtables.

Term = VALUES('Table'[Term])
EOM = VALUES('Table'[changeDateEOM])

Measure:

Measure 2 = 
VAR _BASIC =
    GENERATE (
        SUMMARIZE (ALL( 'Table'), 'Table'[ID], 'Table'[CreateDate] ),
        CALCULATETABLE( VALUES ( 'Table'[changeDateEOM] ),ALL('Table'))
    )
VAR _ADD1 =
    ADDCOLUMNS (
        _BASIC,
        "Status change date",
            CALCULATE (
                MAX ( 'Table'[Status change date] ),
                FILTER (
                    'Table',
                    'Table'[ID] = EARLIER ( [ID] )
                        && YEAR ( 'Table'[Status change date] ) * 100
                            + MONTH ( 'Table'[Status change date] )
                            = YEAR ( EARLIER ( [changeDateEOM] ) ) * 100
                                + MONTH ( EARLIER ( [changeDateEOM] ) )
                )
            )
    )
VAR _ADD2 =
    ADDCOLUMNS (
        _ADD1,
        "Fill",
            VAR _MIN =
                MINX (
                    FILTER (
                        _ADD1,
                        [ID] = EARLIER ( [ID] )
                            && [changeDateEOM] > EARLIER ( [changeDateEOM] )
                    ),
                    [Status change date]
                )
            VAR _MAX =
                MAXX (
                    FILTER (
                        _ADD1,
                        [ID] = EARLIER ( [ID] )
                            && [changeDateEOM] < EARLIER ( [changeDateEOM] )
                    ),
                    [Status change date]
                )
            VAR _RESULT1 =
                IF ( ISBLANK ( [Status change date] ), _MIN, [Status change date] )
            RETURN
                IF ( _RESULT1 = BLANK (), _MAX, _RESULT1 )
    )
VAR _ADD3 =
    ADDCOLUMNS (
        _ADD2,
        "Status",
            CALCULATE (
                MAX ( 'Table'[Status] ),
                FILTER (
                    'Table',
                    'Table'[ID] = EARLIER ( [ID] )
                        && 'Table'[Status change date] = EARLIER ( [Fill] )
                )
            ),
        "Term",
            CALCULATE (
                MAX ( 'Table'[Term] ),
                FILTER (
                    'Table',
                    'Table'[ID] = EARLIER ( [ID] )
                        && 'Table'[Status change date] = EARLIER ( [Fill] )
                )
            )
    )
RETURN
COUNTAX(FILTER(_ADD3,[changeDateEOM] = MAX(EOM[changeDateEOM]) && [Term] = MAX(Term[Term]) && [Status] <>"Resolved"),[ID])

Result is as below.

RicoZhou_0-1651744482766.png

 

Best Regards,
Rico Zhou

 

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

 

 

Thank you @Anonymous . 

Chetanab
Microsoft Employee
Microsoft Employee

I have this as of now, it doesnt give me the expected result

# Active =
VAR __eom = MAX( EoM[Date] )
Var _st =
COUNTROWS(
FILTER(
CALCULATETABLE( VALUES( Sheet1[ID] ), Sheet1[CreateDate] <= __eom ),
CALCULATE(
LASTNONBLANKVALUE( Sheet1[Status change date], MAX( Sheet1[Status] ) ),
Sheet1[Status change date] <= __eom,ALLEXCEPT(sheet1,Sheet1[Term])
) <> "Resolved"
)
)
return _st

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!

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.