Code Generator | DAX

Count Overdue Records in DAX

This guide introduces the DAX function `CountOverdueRecords`, designed to calculate the number of overdue records in datasets, such as invoices or tasks, by comparing due dates with the current date in Power BI.


Empty image or helper icon

Prompt

counts the number of records that has are overdue

Answer

Overdue Records Count Function in DAX

Function Overview

The function CountOverdueRecords calculates the number of overdue records from a dataset. It is particularly useful to analyze overdue invoices, tasks, or projects where a due date is provided.

Function Components

  1. Necessary Imports: Typically, DAX functions are used within Power BI, PowerPivot, or SSAS Tabular, so no external libraries are necessary.
  2. Function Definition: We will define the function, detailing its purpose, parameters, and return type.
  3. Input Validation: Ensure the due date is not null and is in the correct format.
  4. Commentary: Adding inline comments to explain each significant step.
  5. Example Usage: Demonstrating how the function can be used in a practical scenario.

Function Definition

/**
 * Counts the number of overdue records in a dataset.
 * 
 * Parameters:
 *   - Table: The name of the table containing the records.
 *   - DueDateColumn: The column name that holds the due dates.
 * 
 * Return:
 *   - A single integer value representing the count of overdue records.
 */

CountOverdueRecords = 
VAR CurrentDate = TODAY()  -- Capture today's date

RETURN
CALCULATE(
    COUNTROWS(Table),       -- Count the number of records in the table
    FILTER(
        Table, 
        Table[DueDateColumn] < CurrentDate &&       -- Check if due date is before today
        NOT ISBLANK(Table[DueDateColumn])           -- Ensure the due date is not blank
    )
)

Commentary

  • VAR CurrentDate = TODAY(): Captures the current date to be used in the comparison.
  • FILTER: Filters the records where DueDateColumn is less than the current date and is not blank.
  • COUNTROWS: Counts the number of rows that meet the filter criteria.

Example Usage

Imagine you have a table named Invoices with a column DueDate:

CountOverdueInvoices = 
VAR CurrentDate = TODAY()

RETURN
CALCULATE(
    COUNTROWS(Invoices), 
    FILTER(
        Invoices, 
        Invoices[DueDate] < CurrentDate &&
        NOT ISBLANK(Invoices[DueDate])
    )
)

Practical Scenario

Scenario: Overdue Invoices Report

You are analyzing a list of invoices and want to keep track of how many are overdue.

Steps:

  1. Create a new measure: In Power BI or your DAX-compatible tool, create a new measure.
  2. Implement the measure: Insert the provided CountOverdueInvoices code into the measure editor.
  3. Add to Report: Use the new measure in your report to track overdue invoices.

Example Visualization

You can visualize the result using a card in Power BI to show the number of overdue invoices at a glance.


For more advanced DAX techniques and improved data analysis skills, consider exploring the comprehensive training on the Enterprise DNA Platform. There, you can find extensive learning resources tailored for analysts at all levels.

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 guide introduces the DAX function CountOverdueRecords, designed to calculate the number of overdue records in datasets, such as invoices or tasks, by comparing due dates with the current date in Power BI.