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

Be one of the first to start using Fabric Databases. View on-demand sessions with database experts and the Microsoft product team to learn just how easy it is to get started. Watch now

Reply
EBGAL
Helper III
Helper III

Find longest continuous run DAX

Hi,

 

Consider following example:

 

PartWeekNumber
A1
A5
A4
A3
A11
A10
A15
A14
A16

 

There are 4 separate runs for part A which are (1), (3,4,5), (10,11), (14,15,16).

I need to have a measure which would output count of elements in the longest run, so in the example above this would be 3.

It does not matter if there is more than one equally long run, just need to display the highest number.

WeekNumber is not always sorted.

 

Thanks

Evan

1 ACCEPTED SOLUTION
v-kkf-msft
Community Support
Community Support

Hi @EBGAL ,

 

Try the following calculated columns and measure:

 

Column = 
IF(
    'Table'[WeekNumber] = 1,
    0,
    'Table'[WeekNumber] - CALCULATE(
                            MAX('Table'[WeekNumber]),
                            FILTER('Table',
                                'Table'[Part] = EARLIER('Table'[Part])
                                && 'Table'[WeekNumber] < EARLIER('Table'[WeekNumber])
                            )
                        )
)
GroupMin = 
CALCULATE(
    MAX('Table'[WeekNumber]),
    FILTER(
        'Table',
        'Table'[Column] <> 1
        &&'Table'[Part] = EARLIER('Table'[Part])
        && 'Table'[WeekNumber] <= EARLIER('Table'[WeekNumber])
    )
)
Measure = 
MAXX(
    'Table',
    CALCULATE(
        DISTINCTCOUNT('Table'[WeekNumber]),
        ALLEXCEPT(
            'Table',
            'Table'[Part],
            'Table'[GroupMin]
        )
    )
)

 image.png   image.png

 

If the problem is still not resolved, please provide detailed error information or the expected result you expect. Let me know immediately, looking forward to your reply.

Best Regards,
Winniz

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

2 REPLIES 2
v-kkf-msft
Community Support
Community Support

Hi @EBGAL ,

 

Try the following calculated columns and measure:

 

Column = 
IF(
    'Table'[WeekNumber] = 1,
    0,
    'Table'[WeekNumber] - CALCULATE(
                            MAX('Table'[WeekNumber]),
                            FILTER('Table',
                                'Table'[Part] = EARLIER('Table'[Part])
                                && 'Table'[WeekNumber] < EARLIER('Table'[WeekNumber])
                            )
                        )
)
GroupMin = 
CALCULATE(
    MAX('Table'[WeekNumber]),
    FILTER(
        'Table',
        'Table'[Column] <> 1
        &&'Table'[Part] = EARLIER('Table'[Part])
        && 'Table'[WeekNumber] <= EARLIER('Table'[WeekNumber])
    )
)
Measure = 
MAXX(
    'Table',
    CALCULATE(
        DISTINCTCOUNT('Table'[WeekNumber]),
        ALLEXCEPT(
            'Table',
            'Table'[Part],
            'Table'[GroupMin]
        )
    )
)

 image.png   image.png

 

If the problem is still not resolved, please provide detailed error information or the expected result you expect. Let me know immediately, looking forward to your reply.

Best Regards,
Winniz

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

amitchandak
Super User
Super User

@EBGAL , continuous streak problem

Refer if old solution can help

https://community.powerbi.com/t5/Desktop/Need-help-in-DAX/m-p/1277302#M559393

 

 

My attempt

https://community.powerbi.com/t5/Community-Blog/Power-BI-Continuous-Streak-With-One-Day-Break/ba-p/1...

Join us as experts from around the world come together to shape the future of data and AI!
At the Microsoft Analytics Community Conference, global leaders and influential voices are stepping up to share their knowledge and help you master the latest in Microsoft Fabric, Copilot, and Purview.
️ November 12th-14th, 2024
 Online Event
Register Here

Helpful resources

Announcements
Las Vegas 2025

Join us at the Microsoft Fabric Community Conference

March 31 - April 2, 2025, in Las Vegas, Nevada. Use code MSCUST for a $150 discount!

ArunFabCon

Microsoft Fabric Community Conference 2025

Arun Ulag shares exciting details about the Microsoft Fabric Conference 2025, which will be held in Las Vegas, NV.

December 2024

A Year in Review - December 2024

Find out what content was popular in the Fabric community during 2024.