Code Generator | DAX

DAX Percentage of Total with Top N and Slicers

This guide provides a DAX function to calculate the percentage of a measure against the total in Power BI, factoring in any slicers and ignoring the Top N filter in visuals for accurate reporting.


Empty image or helper icon

Prompt

calculates the % total, but i use top n on the visual and the % total should be calculated on the entire set but slicers on the page should be considered

Answer

DAX Function to Calculate % Total with Top N and Slicers Consideration

This guide outlines a DAX function that computes the percentage of total for a specific measure while taking into account any slicers applied to the page, even when using a Top N filter in a visual.

Overview

Purpose

The objective is to calculate the percentage of a selected measure against the total, where the total is evaluated over all relevant data, regardless of the Top N filter applied in the visual.

Key Concepts

  • Top N Filtering: Refers to limiting the displayed data to the top N records based on a specific measure.
  • Slicers: Filters that allow users to select specific subsets of data which affect the calculations.

DAX Code

Below is the DAX function that achieves this:

% Total = 
VAR CurrentMeasureValue = SUM(yourTable[yourMeasure]) 
VAR TotalMeasureValue = 
    CALCULATE(
        SUM(yourTable[yourMeasure]), 
        ALLSELECTED(yourTable)
    )
RETURN 
    DIVIDE(CurrentMeasureValue, TotalMeasureValue, 0)

Explanation of the Code

  1. Variable Definitions:

    • CurrentMeasureValue: Calculates the sum of the specified measure for the current context (i.e., filtered by the visual).
    • TotalMeasureValue: Uses CALCULATE with ALLSELECTED, to sum the measure over all data that is still affected by any slicers applied on the report (ignoring other filters).
  2. Return Statement:

    • The DIVIDE function is used to avoid division by zero errors. It returns the percentage by dividing the current value by the total value.

Best Practices

  • Use of ALLSELECTED: This function is crucial as it retains the filtering context from the slicers while ignoring the filters applied directly in the visual.
  • Error Handling: The DIVIDE function manages cases where the total might be zero, returning a specified alternate result (in this case, 0).

Usage Example

To implement this measure in a Power BI report, follow these steps:

  1. Create a measure named % Total using the DAX code provided.
  2. Add this measure to your visual.
  3. Use a slicer on your report to filter data (for instance, by year or category).
  4. Apply a Top N filter to your visual (e.g., Top 10 by sales).
  5. Observe how the % Total reflects the overall total taking slicers into account, even when showing only the top records.

Conclusion

This DAX function allows for dynamic reporting in Power BI, providing a clear percentage of total that is unaffected by the visual's Top N settings, while still honoring the context provided by user slicers. By adopting this approach, analysts ensure that reports are informative and actionable.

For extensive training and advanced DAX knowledge, consider engaging with 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 guide provides a DAX function to calculate the percentage of a measure against the total in Power BI, factoring in any slicers and ignoring the Top N filter in visuals for accurate reporting.