Code Generator | DAX

Modified No Show Rate DAX Function

This DAX function calculates the no-show rate, displaying `-1` for negative counts instead of hiding rows. It incorporates checks for division by zero, ensuring all data is presented clearly in reports while adhering to best practices.


Empty image or helper icon

Prompt

fix this it is hiding rows if its negative, I just want it to display a - if negative NoShowRate1 = 
VAR RegisteredCount = SUM('Attendance'[Registered])   // Calculate total registered attendees
VAR AttendanceCount = SUM('Attendance'[Attendance])   // Calculate total number of attendees
VAR NoShowCount = RegisteredCount - AttendanceCount    // Determine the number of no-shows
RETURN 
    IF(NoShowCount <= 0,                                // Check if NoShowCount is negative
       BLANK(),                                       // Return blank if it's negative
       NoShowCount / RegisteredCount                   // Calculate the No Show Rate otherwise
    )

Answer

DAX Function to Display No Show Rate with Negative Handling

Overview

The goal is to modify the NoShowRate1 measure to display a negative sign (-) instead of hiding rows when the no-show count is negative. This can be achieved by adjusting the conditions in the RETURN statement.

Updated DAX Function

Here’s the updated DAX function that performs the required calculation while adhering to best practices:

NoShowRate1 = 
VAR RegisteredCount = SUM('Attendance'[Registered])   // Calculate total registered attendees
VAR AttendanceCount = SUM('Attendance'[Attendance])   // Calculate total number of attendees
VAR NoShowCount = RegisteredCount - AttendanceCount    // Determine the number of no-shows

RETURN 
    IF(NoShowCount < 0,                                // Check if NoShowCount is negative
       -1,                                           // Return -1 if it's negative
       NoShowCount / RegisteredCount                   // Calculate the No Show Rate otherwise
    )

Explanation

  1. Variables:

    • RegisteredCount: Computes the total number of registered attendees by summing the Registered column.
    • AttendanceCount: Computes the total number of attendees by summing the Attendance column.
    • NoShowCount: Calculates the number of no-shows by subtracting the AttendanceCount from the RegisteredCount.
  2. Return Logic:

    • Instead of returning BLANK() for negative no-shows, return -1 to indicate a negative no-show count.
    • If NoShowCount is zero or positive, it computes the no-show rate as NoShowCount / RegisteredCount.

Input Validation

Though not explicitly required in DAX, it's good practice to ensure that the denominators do not lead to division by zero. In this case, if RegisteredCount might ever be zero, handle it appropriately:

RETURN 
    IF(NoShowCount < 0, 
       -1, 
       IF(RegisteredCount = 0, 
          BLANK(),  // Avoid division by zero
          NoShowCount / RegisteredCount
       )
    )

Usage Example

Assuming we have an Attendance table with the following data:

Registered Attendance
100 80
100 70
100 150

Evaluation

  • Row 1: NoShowCount = 100 - 80 = 20 → No Show Rate = 20/100 = 0.20
  • Row 2: NoShowCount = 100 - 70 = 30 → No Show Rate = 30/100 = 0.30
  • Row 3: NoShowCount = 100 - 150 = -50 → Result will be -1 indicating a negative no-show count.

Conclusion

The updated DAX function now effectively displays a negative indicator for negative no-show counts while calculating the no-show rate for positive counts. This approach ensures that all data is displayed without hiding any rows while maintaining clarity in reporting. You can enhance your DAX skills further by exploring courses on the Enterprise DNA platform.

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 DAX function calculates the no-show rate, displaying -1 for negative counts instead of hiding rows. It incorporates checks for division by zero, ensuring all data is presented clearly in reports while adhering to best practices.