Formula Generator | DAX

Advanced DAX Table Manipulation with CROSSJOIN and UNION

This guide demonstrates how to use DAX functions `CROSSJOIN`, `UNION`, and `CALCULATETABLE` to combine and filter data from multiple tables, specifically focusing on sales and returns, to generate a comprehensive dataset.


Empty image or helper icon

Prompt

Can you give me a very complex formula that includes the functions crossjoin, union and calculatetable

Answer

Complex DAX Formula Using CROSSJOIN, UNION, and CALCULATETABLE

Objective

Create a DAX formula that leverages the functions CROSSJOIN, UNION, and CALCULATETABLE to perform an advanced table manipulation. This example will demonstrate combining data from multiple tables and applying specific filters to derive the final dataset.

Example Scenario

We have two tables, Sales and Returns, and we want to create a table that:

  1. Combines all unique product sales and returns.
  2. Applies filters to include only the entries where the sales amount is greater than a defined threshold and the return amount is non-zero.

DAX Formula

CombinedTable =
var SalesTableFiltered = 
    CALCULATETABLE(
        Sales, 
        Sales[SalesAmount] > 500
    )
var ReturnsTableFiltered = 
    CALCULATETABLE(
        Returns, 
        Returns[ReturnAmount] > 0
    )
var UnionTable = 
    UNION(
        SalesTableFiltered,
        ReturnsTableFiltered
    )
return
    CROSSJOIN(UnionTable, Product)

Explanation

  1. SalesTableFiltered: Filters the Sales table to only include rows where the sales amount is greater than 500.
  2. ReturnsTableFiltered: Filters the Returns table to include rows where the return amount is greater than 0.
  3. UnionTable: Uses the UNION function to combine the filtered Sales and Returns tables into a single table.
  4. CROSSJOIN: Creates a Cartesian product of the UnionTable and the Product table.

Practical Illustration

Assume the following simple structured tables:

Sales Table

ProductID SalesDate SalesAmount
1 2023-01-01 600
2 2023-01-02 150

Returns Table

ProductID ReturnDate ReturnAmount
1 2023-01-10 100
3 2023-01-12 50

Product Table

ProductID ProductName
1 Product A
2 Product B
3 Product C

Resulting CombinedTable

The resulting table will include only rows where sales amounts exceed 500 and returns are greater than 0, then joined by the Product table:

ProductID SalesDate SalesAmount ReturnDate ReturnAmount ProductName
1 2023-01-01 600 2023-01-10 100 Product A
1 2023-01-01 600 2023-01-10 100 Product A

Conclusion

The provided formula demonstrates a method to combine and filter data from multiple tables using CROSSJOIN, UNION, and CALCULATETABLE. This advanced manipulation can be applied to various complex reporting needs.

For further learning, consider exploring advanced courses on the Enterprise DNA Platform to deepen your understanding of these DAX functions and their practical applications.

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 demonstrates how to use DAX functions CROSSJOIN, UNION, and CALCULATETABLE to combine and filter data from multiple tables, specifically focusing on sales and returns, to generate a comprehensive dataset.