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
miketangren
Regular Visitor

DAX - Count Days Between two date fields on a Date Axis

Hello,

 

I'm trying to use DAX to count every day between two date fields for a large dataset and create a stacked column chart broken out by Program.  You can do this in excel by means of the COUNTIFS function.

 

I have two tables - a status table & a date table.  I'd like to calculate with DAX the same thing I calculate in Excel with the COUNTIFS function, but broken out by Program on a legend with Date as the Axis.

 

Example dataset:

(My formula in Column G, COUNTIFS, is =COUNTIFS(B:B,"<="&F4,C:C,">="&F4)

 

Program Status Table                                                                                   Date Table

ProgramStatus Start DateStatus End Date  DateCOUNTIFS
A1/1/20222/1/2022  1/1/20222
A1/5/20221/20/2022  1/2/20222
A1/10/20222/1/2022  1/3/20223
A1/15/20221/25/2022  1/4/20223
A1/20/20222/1/2022  1/5/20225
A1/25/20221/20/2022  1/6/20225
B1/1/20222/1/2022  1/7/20225
B1/5/20221/25/2022  1/8/20226
B1/10/20222/1/2022  1/9/20226
B1/15/20221/20/2022  1/10/20228
B1/20/20222/1/2022  1/11/20228
B1/25/20221/25/2022  1/12/20228
C1/3/20222/1/2022  1/13/20229
C1/8/20221/20/2022  1/14/20229
C1/13/20222/1/2022  1/15/202211
C1/18/20221/25/2022  1/16/202211
C1/23/20222/1/2022  1/17/202211
C1/28/20221/20/2022  1/18/202212
     1/19/202212
     1/20/202214
     1/21/202211
     1/22/202211
     1/23/202212
     1/24/202212
     1/25/202213
     1/26/20229
     1/27/20229
     1/28/20229
     1/29/20229
     1/30/20229
     1/31/20229
     2/1/20229

 

Thank you for your help!!!

1 ACCEPTED SOLUTION
bolfri
Super User
Super User

Hi,

 

hope this will help you. Dynamic measure based on your calendar.

Count Correct Rows =
var date_ref = SELECTEDVALUE(dim_calendar[Date])
return
    COUNTX(
        FILTER('Sample';AND('Sample'[Status Start Date] <= date_ref;'Sample'[Status End Date]>=date_ref));
        COUNTROWS('Sample')
    )
 
You can use Program column as you want. No relationship needed.
bolfri_0-1671580078632.png

 





Did I answer your question? Mark my post as a solution!

Proud to be a Super User!




View solution in original post

7 REPLIES 7
bolfri
Super User
Super User

Hi,

 

hope this will help you. Dynamic measure based on your calendar.

Count Correct Rows =
var date_ref = SELECTEDVALUE(dim_calendar[Date])
return
    COUNTX(
        FILTER('Sample';AND('Sample'[Status Start Date] <= date_ref;'Sample'[Status End Date]>=date_ref));
        COUNTROWS('Sample')
    )
 
You can use Program column as you want. No relationship needed.
bolfri_0-1671580078632.png

 





Did I answer your question? Mark my post as a solution!

Proud to be a Super User!




Hi @bolfri ,

 

This Solution does exactly as expected.  Thank you!

 

One follow-up question.  I'd like to show date hierarchy on my axis, with date rolling up to Month & Quarter.  PBI will not accept the Month or Quarter axis - visual shows blank when I try that.  Is there a way to rewrite this to account for those?  Can they be included as variables?

 

Thanks again!

Sure. I needed to change it a little, but this i a result:

Count Correct Rows - Aggregatable =
var min_date = FIRSTDATE(dim_calendar[Date])
var max_date = LASTDATE(dim_calendar[Date])

return
    COUNTX(
        FILTER('Sample';
            AND(
                OR(
                    OR(
                        //Scenario 1
                        AND('Sample'[Status Start Date]<=min_date;'Sample'[Status End Date]>=max_date);
                        //Scenario 2
                        AND('Sample'[Status Start Date]>=min_date;'Sample'[Status End Date]<=max_date)
                    );
                    OR(
                        //Scenario 3
                        AND('Sample'[Status Start Date]<=min_date;AND('Sample'[Status End Date]>=min_date;'Sample'[Status End Date]<=max_date));
                        //Scenario 4
                        AND(AND('Sample'[Status Start Date]>=min_date;'Sample'[Status Start Date]<=max_date);'Sample'[Status End Date]>=max_date)
                    )
                );
            //Exclude all records with wrong data, when start is after end
            'Sample'[Status Start Date] <= 'Sample'[Status End Date]
            )
        );
        COUNTROWS('Sample')
    )
 
Graphic representation on each scenario:
bolfri_0-1671635150653.png

 

Result:
Count Correct Rows - Aggregatable - new one

Count Correct Rows - old one, that works only for day representation

bolfri_1-1671635209105.png

 





Did I answer your question? Mark my post as a solution!

Proud to be a Super User!




Bifinity_75
Solution Sage
Solution Sage

Hi @miketangren , how many programas have you got?. 
if you have few programs try this:

- Create a diferent calculate column for every program:

 

COUNTIFS_A = CALCULATE(COUNTROWS('Program Status Table'), 
FILTER(ALL('Program Status Table'),'Program Status Table'[Status Start Date] <='Date Table'[Date] && 'Program Status Table'[Status End Date]>='Date Table'[Date]),'Program Status Table'[Program]="A")
COUNTIFS_B = CALCULATE(COUNTROWS('Program Status Table'),
FILTER(ALL('Program Status Table'),'Program Status Table'[Status Start Date] <='Date Table'[Date] && 'Program Status Table'[Status End Date]>='Date Table'[Date]),'Program Status Table'[Program]="B")
COUNTIFS_C = CALCULATE(COUNTROWS('Program Status Table'),
FILTER(ALL('Program Status Table'),'Program Status Table'[Status Start Date] <='Date Table'[Date] && 'Program Status Table'[Status End Date]>='Date Table'[Date]),'Program Status Table'[Program]="C")

 

So you can use the programs as a legend for your chart

 

The result:

Bifinity_75_0-1671574791558.png

 

Best Regards

 

Ashish_Mathur
Super User
Super User

Hi,

You may download my PBI file from here.

Hope this helps.

Untitled.png


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

Hi @miketangren , try this calculate column:

COUNTIFS = CALCULATE(COUNTROWS('Program Status Table'), 
FILTER(ALL('Program Status Table'),'Program Status Table'[Status Start Date] <='Date Table'[Date] && 'Program Status Table'[Status End Date]>='Date Table'[Date]))

 

The result:

Bifinity_75_0-1671488122989.png

 

Best Regards

 

 

Hi @Bifinity_75 ,

 

Using the calculated column worked, but it I am not able to add Program to the legend.  How can I modify the formula to accept the row context for Program?

miketangren_0-1671552633940.png

 

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.