Code Generator | DAX

DAX Days Count Calculation Between Date Ranges

This input describes a DAX measure to count the number of date ranges in Table A that encompass specific dates from Table B. It provides a clear example and code explanation for implementing this functionality in Power BI.


Empty image or helper icon

Prompt

table A has 2 columns. start date and end date. Table B has column off dates. Count the days between start and end date from table A. list the count for each day in table B column days counted

Answer

DAX Solution for Counting Days Between Date Ranges

The task is to count the number of days between a start and end date from Table A, then list these counts for each date in Table B. This will be done through a DAX measure.

Assumptions

  • Table A has two columns: Start Date and End Date.
  • Table B has a column called Off Dates.

DAX Code

Days Counted = 
VAR SelectedDate = SELECTEDVALUE('Table B'[Off Dates]) 
RETURN 
    COUNTROWS(
        FILTER(
            'Table A',
            'Table A'[Start Date] <= SelectedDate && 
            'Table A'[End Date] >= SelectedDate
        )
    )

Explanation of the Code

  1. Variable Declaration: We declare a variable SelectedDate to capture the current date context from Table B.
  2. RETURN Statement: This part utilizes the FILTER function which iterates over Table A:
    • We filter Table A to check if the Start Date is less than or equal to the SelectedDate.
    • We also ensure that the End Date is greater than or equal to the SelectedDate.
  3. Count Rows: Finally, COUNTROWS counts the number of rows in the filtered result which meet the criteria.

Input Validation

Although DAX inherently handles context, you may want to ensure:

  • The date formats in Table A are valid dates.
  • The Off Dates in Table B are also in a recognized date format.

Usage Example

Assuming:

  • Table A contains:
    • Row 1: Start Date = 2023-01-01, End Date = 2023-01-10
    • Row 2: Start Date = 2023-01-05, End Date = 2023-01-15
  • Table B contains:
    • Row 1: Off Date = 2023-01-03
    • Row 2: Off Date = 2023-01-07
    • Row 3: Off Date = 2023-01-12

In this scenario:

  • For 2023-01-03, the count would be 1 (only Row 1 of Table A is valid).
  • For 2023-01-07, the count would be 2 (both Rows 1 and 2 are valid).
  • For 2023-01-12, the count would be 1 (only Row 2 is valid).

Conclusion

This DAX formula efficiently counts the number of active date ranges from Table A for each Off Dates entry in Table B, demonstrating best practices in DAX coding. For further learning and enhancement of your DAX skills, consider exploring courses on the Enterprise DNA platform, where you can deepen your knowledge in advanced data analysis techniques.

Create your Thread using our flexible tools, share it with friends and colleagues.

Your current query will become the main foundation for the thread, which you can expand with other tools presented on our platform. We will help you choose tools so that your thread is structured and logically built.

Description

This input describes a DAX measure to count the number of date ranges in Table A that encompass specific dates from Table B. It provides a clear example and code explanation for implementing this functionality in Power BI.