Forum Discussion

Anonymous's avatar
Anonymous
Not applicable
3 years ago
Solved

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!!!

  • 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.

     

7 Replies

  • Hi Anonymous , 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:

     

    Best Regards

     

     

    • Anonymous's avatar
      Anonymous
      Not applicable

      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?

       

  • bolfri's avatar
    bolfri
    Solution Sage

    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.

     

    • Anonymous's avatar
      Anonymous
      Not applicable

      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!

      • bolfri's avatar
        bolfri
        Solution Sage

        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:

         

        Result:
        Count Correct Rows - Aggregatable - new one

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

         

  • Hi Anonymous , 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:

     

    Best Regards