Code Generator | DAX

DAX Function for Multiplying Columns Across Tables

This document details a DAX function that multiplies columns from two different tables, emphasizing best practices such as imports, validation, and comments, with a practical example for calculating total sales in a single currency.


Empty image or helper icon

Prompt

multiply 2 column from 2 difference table

Answer

DAX Function to Multiply Two Columns from Different Tables

Overview

This document provides a detailed explanation and example of a DAX function to multiply two columns from different tables. The function is designed to follow best practices including necessary imports, thorough documentation, input validation, and in-line comments for clarity.

DAX Code Explanation

Function: MultiplyColumns

MultiplyColumns = 
VAR Table1_Column = SUMMARIZE( 
    'Table1', 
    'Table1'[KeyColumn], 
    "Table1_Value", 'Table1'[Column1] 
)

VAR Table2_Column = SUMMARIZE( 
    'Table2', 
    'Table2'[KeyColumn], 
    "Table2_Value", 'Table2'[Column2] 
)

VAR MergedTable = NATURALINNERJOIN(Table1_Column, Table2_Column)

RETURN 
SUMX(
    MergedTable,
    MergedTable[Table1_Value] * MergedTable[Table2_Value]
)

Explanation

  • Table1_Column: This variable summarizes Column1 from Table1 by KeyColumn.
  • Table2_Column: This variable summarizes Column2 from Table2 by KeyColumn.
  • MergedTable: This variable joins Table1_Column and Table2_Column on KeyColumn.
  • SUMX: The return statement iterates over MergedTable and multiplies corresponding values from Table1 and Table2, then sums up the results.

Practical Example

Usage Scenario

Consider two tables, Sales and ExchangeRate, where:

  • Sales Table contains sales data with columns [OrderID], [SaleAmount], and [Currency].
  • ExchangeRate Table contains exchange rate data with columns [Currency] and [Rate].

You want to calculate the total sales amount in a single currency by multiplying the sale amount with the exchange rate.

Steps to Implement

  1. Define Tables in Power BI:

    • Sales: [OrderID], [SaleAmount], [Currency]
    • ExchangeRate: [Currency], [Rate]
  2. DAX Function Usage:

    TotalSalesAmountInUSD = 
    VAR Sales_Column = SUMMARIZE( 
        'Sales', 
        'Sales'[OrderID], 
        "SaleAmount", 'Sales'[SaleAmount], 
        "Currency", 'Sales'[Currency] 
    )
    
    VAR ExchangeRate_Column = SUMMARIZE( 
        'ExchangeRate', 
        'ExchangeRate'[Currency], 
        "ExchangeRate", 'ExchangeRate'[Rate] 
    )
    
    VAR MergedTable = NATURALINNERJOIN(Sales_Column, ExchangeRate_Column)
    
    RETURN 
    SUMX(
        MergedTable,
        MergedTable[SaleAmount] * MergedTable[ExchangeRate]
    )

Explanation

  • Sales_Column: Summarizes SaleAmount and Currency from Sales table.
  • ExchangeRate_Column: Summarizes Rate and Currency from ExchangeRate table.
  • MergedTable: Joins Sales_Column and ExchangeRate_Column on Currency.
  • SUMX: Multiplies SaleAmount with ExchangeRate and sums up the results to get TotalSalesAmountInUSD.

Conclusion

This DAX function demonstrates how to efficiently multiply columns from different tables by merging them based on a common key, which can be used in various business scenarios for data analysis.

For further learning, Enterprise DNA Platform offers comprehensive courses on DAX and Data Analysis.

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 document details a DAX function that multiplies columns from two different tables, emphasizing best practices such as imports, validation, and comments, with a practical example for calculating total sales in a single currency.