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
mpatel181
New Member

Get count of consecutive months

I have a table with date (monh year) field and some data.  I have a measure that returns 0 and 1 for each month year.  I'm trying to see if there's a way to get count of consecutive months from the most current month where the measure value is 1.  Ex:  If curr month is Dec 2023 and my measure returns 1 for mos: Oct - Dec, then it should return 3.   There could be additioanl consecutive months (ex: Jan - May) with value of 1 but months in the middle have 0.  In this case, I still only care about Oct-Dec.  I've tried to do SUMX on my table but that didn't help.  Is there a loop function that I can use to iterate through the dates?

1 ACCEPTED SOLUTION
Anonymous
Not applicable

Hi @mpatel181 

You can refer to the following measure.

Sample data 

vxinruzhumsft_0-1707120495500.png

I create the two measures to make the sample

Measure = SUM('Table'[Flag])
Measure 3 = DATE(2024,1,1)

(Use the measure 3 to change the date)

We need to  create a new measure

Measure2 =
VAR a =
    CALCULATE (
        [Measure],
        FILTER ( 'Table', EOMONTH ( 'Table'[Date], 0 ) = EOMONTH ( [Measure 3], 0 ) )
    )
VAR b =
    MAXX (
        FILTER (
            ALLSELECTED ( 'Table' ),
            [Measure] = 0
                && EOMONTH ( [Date], 0 ) <= EOMONTH ( [Measure 3], 0 )
        ),
        [Date]
    )
VAR c =
    MINX ( FILTER ( 'Table', [Date] > b ), [Date] )
RETURN
    IF ( a <> 0, DATEDIFF ( c, [Measure 3], MONTH ) + 1, 0 )

Output

vxinruzhumsft_1-1707120779995.png

 

vxinruzhumsft_2-1707120812672.png

Best Regards!

Yolo Zhu

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

4 REPLIES 4
Anonymous
Not applicable

Hi @mpatel181 

You can refer to the following measure.

Sample data 

vxinruzhumsft_0-1707120495500.png

I create the two measures to make the sample

Measure = SUM('Table'[Flag])
Measure 3 = DATE(2024,1,1)

(Use the measure 3 to change the date)

We need to  create a new measure

Measure2 =
VAR a =
    CALCULATE (
        [Measure],
        FILTER ( 'Table', EOMONTH ( 'Table'[Date], 0 ) = EOMONTH ( [Measure 3], 0 ) )
    )
VAR b =
    MAXX (
        FILTER (
            ALLSELECTED ( 'Table' ),
            [Measure] = 0
                && EOMONTH ( [Date], 0 ) <= EOMONTH ( [Measure 3], 0 )
        ),
        [Date]
    )
VAR c =
    MINX ( FILTER ( 'Table', [Date] > b ), [Date] )
RETURN
    IF ( a <> 0, DATEDIFF ( c, [Measure 3], MONTH ) + 1, 0 )

Output

vxinruzhumsft_1-1707120779995.png

 

vxinruzhumsft_2-1707120812672.png

Best Regards!

Yolo Zhu

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

 

 

 

mpatel181
New Member

For some reason, when I add the EARLIER function with table[column], it's underline in red.  Any reason why?

copy and paste your table and column name or the DAX you are testing with





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

Proud to be a Super User!




amustafa
Solution Sage
Solution Sage

Here's a generic DAX measure DAX. 

 

ConsecutiveMonthsCount =
VAR CurrentMonth = MAX(YourTable[Date])
VAR FilteredTable =
FILTER(
YourTable,
YourTable[Date] >= CurrentMonth &&
YourTable[Measure] = 1
)
VAR CountConsecutiveMonths =
COUNTROWS(
ADDCOLUMNS(
FilteredTable,
"MonthDiff",
DATEDIFF(
EARLIER(YourTable[Date]),
YourTable[Date],
MONTH
)
)
)
RETURN
CountConsecutiveMonths

 





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

Proud to be a Super User!




Helpful resources

Announcements
August Power BI Update Carousel

Power BI Monthly Update - August 2025

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

August 2025 community update carousel

Fabric Community Update - August 2025

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

Top Solution Authors