Blog Post

Power BI Community Blog
2 MIN READ

Power BI Desktop groups consecutive dates

V-lianl-msft's avatar
V-lianl-msft
Icon for Community Support rankCommunity Support
4 years ago

Scenario:

It is always necessary to group consecutive dates before proceeding to the next step of calculation when we are at work, which can be resolved by using the DAX function or Power Query in Power BI Desktop.

 

The specific operations are:

  1. Use the DAX function combination.
  2. Open Power Query, add Index to the table, subtract date and combine multiple steps of the table and the table to complete.

 

Sample data:

Table Date1:

 

 

Expected Results:

 

 

Method 1:Use the DAX function to group

  1. Use RANKX function to create a calculated column to sort the dates

 

 

rank = RANKX(ALL(Table1),'Table1'[date],,ASC)

 

 

        

 

  1. Create the following measures to get the groups based on consecutive dates range. 

Measure [if]

By subtracting the current date from the next date, it is a continuous date if it is equal to 1, and it is not a continuous date if it is not equal to it, and it is judged by the IF() function.

 

 

 

 if =

var _current = MAX(Table1[date])

var _next = CALCULATE (MAX ('Table1'[date]),

FILTER (ALL(Table1), 'Table1'[rank] = MAX ('Table1'[rank]) + 1)

)

var _datediff =

DATEDIFF (_current, _next, DAY )

RETURN

IF ( _datediff = 1, 0,1 )

 

 

Measure [accumulative]

 Accumulate the value of [IF] by date.

 

 

 accumulative =
 SUMX (
     FILTER ( ALL ( Table1 ), 'Table1'[date] <= MAX ( 'Table1'[date] ) ),
     [if]
 )

 

 

Measure [Group_consecutive days]

      Compare the current [accumulative] with the previous [accumulative], If not equal, it is the current value, if not, it is the current value + 1.

 

 

Group_consecutive days =
 VAR _current =
     MAXX (
         FILTER ( ALL ( Table1 ), 'Table1'[rank] = MAX ( 'Table1'[rank] ) ),
         [accumulative]
     )
 VAR _previous =
     MAXX (
         FILTER ( ALL ( Table1 ), 'Table1'[rank] = MAX ( 'Table1'[rank] ) - 1 ),
         [accumulative]
     )
 RETURN
     IF ( _current <> _previous, _current, _current + 1 )

 

 

Result:

 

 

Method 2: Use Power Query to group consecutive dates.

  1. Add Index for Date1: Add Column --> Index Column

Table Date1 -- Index Column -- From 0.

 

  1. Click on Add Column -- Custom Column.

Enter the following code:

#"Added Index" [date] {[Index]+1}

 

  1. Select the newly formed column -- right click -- Replace Errors.

 

Replace it with null.

 

  1. First select [date2], then select [date], then click Add Column --> Date --> Subtract Days.

 

  1. Click on the table Date1 -- Copy -- to form a new table Date2.

 

  1. Click the column [Subtraction] of the new table Date2 and filter it out to 1.

 

 

  1. For table Date2 -- Add Column --> Index Column --> From 1

 

  1. Merge table Date1 and Date2 to form Merge1, Home --> Merge Queries --> Merge Queries as New

 

  1. In the new table Merge1, click the extension icon of [Date2.1] in the red box -- only select [Index.1].

 

  1. In the table Merge1, click on the column [date] -- Sort Ascending.

 

        

 

  1. Right click on the column [Date2.1.Index.1] of table Merge1 -- Fill -- Up.

 

 

  1. Delete redundant columns.

 

 

Result:

 

 

Summarize:

Above we have realized the grouping of different consecutive date periods by means of DAX and Power Query respectively, hope this article can help you to solve a similar problem.

 

 

Author: Liu Yang

Reviewer: Kerry Wang & Ula Huang

Updated 4 years ago
Version 3.0

2 Comments

  • Hi V-lianl-msft  

    Thanks for the steps to group consecutive dates. I am new to PBI and have been trying to do something similar.  

    I have applied the steps that you have mentioned to group consecutive dates in power Querry. However, it is not working for my data table because the dates in my date column are not unique unlike the date column in your table above. There are two columns - date and their associated ID's. Is there any way that I can use your approach to group consecutive dates for each ID (please look at the table in the screenshot). 

    For example, I have below table: 

    and to this I wish to add an index column using your approach in power querry:

    I have been stuck with this problem for a very long time and would really appreciate if you can suggest something, anything that can help in this case. 

    Thanks in Advance

     

  • Hi to all!

    I want the formula to mark the initial date of each group of consecutive dates and maintain that same date for all dates within each group. It is necessary to use a formula that identifies the start of each group of consecutive dates and marks that date for all rows within the group.

    The idea is in the "Vacations Start Date" column, dates are listed, some consecutive and others not.

    The "If days" column determines if each date is consecutive with the one above: it marks 0 if not consecutive, and 1 if consecutive.

    The "Part" column indicates that for consecutive dates, it shows the same start date where the pattern begins at zero.

    The "Count Days" column displays the number of consecutive days, excluding weekends.

    Lastly, the "Consecutive days" column repeats the total count of consecutive days for each row

     

    Expected result: