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

Register now to learn Fabric in free live sessions led by the best Microsoft experts. From Apr 16 to May 9, in English and Spanish.

Reply
AAlvarez13
Regular Visitor

How to write a measure that counts rows from 2 different dates columns in Power BI

I need to create a measure that counts rows based on 2 different date columns. Using 10/1/2023 as an reference example this should return 4 records. Where the Work Date is <= 10/1/2023 and the Paid Date is >= 10/1/2023
If I used 11/1/2023 it should return 5 records.
How would I translate this to DAX?

In Excel this is easily accomplished by adding a filter and then entering "Custom Date" for both Date columns.

Date Filters in Excel :

excel_filter1.JPG

For example in excel the custom filter options: Work Date is before or equal to 10/1/2023 and Paid Date is after or equal to 10/1/2023

Adding a Custom Filter Excel :

excel_custom_filter1.png

The data model is using both active and inactive relationships.

How would I translate this to DAX?

 

Using 10/1/2023 as an reference example this should return 4 records.
Where the Work Date is <= 10/1/2023 and the Paid Date is >= 10/1/2023
If I used 11/1/2023 it should return 5 records.

Test Data :

 

OrderDateOrderEndMonthForksNapkinsWork DatePaid Date
2/13/20232/1/202310  
2/23/20232/1/202310  
3/2/20233/1/2023103/1/202312/6/2023
10/22/201910/1/20191010/1/201911/26/2023
10/29/201910/1/201910  
12/23/201912/1/20191012/1/201910/22/2020
4/4/20234/1/202310  
4/18/20234/1/2023154/1/20236/22/2023
3/26/20193/1/2019134/1/20198/30/2019
4/30/20194/1/201910  
5/28/20195/1/201910  
6/4/20196/1/2019116/1/20197/15/2023
10/31/202310/1/20231111/1/202312/14/2023
7/9/20197/1/2019147/1/20198/21/2019
7/31/20187/1/2018128/1/201810/10/2018
3/13/20183/1/201811  
7/23/20197/1/2019108/1/20199/24/2019
10/24/201910/1/20191011/1/201912/11/2023
10/17/201910/1/201910  
11/25/201911/1/201910  
12/3/201912/1/20191112/1/201911/15/2023
1/7/20201/1/2020172/1/20202/28/2020
1/2/20201/1/2020121/1/20202/18/2020



Expected Values:

DatesValues
1/1/20234
2/1/20234
3/1/20235
4/1/20236
5/1/20236
6/1/20236
7/1/20235
8/1/20234
9/1/20234
10/1/20234
11/1/20235
12/1/20223



Here are the measures I have tried so far, the closest one is TestD. However, when i get to June of 2023 it returns the wrong count of June (5) should be 6 and October (5) should be 5.

 

 

 

 

 

 

 

Test1 = CALCULATE(
    COUNTROWS('TestData')
)

TestA = CALCULATE(
        SUMX (
            'Date',
            CALCULATE (
                COUNTROWS ( 'TestData' ),
                FILTER (
                    'TestData',
                    [Date] <= 'TestData'[Work Date]
                        && [Date] <= 'TestData'[Paid Date]
                )
            )
        )
)

TestB = 
CALCULATE(
        COUNTROWS(
                    FILTER('TestData', 'TestData'[Work Date] <= MAX('Date'[Date])
                    )
                )
)


TestC = 
CALCULATE(
	COUNTROWS('TestData')
	,'TestData'[Work Date] <= MAX('Date'[Date]) && 'TestData'[Paid Date] >= MAX('Date'[Date])
)



TestD = CALCULATE(
	COUNTROWS(
	    FILTER(
	    ALL('TestData'),
	    'TestData'[Work Date] <= MAX('Date'[Date])
        &&
	    'TestData'[Paid Date] >= MAX('Date'[Date])
	    )
	)
)

TestG = COUNTROWS(
FILTER(
'TestData',
'TestData'[Work Date] >= MIN('Date'[Date]) // date1 must be in the current month 1/2
&& 'TestData'[Work Date]<= MAX('Date'[Date]) // date1 must be in the current month 2/2
&& 'TestData'[Paid Date] >= MIN('Date'[Date]) // date 2 must be in the current month 1/2
&& 'TestData'[Paid Date] <= MAX('Date'[Date] // date2 must be in the current month 2/2

)))

 

 

 

 

 

 

 


I'm really stuck on this one and have tried many things for a couple of days now. Any help would be greatly appreciated. 
Added the Dropbox link to the pbix here SampleTestDates.PBIX 

6 REPLIES 6
HotChilli
Super User
Super User

Thanks for pbix.

TestD is the one that works with a connected date table (as in your pbix).

--

So why do you think there is a problem (as illustrated on the 'Visual' tab)?  The measure:

TestD = CALCULATE(
	COUNTROWS(
	    FILTER(ALL('TestData'),
	    'TestData'[Work Date] <= MAX('Date'[Date])       &&
	    'TestData'[Paid Date] >= MAX('Date'[Date])
        )
    )
)

works if you have a table visual with the 1st of each month and the measure.

It doesn't give you what you want on the 'Visual' tab because the month dimension includes all the days of the month (not just the first day) . So for the June 2023 example (desired: 6, actual:5) the record for work date: 4/1/2023, paid date: 06/22/2023 is not included in the count for the whole month because the paid date is before the end of June.

Here's a test measure that you can put in a visual with work date and paid date:

MeasureQ = var _theDate = DATE(2023,6,1)
RETURN
IF (AND(SELECTEDVALUE(TestData[Work Date]) < _theDate,  SELECTEDVALUE(TestData[Paid Date]) > _theDate),1)

and you can identify which records are included in a 'month' visual or a 1st of Month visual.

What is happening is that this is first filtering the table on the Work Date and then
It filters that filtered table by the paid date. The problem is that I cant get that to work in a measure.
Heres what I tried.

DatesB = VAR xTable =
CALCULATETABLE(
'TestData', 'TestData'[Work Date] <= MAX('Date'[Date])
&& ISBLANK('TestData'[Work Date]) = FALSE()
)

VAR xCount = CALCULATE(
COUNTROWS(xTable)
,FILTER(xTable, 'TestData'[Paid Date] >= MAX('Date'[Date]) )
)
RETURN xCount

HotChilli
Super User
Super User

The fileio was deleted.

The google drive wanted a login which I don't do.

HotChilli
Super User
Super User

Quick test with a disconnected date table. It looks like C and D are good.

 

Should this "count of June (5) should be 6 and October (5) should be 5." be
"count of June (5) should be 6 and October (5) should be 4."

Also the result data has a year 2022 entry in there

---

I couldn't access the attached files.  If you fix that I'll take another look.

I updated the link to the PBIX from dropbox. Adding it here as well SampleTestDates.pbix 

What is an easier way to drop a fileshare link or what is the most popular method? If i have to ill get dropbox which i dont currently have/use.

Helpful resources

Announcements
Microsoft Fabric Learn Together

Microsoft Fabric Learn Together

Covering the world! 9:00-10:30 AM Sydney, 4:00-5:30 PM CET (Paris/Berlin), 7:00-8:30 PM Mexico City

PBI_APRIL_CAROUSEL1

Power BI Monthly Update - April 2024

Check out the April 2024 Power BI update to learn about new features.

April Fabric Community Update

Fabric Community Update - April 2024

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

Top Solution Authors