Code Generator | DAX

DAX Function for Total Sales Calculation

This guide details a DAX function that multiplies `Quantity` from a `Sales` table with `Price` from a `Product` table to calculate total sales amounts, emphasizing relationship setup and best practices for effective data modeling.


Empty image or helper icon

Prompt

quantity * price both from difference table

Answer

DAX Function to Multiply Quantity and Price from Different Tables

Objective

Create a DAX function that multiplies Quantity from one table with Price from another table and returns the result.

Assumptions

  • Sales table includes a column Quantity.
  • Product table includes a column Price.
  • Both tables have a relationship through a common column ProductID.

DAX Function

Total Sales Amount = 
    SUMX(
        Sales, 
        Sales[Quantity] * RELATED(Product[Price])
    )

Code Explanation

  1. SUMX Function: It iterates over a table (Sales in this case), and for each row, performs the multiplication of the Quantity column from the Sales table and the Price column from the Product table.
  2. RELATED Function: Used to fetch related data from the Product table through the established relationship using ProductID.
  3. Multiplication: For each row in the Sales table, Sales[Quantity] is multiplied by RELATED(Product[Price]).
  4. Sum: SUMX then aggregates these multiplied values.

Usage Example

To use the Total Sales Amount measure in a report:

  1. Model Setup:

    • Ensure there is a relationship between Sales and Product tables via ProductID.
  2. Data Model:

    Sales Table Product Table
    ProductID ProductID
    Quantity Price
    • Example data:

      Sales Product
      ProductID: 1 ProductID: 1
      Quantity: 10 Price: 100
      ProductID: 2 ProductID: 2
      Quantity: 20 Price: 150
  3. Create Measure:

    • Add the provided DAX formula as a measure in your data model.
  4. Visualization:

    • Use the Total Sales Amount measure in visualizations to show the total sales amount, reflecting correct calculations across tables.

Best Practices

  • Validate Relationships: Ensure correct relationships are established in your data model, necessary for the RELATED function to work properly.
  • Input Validation: Although DAX doesn't have explicit input validation, always ensure the data types and relationships are correctly set in your model.
  • Documentation: Commenting and descriptive names improve readability and maintenance.

Further Learning

For more advanced techniques regarding DAX measures and relationships, consider courses on the Enterprise DNA platform.

By following these structured steps, the solution ensures efficiency, readability, and adherence to best practices in DAX and Power BI modeling.

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 details a DAX function that multiplies Quantity from a Sales table with Price from a Product table to calculate total sales amounts, emphasizing relationship setup and best practices for effective data modeling.