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

Power BI is turning 10! Let’s celebrate together with dataviz contests, interactive sessions, and giveaways. Register now.

Reply
leekohr
Microsoft Employee
Microsoft Employee

Need to count the number rows between two date

I would like to build table that counts the number of records between to dates. 

I have data below call Data and date table called Calendar. My Data table looks like this.

 

leekohr_0-1701402205449.png

I have date table has one column with all the dates from my data colunm.

 

When I use the following measure, I return only the date that are equal to the start date. When there is a relationship between Data[Start Date] and Calendar'[Date].  We I remove the relationship it works as expected. My question is why?

 

OpenItems =
VAR DateX = MAX('Calendar'[Date])
Return
CALCULATE(COUNTROWS(Data),Data[Start Date]<=DateX)

 

 

OpenItems =
VAR DateX = MAX('Calendar'[Date])
Return
CALCULATE(COUNTROWS(Data),Data[Start Date]<=DateX && Data[End Date]>Datex)

1 ACCEPTED SOLUTION
Anonymous
Not applicable

Hi @leekohr ,

 

The behavior you're observing is likely due to the way DAX handles filter context in Power BI. When there is an active relationship between the Data table and the Calendar table, the filter context is automatically applied based on that relationship. This means that when you calculate the measure, it is only considering the dates that match exactly in both tables due to the relationship.

 

Here's a step-by-step action plan to resolve the issue:

1. Review Relationships:

- Ensure that the relationship between the Data table and the Calendar table is set up correctly. It should typically be a one-to-many relationship, with the Calendar table on the "one" side.

2. Modify the Measure:

- You may need to adjust your measure to ignore the filter context imposed by the relationship. You can do this by using the ALL() function or to REMOVEFILTERS() function the filter context from the Calendar table.

OpenItems = 
VAR DateX = MAX('Calendar'[Date])
Return
COUNTROWS(FILTER(ALL(Data),Data[Start Date]<=DateX && Data[End Date]>Datex))

Best Regards,

Neeko Tang

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

3 REPLIES 3
Anonymous
Not applicable

Hi @leekohr ,

 

The behavior you're observing is likely due to the way DAX handles filter context in Power BI. When there is an active relationship between the Data table and the Calendar table, the filter context is automatically applied based on that relationship. This means that when you calculate the measure, it is only considering the dates that match exactly in both tables due to the relationship.

 

Here's a step-by-step action plan to resolve the issue:

1. Review Relationships:

- Ensure that the relationship between the Data table and the Calendar table is set up correctly. It should typically be a one-to-many relationship, with the Calendar table on the "one" side.

2. Modify the Measure:

- You may need to adjust your measure to ignore the filter context imposed by the relationship. You can do this by using the ALL() function or to REMOVEFILTERS() function the filter context from the Calendar table.

OpenItems = 
VAR DateX = MAX('Calendar'[Date])
Return
COUNTROWS(FILTER(ALL(Data),Data[Start Date]<=DateX && Data[End Date]>Datex))

Best Regards,

Neeko Tang

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

FreemanZ
Super User
Super User

how is your measure visualized or what filter context do you have?

 

it could be that you are using the calendar[date] to get a date range and a date point in the same time, so only the the date point meets such requirement. 

 

leekohr
Microsoft Employee
Microsoft Employee

I add the date field to the X-Axis and the measure to Y-axis to create the following chart.

leekohr_0-1701430255604.png

 

Helpful resources

Announcements
Join our Fabric User Panel

Join our Fabric User Panel

This is your chance to engage directly with the engineering team behind Fabric and Power BI. Share your experiences and shape the future.

June 2025 Power BI Update Carousel

Power BI Monthly Update - June 2025

Check out the June 2025 Power BI update to learn about new features.

June 2025 community update carousel

Fabric Community Update - June 2025

Find out what's new and trending in the Fabric community.