Advance your Data & AI career with 50 days of live learning, dataviz contests, hands-on challenges, study groups & certifications and more!
Get registeredGet Fabric Certified for FREE during Fabric Data Days. Don't miss your chance! Learn more
Hi,
I am new in DAX and I need help with below:
I have a column with dates and I need a count of consecutive dates, so when days are consecutive it will make cound and when there is a gap then it starts again. to look something like this:
| date | days count |
| 01/01/2024 | 1 |
| 02/01/2024 | 2 |
| 03/01/2024 | 3 |
| 05/01/2024 | 1 |
| 06/01/2024 | 2 |
| 07/01/2024 | 3 |
| 08/01/2024 | 4 |
| 09/01/2024 | 5 |
| 12/01/2024 | 1 |
| 13/01/2024 | 2 |
| 15/01/2024 | 1 |
Solved! Go to Solution.
Hi @damo230
Please try this:
Table:
Click the Transform data in the Home pane.
Then select the Advanced editor, copy the following code and replace all lines of code after Changed Type:
#"Sorted Rows" = Table.Sort(#"Changed Type",{{"Date", Order.Ascending}}),
#"Added Index" = Table.AddIndexColumn(#"Sorted Rows", "Index", 1, 1, Int64.Type),
#"Added Custom" = Table.AddColumn(#"Added Index", "IsNewGroup", each if [Index] = 1 then 1 else if Duration.Days(Date.From([Date]) - Date.From(#"Added Index"{[Index]-2}[Date])) = 1 then 0 else 1),
#"Added Cumulative Group" = Table.AddColumn(#"Added Custom", "Group", each List.Sum(List.FirstN(#"Added Custom"[IsNewGroup], [Index]))),
#"Removed Columns" = Table.RemoveColumns(#"Added Cumulative Group",{"Index", "IsNewGroup"}),
#"Grouped Rows" = Table.Group(#"Removed Columns", {"Group"}, {{"Count", each Table.AddIndexColumn(_, "days count", 1, 1, Int64.Type)}}),
#"Expanded Count" = Table.ExpandTableColumn(#"Grouped Rows", "Count", {"Date", "days count"}, {"Date", "days count"}),
#"Removed Columns1" = Table.RemoveColumns(#"Expanded Count",{"Group"})
in
#"Removed Columns1"
The result is as follow:
Best Regards
Zhengdong Xu
If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.
Hi @damo230
Please try this:
Table:
Click the Transform data in the Home pane.
Then select the Advanced editor, copy the following code and replace all lines of code after Changed Type:
#"Sorted Rows" = Table.Sort(#"Changed Type",{{"Date", Order.Ascending}}),
#"Added Index" = Table.AddIndexColumn(#"Sorted Rows", "Index", 1, 1, Int64.Type),
#"Added Custom" = Table.AddColumn(#"Added Index", "IsNewGroup", each if [Index] = 1 then 1 else if Duration.Days(Date.From([Date]) - Date.From(#"Added Index"{[Index]-2}[Date])) = 1 then 0 else 1),
#"Added Cumulative Group" = Table.AddColumn(#"Added Custom", "Group", each List.Sum(List.FirstN(#"Added Custom"[IsNewGroup], [Index]))),
#"Removed Columns" = Table.RemoveColumns(#"Added Cumulative Group",{"Index", "IsNewGroup"}),
#"Grouped Rows" = Table.Group(#"Removed Columns", {"Group"}, {{"Count", each Table.AddIndexColumn(_, "days count", 1, 1, Int64.Type)}}),
#"Expanded Count" = Table.ExpandTableColumn(#"Grouped Rows", "Count", {"Date", "days count"}, {"Date", "days count"}),
#"Removed Columns1" = Table.RemoveColumns(#"Expanded Count",{"Group"})
in
#"Removed Columns1"
The result is as follow:
Best Regards
Zhengdong Xu
If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.
you can try this to create calculated columns
Proud to be a Super User!
In tha calculation you returning 'Days Count' but becasue 'Days Count' is not been yet created 'Days Count' is invalid in this DAX.
You will need a date table containing all the dates for the years in your data. You can create one with CALENDARAUTO() if you don't already have one. It does not need to be related to your fact table for this calculation.
Day count =
VAR AllDates = ALL( 'Date'[Date] )
VAR ExistingDates = ALL( 'Table'[date] )
VAR MissingDates = EXCEPT( AllDates, ExistingDates )
VAR CurrentDate = MAX( 'Table'[date] )
VAR PreviousGap = COALESCE(
MAXX( FILTER( MissingDates, 'Date'[Date] < CurrentDate ), 'Date'[Date] ),
MINX( AllDates, 'Date'[Date] ) - 1
)
VAR Result = DATEDIFF( PreviousGap, CurrentDate, DAY )
RETURN Result
To achieve consecutive day counting in Power BI using DAX, follow these steps:
Ensure you have a table (e.g., DatesTable) with at least one column:
Add an index column to your table to help with sequential calculations. This can be done using Power Query:
Let's call the table column Index.
In Power BI, add the following calculated column to your table:
You should get a column (Days Count) that resembles this output:
Date Days Count
| 01/01/2024 | 1 |
| 02/01/2024 | 2 |
| 03/01/2024 | 3 |
| 05/01/2024 | 1 |
| 06/01/2024 | 2 |
| 07/01/2024 | 3 |
| 08/01/2024 | 4 |
| 09/01/2024 | 5 |
| 12/01/2024 | 1 |
| 13/01/2024 | 2 |
| 15/01/2024 | 1 |
Let me know if you encounter any issues! 😊
Advance your Data & AI career with 50 days of live learning, contests, hands-on challenges, study groups & certifications and more!
Check out the October 2025 Power BI update to learn about new features.