Languages Advisor | DAX

Dynamic Top N Product Sales

This DAX expression generates a summary table of the top N products based on their sales in a dataset. The number of products to display can be dynamically controlled using a slicer or an input value.


Empty image or helper icon

This Query related with thread "Calculating New Customers with DAX"

Prompt

DynamicTopNProductSales = 
Summarize(Products, 'Product'[Product Name], "Sales", 
CALCULATE(SUM('Sales'[Sales])),
"Rank", RANKX(ALL('Product'[Product Name]), [Sales],,DESC,Dense)
)

Answer

The DAX (Data Analysis Expressions) expression you have provided is used to create a summary table based on the product names and sales in your dataset.

Here, I'll break down the components of your formula:

  1. SUMMARIZE function: This function returns a summary table for specified columns (in this case 'Product'[Product Name] and Sales) of a table (Products).

  2. 'Product'[Product Name]: This is a column that you want to group by in the summary table.

  3. CALCULATE and SUM functions: The CALCULATE function evaluates an expression in a context that is modified by the specified filters. Here it is calculating the sum of the Sales column in 'Sales' table.

  4. RANKX function: This function returns the ranking of a number in a list of numbers for each row in the table. Here it is calculating the rank of each product based on the Sales in descending order. If there are two or more products with identical values of Sales, they are assigned the same rank (that's the 'Dense' option in the function).

Now, if you want to provide a dynamic top N product sales, you need to combine the formula with the TOPN function. This function returns the top 'N' rows of a table according to a specific measure.

Here's an example:

DynamicTopNProductSales = 
VAR NumberOfProducts = NUMBERVALUE ( SELECTEDVALUE ( 'Top N Products'[Top N Product], 5 ) )
RETURN
TOPN(
    NumberOfProducts,
    SUMMARIZE(Products, 'Product'[Product Name], "Sales", 
    CALCULATE(SUM('Sales'[Sales])),
    "Rank", RANKX(ALL('Product'[Product Name]), [Sales],,DESC,Dense)
    ),
    [Sales],
    DESC
)

In this code block, 'Top N Products'[Top N Product] is a slicer or input value where you can input the number of top products you want to see. By default, it is set to 5. This variable is then used in the TOPN function to return the top N products based on the sales.

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

The provided DAX expression utilizes the SUMMARIZE, CALCULATE, and RANKX functions to produce a summary table that ranks products based on their sales. The expression can be combined with the TOPN function to limit the results to the top N products. By adjusting the value of the 'Top N Products' slicer or input, users can dynamically control the number of products displayed. This DAX expression is a powerful tool for analyzing and showcasing top-performing products in a dataset.