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

Next up in the FabCon + SQLCon recap series: The roadmap for Microsoft SQL and Maximizing Developer experiences in Fabric. All sessions are available on-demand after the live show. Register now

Reply
mmcquil2
Frequent Visitor

Dynamic Function To Count Values in a Date Range

I have a table that has an open and close date for Work Orders. I want to show, month by month, how many Work Orders were/are open. It is easy to just track a history going forward and query the data but I would like to show the history up to this point and not have to query so many files going forward.

 

Any ideas?

 

Thanks,

Michael

1 ACCEPTED SOLUTION
Anonymous
Not applicable

Hi  @mmcquil2 ,

I created some data:

vyangliumsft_0-1648781017975.png

Here are the steps you can follow:

1. Create calculated table.

Date = CALENDAR(MIN('Table'[start date]),MAX('Table'[end date]))

vyangliumsft_1-1648781017977.png

2. Create measure.

Count_open =
var _select=SELECTEDVALUE('Date'[Date])
var _maxdate=EOMONTH(_select,0)
var _mindate=DATE(YEAR(_select),MONTH(_select),1)
return
COUNTX(FILTER(ALL('Table'),'Table'[end date]>=_mindate&& 'Table'[end date]<=_maxdate&&'Table'[end date]<>BLANK()),[Orders])
Flag =
var _select=SELECTEDVALUE('Date'[Date])
var _maxdate=EOMONTH(_select,0)
var _mindate=DATE(YEAR(_select),MONTH(_select),1)
return
IF(
    MAX('Table'[end date])>=_mindate&& MAX('Table'[end date])<=_maxdate&&MAX('Table'[end date])<>BLANK(),1,0)

3. Place [Flag]in Filters, set is=1, apply filter.

vyangliumsft_2-1648781017978.png

4. Result:

When selecting 2022.3.20 in the calendar table, the count of [End date] in this March 2022.3.1 and 2022.3.31 time interval will be displayed in Viusal. If it is empty, it will not be counted.

vyangliumsft_3-1648781017978.png

Please click here for the pbix file.

Best Regards,

Liu Yang

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

6 REPLIES 6
Anonymous
Not applicable

Hi  @mmcquil2 ,

I created some data:

vyangliumsft_0-1648781017975.png

Here are the steps you can follow:

1. Create calculated table.

Date = CALENDAR(MIN('Table'[start date]),MAX('Table'[end date]))

vyangliumsft_1-1648781017977.png

2. Create measure.

Count_open =
var _select=SELECTEDVALUE('Date'[Date])
var _maxdate=EOMONTH(_select,0)
var _mindate=DATE(YEAR(_select),MONTH(_select),1)
return
COUNTX(FILTER(ALL('Table'),'Table'[end date]>=_mindate&& 'Table'[end date]<=_maxdate&&'Table'[end date]<>BLANK()),[Orders])
Flag =
var _select=SELECTEDVALUE('Date'[Date])
var _maxdate=EOMONTH(_select,0)
var _mindate=DATE(YEAR(_select),MONTH(_select),1)
return
IF(
    MAX('Table'[end date])>=_mindate&& MAX('Table'[end date])<=_maxdate&&MAX('Table'[end date])<>BLANK(),1,0)

3. Place [Flag]in Filters, set is=1, apply filter.

vyangliumsft_2-1648781017978.png

4. Result:

When selecting 2022.3.20 in the calendar table, the count of [End date] in this March 2022.3.1 and 2022.3.31 time interval will be displayed in Viusal. If it is empty, it will not be counted.

vyangliumsft_3-1648781017978.png

Please click here for the pbix file.

Best Regards,

Liu Yang

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

mh2587
Super User
Super User

CALCULATE (
COUNTROWS ( TableName ),
FILTER (
TableName,
TableName[Open Date] <= MAX ( 'Calendar'[Date] )
&& TableName[Close Date] >= MIN ( 'Calendar'[Date] )
)
)

//I am assuming you have calendar table


✔ Answered? Mark as solution

Muhammad Hasnain | Super User • Fabric • Power BI • Data Engineering

Let's connect on LinkedIn

So I figured out the date table. The issue I am having now is that it does not count any rows if there is not a close date. Obviously if there is no close date, then it should be considered open. I would assume this is a simple "if" statement but it is not currently coming to me.

 

Thanks,

Michael

Thanks for the response! When you say calendar table, are you asking if I have a seperate table built out that lists MonthYear? As in:

January 2021

February 2021

March 2021

etc.

Yes use max of open date and min of open date then create the separate date table 

Then related this table with your other or fact table


✔ Answered? Mark as solution

Muhammad Hasnain | Super User • Fabric • Power BI • Data Engineering

Let's connect on LinkedIn

I believe this works, thanks! I am still having the issue of it not counting rows that do not have close dates. How can I get this formula to count rows that have an open date but no close?

 

Thanks,

Michael

Helpful resources

Announcements
New to Fabric survey Carousel

New to Fabric Survey

If you have recently started exploring Fabric, we'd love to hear how it's going. Your feedback can help with product improvements.

Power BI DataViz World Championships carousel

Power BI DataViz World Championships - June 2026

A new Power BI DataViz World Championship is coming this June! Don't miss out on submitting your entry.

Join our Fabric User Panel

Join our Fabric User Panel

Share feedback directly with Fabric product managers, participate in targeted research studies and influence the Fabric roadmap.

March Power BI Update Carousel

Power BI Community Update - March 2026

Check out the March 2026 Power BI update to learn about new features.