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

Don't miss out! 2025 Microsoft Fabric Community Conference, March 31 - April 2, Las Vegas, Nevada. Use code MSCUST for a $150 discount. Prices go up February 11th. Register now.

Reply
Anonymous
Not applicable

Count of Rows by Month that Fall Between Certain Dates

I'm trying to create a measure that provides a count of contracts that are active at any time in a given month.

 

Right now each row of data is a single contract so I can just use countrows() for the counting but I'm trying to figure out the active part. 

 

You can see below a simple version of the data I have along with the results I'm trying to put together. I also have a Primary Date Table which I link to the Contract Start Date. This table contains every date possible along with a year-month column for the year month portion of the matrix. 

 

Any help would be appreciated. 

 

 

 

Contract NameStart DateEnd Date
Contract 110/1/201812/31/2018
Contract 210/15/20181/15/2019
Contract 310/31/20182/1/2019
Contract 41/1/20191/30/2019
Contract 51/28/20194/1/2019

 

 

Results: 

 Oct-18Nov-18Dec-18Jan-19Feb-19Mar-19Apr-19
Active Contracts3334211
2 ACCEPTED SOLUTIONS
Anonymous
Not applicable

This works (at least it gets the same results as your expected results. Note I am in Australia so our date format is dd/mm/yyyy!

 

1. Get data (Contracts)

 

P1.jpg

 

Create a pair of measures called Mindate and Maxdate on Contracts 

 

Mindate = min(Contracts[Start Date])
MaxDate = max(Contracts[End Date])

 

 

3. Create a calendar table as follows;

 

ContractDateTable = filter(
 
 
//************************************************************
// Date range below. This will generate a table with a [Date] column
//************************************************************
CALENDAR(DATE(YEAR(Contracts[Mindate]),MONTH(Contracts[Mindate]),DAY(Contracts[Mindate])),
DATE(Year(Contracts[MaxDate]),month(Contracts[MaxDate]),day(Contracts[MaxDate])))


 
 
,day([Date]) = 1
)
 
This gets a table with the min contract start date and max contract end date normalised to the 1st of each month.
 
4. Create 2 columns on Contracts
 
ContractStartMonth = Date(YEAR(Contracts[Start Date]),month(Contracts[Start Date]),1)
ContractEndMonth = eomonth(Contracts[End Date],0)
 
5. Create a column on ContractDateTable as follows;
 
ActiveContracts = CountRows(FILTER(Contracts,Contracts[ContractStartMonth]<=[Date]&&Contracts[ContractEndMonth] >=[Date]))
 
Now in the ContractsDateTable you have a column called ActiveContracts that shows how many contracts were active during that month.
 
My results are;
 
P2.jpg
 
 
Stan
 
 
 
 
 

View solution in original post

Ashish_Mathur
Super User
Super User

Hi,

You may download my PBI file from here.

Hope this helps.


Regards,
Ashish Mathur
http://www.ashishmathur.com
https://www.linkedin.com/in/excelenthusiasts/

View solution in original post

2 REPLIES 2
Ashish_Mathur
Super User
Super User

Hi,

You may download my PBI file from here.

Hope this helps.


Regards,
Ashish Mathur
http://www.ashishmathur.com
https://www.linkedin.com/in/excelenthusiasts/
Anonymous
Not applicable

This works (at least it gets the same results as your expected results. Note I am in Australia so our date format is dd/mm/yyyy!

 

1. Get data (Contracts)

 

P1.jpg

 

Create a pair of measures called Mindate and Maxdate on Contracts 

 

Mindate = min(Contracts[Start Date])
MaxDate = max(Contracts[End Date])

 

 

3. Create a calendar table as follows;

 

ContractDateTable = filter(
 
 
//************************************************************
// Date range below. This will generate a table with a [Date] column
//************************************************************
CALENDAR(DATE(YEAR(Contracts[Mindate]),MONTH(Contracts[Mindate]),DAY(Contracts[Mindate])),
DATE(Year(Contracts[MaxDate]),month(Contracts[MaxDate]),day(Contracts[MaxDate])))


 
 
,day([Date]) = 1
)
 
This gets a table with the min contract start date and max contract end date normalised to the 1st of each month.
 
4. Create 2 columns on Contracts
 
ContractStartMonth = Date(YEAR(Contracts[Start Date]),month(Contracts[Start Date]),1)
ContractEndMonth = eomonth(Contracts[End Date],0)
 
5. Create a column on ContractDateTable as follows;
 
ActiveContracts = CountRows(FILTER(Contracts,Contracts[ContractStartMonth]<=[Date]&&Contracts[ContractEndMonth] >=[Date]))
 
Now in the ContractsDateTable you have a column called ActiveContracts that shows how many contracts were active during that month.
 
My results are;
 
P2.jpg
 
 
Stan
 
 
 
 
 

Helpful resources

Announcements
Las Vegas 2025

Join us at the Microsoft Fabric Community Conference

March 31 - April 2, 2025, in Las Vegas, Nevada. Use code MSCUST for a $150 discount!

December 2024

A Year in Review - December 2024

Find out what content was popular in the Fabric community during 2024.