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

Vote for your favorite vizzies from the Power BI Dataviz World Championship submissions. Vote now!

Reply
topazz11
Helper III
Helper III

DAX Count consecutive

Hi,

 

How do I write DAX to calculate these?

I'd like to achieve the Total column number.

Thank you,

 

del.PNG

1 ACCEPTED SOLUTION
Ashish_Mathur
Super User
Super User

Hi,

PBI file attached.

Hope this helps.

Ashish_Mathur_0-1769558292003.png

 


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

View solution in original post

3 REPLIES 3
Ashish_Mathur
Super User
Super User

Hi,

PBI file attached.

Hope this helps.

Ashish_Mathur_0-1769558292003.png

 


Regards,
Ashish Mathur
http://www.ashishmathur.com
https://www.linkedin.com/in/excelenthusiasts/
Irwan
Super User
Super User

hello @topazz11 

 

please check if this accomodate your need.

Irwan_0-1769556001813.png

1. Unpivot your table and change null value into 0

Irwan_1-1769556044110.png

Irwan_2-1769556064085.png

2. create a calculated column to seek min value of consecutive date

Min Date =
var _Date =
ADDCOLUMNS(
    ADDCOLUMNS(
        'Table',
        "Next Date",
        MAXX(
            FILTER(
                'Table',
                'Table'[Item]=EARLIER('Table'[Item])&&
                'Table'[Date]<EARLIER('Table'[Date])
            ),
            'Table'[Date]
        )
    ),
    "ConsDate",
    MAXX(
        FILTER(
            'Table',
            'Table'[Item]=EARLIER('Table'[Item])&&
            'Table'[Date]=[Next Date]
        ),
        'Table'[Value]
  )
)
Return
MAXX(
    FILTER(
        _Date,
        'Table'[Item]=EARLIER('Table'[Item])&&
        'Table'[Value]=1&&
        [ConsDate]=0
    ),
    'Table'[Date]
)
3. create a calculated column to seek max date of consecutive date (this has more syntax since you want the max value even it is not consecutive date for example in Item B at 10-Jan)
Max Date =
var _Date =
ADDCOLUMNS(
    ADDCOLUMNS(
        ADDCOLUMNS(
            ADDCOLUMNS(
                'Table',
                "Next Date",
                MAXX(
                    FILTER(
                        'Table',
                        'Table'[Item]=EARLIER('Table'[Item])&&
                        'Table'[Date]<EARLIER('Table'[Date])
                    ),
                    'Table'[Date]
                )
            ),
            "ConsDate",
            MAXX(
              FILTER(
                    'Table',
                    'Table'[Item]=EARLIER('Table'[Item])&&
                    'Table'[Date]=[Next Date]
                ),
                'Table'[Value]
            )
        ),
        "IFMax",
        var _Max =
        MAXX(
            FILTER(
                'Table',
                'Table'[Item]=EARLIER('Table'[Item])&&
                'Table'[Value]=1&&
                [ConsDate]=1
            ),
            'Table'[Date]
        )
        Return
        IF(
            'Table'[Min Date]='Table'[Date],
            'Table'[Date],
            _Max
        )),
    "MaxDate",
    MAXX(
        FILTER(
            'Table',
            'Table'[Item]=EARLIER('Table'[Item])
        ),
        [IFMax]
    )
)
Return
MAXX(
    FILTER(
        _Date,
        'Table'[Item]=EARLIER('Table'[Item])
    ),
    [MaxDate]
)
4. create a calculated column to calculate datediff between min and max date.
Datediff =
DATEDIFF(
    'Table'[Min Date],
    'Table'[Max Date],
    DAY
)+1
this is quite messy but hopefully you can get the idea where to start.
 
Hope this will help.
Thank you.
GeraldGEmerick
Super User
Super User

@topazz11 I found a number of references to "Cthulhu" or "Streaks" by Greg Deckler. From what I see the technique should allow you to achieve your desired result. I have not personally tested the DAX code however.

How to find and report on streaks in data | Greg Deckler posted on the topic | LinkedIn

Helpful resources

Announcements
Power BI DataViz World Championships

Power BI Dataviz World Championships

Vote for your favorite vizzies from the Power BI World Championship submissions!

Sticker Challenge 2026 Carousel

Join our Community Sticker Challenge 2026

If you love stickers, then you will definitely want to check out our Community Sticker Challenge!

January Power BI Update Carousel

Power BI Monthly Update - January 2026

Check out the January 2026 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.