Power BI DAX: CALCULATE, SUM, ALLSELECTED, DIVIDE

Percent of Product Sales =
VAR ProductSales =
    SUM ( 'Sales'[Sales] )
VAR AllSales =
    CALCULATE ( SUM ( 'Sales'[Sales] ), ALLSELECTED ( 'Products' ) )
RETURN
    DIVIDE ( ProductSales, AllSales )

Brief Explanation

To create a measure that returns the percent of total sales for each product while respecting the report-level filter, you can use the following DAX functions: CALCULATE, SUM, and ALLSELECTED.

How it works:

  1. ProductSales: This variable calculates the total sales amount for the current product context.

  2. AllSales: This variable calculates the total sales amount for all selected products, respecting the report-level filter. The ALLSELECTED('Products') function ensures that the calculation respects the report-level filter.

  3. DIVIDE: This function divides the total sales amount for the current product by the total sales amount for all selected products, giving you the percent of total sales for each product.

ALLSELECTED vs FILTER

ALLSELECTED: This function returns all the rows in a table or all the values in a column, while preserving the context of any filters or slicers applied in the report. This makes it ideal for calculating totals that need to respect the user's selections in the report.

FILTER: This function is used to create a new table that contains only the rows that meet a specified condition. While powerful, it doesn't inherently respect the report-level filters and slicers in the same way that ALLSELECTED does.

By using ALLSELECTED, you ensure that the measure dynamically adjusts based on the user's selections, providing a more accurate and context-sensitive calculation.