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

Power BI is turning 10! Let’s celebrate together with dataviz contests, interactive sessions, and giveaways. Register now.

Reply
buttercream
Helper I
Helper I

Counting number of consecutive months

Hi,

 

I'm trying to get a count of consecutive monthly purchases for each product.  What would be the best way to do this?

 

This is my table:

ProductPurchase Date
A00111/1/2024
A00110/8/2024
A0018/31/2024
A20012/15/2024
A00910/21/2024
A00912/10/2024

 

Need to make this visual:

ProductConsecutive Months
A0012
A2001
A0091

 

1 ACCEPTED SOLUTION
Anonymous
Not applicable

Hi @buttercream ,

I create a table as you mentioned.

vyilongmsft_0-1736991160821.png

Then I create a calculated column and here is the DAX code.

Month = MONTH('Table'[Purchase Date])

vyilongmsft_1-1736991227073.png

Next I create another calculated column.

IsConsecutive = 
VAR _CurrentProduct = 'Table'[Product]
VAR _CurrentMonth = 'Table'[Month]
VAR _PreviousMonth =
    CALCULATE (
        MAX ( 'Table'[Month] ),
        FILTER (
            'Table',
            'Table'[Product] = _CurrentProduct
                && 'Table'[Month] < _CurrentMonth
        )
    )
RETURN
    IF ( _CurrentMonth - _PreviousMonth = 1, 1, 0 )

vyilongmsft_2-1736991317892.png

Finally you can create a measure and get a new table.

Consecutive Months = SUM('Table'[IsConsecutive]) + 1

vyilongmsft_3-1736991381671.png

 

 

Best Regards

Yilong 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
Ashish_Mathur
Super User
Super User

Hi,

Why should the answer be 1 for A200 and A009?


Regards,
Ashish Mathur
http://www.ashishmathur.com
https://www.linkedin.com/in/excelenthusiasts/
Anonymous
Not applicable

Hi @buttercream ,

I create a table as you mentioned.

vyilongmsft_0-1736991160821.png

Then I create a calculated column and here is the DAX code.

Month = MONTH('Table'[Purchase Date])

vyilongmsft_1-1736991227073.png

Next I create another calculated column.

IsConsecutive = 
VAR _CurrentProduct = 'Table'[Product]
VAR _CurrentMonth = 'Table'[Month]
VAR _PreviousMonth =
    CALCULATE (
        MAX ( 'Table'[Month] ),
        FILTER (
            'Table',
            'Table'[Product] = _CurrentProduct
                && 'Table'[Month] < _CurrentMonth
        )
    )
RETURN
    IF ( _CurrentMonth - _PreviousMonth = 1, 1, 0 )

vyilongmsft_2-1736991317892.png

Finally you can create a measure and get a new table.

Consecutive Months = SUM('Table'[IsConsecutive]) + 1

vyilongmsft_3-1736991381671.png

 

 

Best Regards

Yilong Zhou

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

How do we account for year?  Dec 2024 to Jan 2025 should be counted as consecutive.

@Anonymous 

Hi Yilong, I went through your solution and want to learn a different solution. 

you used a tricky way that if monthnum is 1 that IsConsective will be 1 - blank , will still be 1.

However, I found a small bug in your solution

If there is a purchase date in Jan and there is not a purchase date in last year's Dec, then your IsConsecutive column will still display 1. 

 

11.PNG

12.PNG

 

Hope I didn't misunderstand your solution and very happy to learn a new way of thinking.





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

Proud to be a Super User!




ryan_mayu
Super User
Super User

@buttercream 

you can try to create columns 

monthstart = EOMONTH('Table'[Purchase Date],-1)+1

 

Column = if( minx(FILTER('Table','Table'[Product]=EARLIER('Table'[Product])&&'Table'[monthstart]>EARLIER('Table'[monthstart])),'Table'[monthstart])=EDATE('Table'[monthstart],1),1,0)
 
11.PNG
 
then create a measure
 
Measure = sum('Table'[Column])+1
 
12.PNG
 
pls see the attachment below
 




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

Proud to be a Super User!




Helpful resources

Announcements
June 2025 Power BI Update Carousel

Power BI Monthly Update - June 2025

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

June 2025 community update carousel

Fabric Community Update - June 2025

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