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

The Power BI Data Visualization World Championships is back! It's time to submit your entry. Live now!

Reply
abhishekjangid
Frequent Visitor

Finding largest consecutive weeks window

Hi all,
I am trying to find out largest consecutive weeks window. I have a Date table something like this:
TableName: DATE DIMENSION

YearWeek
20213
20213
20214
20215
20227
20227
20228
20228
20229
202210

 

I get this table when I drill down from a summarized chart. Now I want to find out largest consecutive window in this data.
For example: In above table, largest window is 4 (2022(7), 2022(8), 2022(9), 2022(10))

I am able to achieve the result using python script but wanted to know if it is possible via DAX query.

This is how python code looks like:

 

 

##############################################################################
### Figure out maximum consecutive week window
##############################################################################
max_window_till_now = 0
prev = None
current_max = 0

for index, row in dataset.iterrows():
    if prev is None:
        current_max += 1
        prev = row
        continue
    
    # If years are not same or week difference is greater than 1 then weeks are not consecutive
    if (
        prev['Year'] != row['Year'] or
        prev['Week'] < row['Week'] - 1
    ):
        max_window_till_now = max(max_window_till_now, current_max)
        current_max = 1
    
    # If years are same and week difference if equals to 1 then weeks are consecutive
    elif prev['Week'] == row['Week'] - 1:
        current_max += 1

    prev = row
    
max_window_till_now = max(max_window_till_now, current_max)
##############################################################################

 


Please help me out here.

2 REPLIES 2
abhishekjangid
Frequent Visitor

@Jihwan_Kim Please consider the example given as complete dataset.
One Date table with these entries, that's it.

Jihwan_Kim
Super User
Super User

Hi,

I am not sure how your dataset looks like, but I tried to create a sample pbix file like below.

Please check the below picture and the attached pbix file.

All measures are in the attached pbix file, and each measure is numbered for easy recognizing the steps.

I hope the below sample can provide some ideas on how to create a solution for your dataset.

 

Picture1.png


If this post helps, then please consider accepting it as the solution to help other members find it faster, and give a big thumbs up.


Click here to visit my LinkedIn page

Helpful resources

Announcements
Power BI DataViz World Championships

Power BI Dataviz World Championships

The Power BI Data Visualization World Championships is back! It's time to submit your entry.

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.