Code Generator | Power BI

Fridge Sales Summary by Region

This DAX function calculates total sales of 'Small' fridges grouped by region, returning a table with regions and respective sales amount. Ideal for summarizing fridge sales data in Power BI reports.


Empty image or helper icon

Prompt

sum of sales by region where sales in fridge and discription is small

Answer

/* 
Calculate the sum of sales for each region where the product category 
is 'Fridge' and the description is 'Small'.

Returns a table with regions and respective sales.

Parameters:
    None (usually context is provided by the data model itself)

Returns:
    Table with columns: Region and SumOfSales
    
Exceptions:
    This function assumes that the data model contains 'Sales', 'Region', 'Category', and 'Description' columns.
*/

SUM_OF_SALES_BY_REGION := 
SUMMARIZECOLUMNS(
    'Sales'[Region],                       -- Group by Region
    FILTER(
        'Sales', 
        'Sales'[Category] = "Fridge" &&    -- Category filter: Fridge
        'Sales'[Description] = "Small"     -- Description filter: Small
    ),
    "SumOfSales", SUM('Sales'[SaleAmount]) -- Sum the sales amount per group
)

-- Sample usage in a Power BI report:
-- Add a table visualization and use this measure
-- as the data source to summarize the sales.

Explanation

1. Preparation

Ensure your data model has the relevant columns:

  • Sales table with Region, Category, Description, and SaleAmount columns.

2. Function Definition

In this DAX function:

  • SUMMARIZECOLUMNS: This function creates a summary of the data with specific group by columns and measures.
  • FILTER: Applied to filter only rows where Category equals 'Fridge' and Description equals 'Small'.
  • SUM('Sales'[SaleAmount]): Aggregates the sales amount of filtered data.

3. Commenting and Documentation

  • Inline comments are added to articulate the logic of filtering and aggregation.
  • The docstring at the top provides information about function parameters, return type, and assumptions.

Usage Example

  1. Open Power BI Desktop: Ensure your data model has the relevant Sales table.

  2. Create a New Measure:

    • Go to the Modeling tab.
    • Click on 'New Measure' and paste the above DAX code.
  3. Visualize the Measure:

    • Add a table visualization.
    • Drag fields Region and the newly created measure SumOfSales onto the table.

This will provide you with a summarized table displaying the sum of sales for 'Fridge' products with a 'Small' description grouped by region.

Points to Remember:

  • The code snippet assumes that the necessary columns are present in the Sales table.
  • Always validate the input data to ensure consistency and correctness.
  • Ensure good practices by making sure the DAX code is efficient and can handle large datasets.

For more advanced topics and courses on Power BI, consider exploring 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 total sales of 'Small' fridges grouped by region, returning a table with regions and respective sales amount. Ideal for summarizing fridge sales data in Power BI reports.