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

Compete to become Power BI Data Viz World Champion! First round ends August 18th. Get started.

Reply
newyearrrr
Frequent Visitor

How can I count consecutive month with dax

I created a DAX measure:

VAR _TBL1 =
    ADDCOLUMNS (
        FILTER (
            SUMMARIZE (
                ALLSELECTED ( FACT_OT ),
                'FACT_OT'[EMP_CODE],
                'FACT_OT'[PERIOD],
                "_Check over", [CHECK_OT_OVER]
            ),
            [_Check over] <> BLANK ()
        ),
        "_month", MONTH ( FACT_OT[PERIOD] ),
        "_currentMonth", MONTH(MAX(FACT_OT[PERIOD]))
    )
 
RETURN FILTER(_TBL1 , OR(FACT_OT[EMP_CODE] = "04583",FACT_OT[EMP_CODE] = "01329") )
 
and then I get the results as shown in the table below.
 
newyearrrr_0-1719228197195.png

but I want to count consecutive value by FACT_OT[PERIOD] , If it is a consecutive month, it will be counted.

Result : 

EMP_CODE: 01329  --->  Consecutive = 2

EMP_CODE: 04583  --->  Consecutive = 6

 

newyearrrr_1-1719228886065.png

 

 

1 ACCEPTED SOLUTION
Anonymous
Not applicable

Hi @newyearrrr 

First you need to sort the period column:

MonthRank = 
RANKX(
    FILTER(
        FACT_OT,
        FACT_OT[EMP_CODE] = EARLIER(FACT_OT[EMP_CODE])
    ),
    FACT_OT[PERIOD],
    ,
    ASC,
    Dense
)


Then you need to combined and sort the EMP_CODE and MonthRank column:

Combined = [EMP_CODE]&"-"&[MonthRank] 

 

Then you can use the following Measure to get the result you want:

Measure = 
VAR current_emp = SELECTEDVALUE(FACT_OT[EMP_CODE])
VAR current_month = CALCULATE(SELECTEDVALUE(FACT_OT[month]),FILTER('FACT_OT','FACT_OT'[EMP_CODE] = current_emp))
VAR current_rank = SELECTEDVALUE(FACT_OT[MonthRank])
VAR previous_month = CALCULATE(SELECTEDVALUE(FACT_OT[month]),FILTER(ALL('FACT_OT'),'FACT_OT'[MonthRank]=current_rank-1&&'FACT_OT'[EMP_CODE] = current_emp))
VAR next_month = CALCULATE(SELECTEDVALUE(FACT_OT[month]),FILTER(ALL('FACT_OT'),'FACT_OT'[MonthRank]=current_rank+1&&'FACT_OT'[EMP_CODE] = current_emp))
RETURN
IF(current_month - previous_month = 1||next_month-current_month=1,1,BLANK())

 

Result:

vjialongymsft_0-1719288413795.png

 

 

 

 

Best Regards,

Jayleny

 

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

1 REPLY 1
Anonymous
Not applicable

Hi @newyearrrr 

First you need to sort the period column:

MonthRank = 
RANKX(
    FILTER(
        FACT_OT,
        FACT_OT[EMP_CODE] = EARLIER(FACT_OT[EMP_CODE])
    ),
    FACT_OT[PERIOD],
    ,
    ASC,
    Dense
)


Then you need to combined and sort the EMP_CODE and MonthRank column:

Combined = [EMP_CODE]&"-"&[MonthRank] 

 

Then you can use the following Measure to get the result you want:

Measure = 
VAR current_emp = SELECTEDVALUE(FACT_OT[EMP_CODE])
VAR current_month = CALCULATE(SELECTEDVALUE(FACT_OT[month]),FILTER('FACT_OT','FACT_OT'[EMP_CODE] = current_emp))
VAR current_rank = SELECTEDVALUE(FACT_OT[MonthRank])
VAR previous_month = CALCULATE(SELECTEDVALUE(FACT_OT[month]),FILTER(ALL('FACT_OT'),'FACT_OT'[MonthRank]=current_rank-1&&'FACT_OT'[EMP_CODE] = current_emp))
VAR next_month = CALCULATE(SELECTEDVALUE(FACT_OT[month]),FILTER(ALL('FACT_OT'),'FACT_OT'[MonthRank]=current_rank+1&&'FACT_OT'[EMP_CODE] = current_emp))
RETURN
IF(current_month - previous_month = 1||next_month-current_month=1,1,BLANK())

 

Result:

vjialongymsft_0-1719288413795.png

 

 

 

 

Best Regards,

Jayleny

 

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

Helpful resources

Announcements
July 2025 community update carousel

Fabric Community Update - July 2025

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

July PBI25 Carousel

Power BI Monthly Update - July 2025

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