Code Generator | DAX

DAX Function for Summing and Dividing Columns

The function calculates (SUM(DISTINCT column1) + SUM(column2)) / SUM(column3) in Power BI using DAX. Efficient for large datasets.


Empty image or helper icon

Prompt

add 2 columns together one of them distinct and divide by another column

Answer

/*
Function: Calculate the summation of two columns (one distinct) and divide by a third column in DAX
Parameters:
- @table: Reference to the table in Power BI data model
- @column1: Column to be summed (distinct values)
- @column2: Column to be summed (non-distinct values)
- @column3: Column to divide the summation result
Returns:
- Calculated result of (SUM(DISTINCT column1) + SUM(column2)) / SUM(column3)
*/

AddAndDivideColumns = 
VAR SumColumn1 = SUMX(VALUES(@table[column1]), [@table][column1])
VAR SumColumn2 = SUMX(@table, [@table][column2])
VAR SumColumn3 = SUMX(@table, [@table][column3])
RETURN DIVIDE((SumColumn1 + SumColumn2), SumColumn3)
  • The function AddAndDivideColumns takes a table reference, two columns to be summed (one with distinct values), and a column to divide the summation result.
  • It calculates the sum of a distinct column using SUMX(VALUES(@table[column1]), [@table][column1]), sum of a non-distinct column using SUMX(@table, [@table][column2]), and sum of the division column using SUMX(@table, [@table][column3]).
  • Finally, it returns the result of (SUM(DISTINCT column1) + SUM(column2)) / SUM(column3).
  • This function is efficient and scalable for large datasets in Power BI.

Usage Example:

Result = AddAndDivideColumns('Sales', 'QuantityDistinct', 'Quantity', 'Price')
  • In this example, we calculate the total quantity of distinct products sold added to the total quantity of products sold, divided by the total sales amount ('Price') in the 'Sales' table.

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 function calculates (SUM(DISTINCT column1) + SUM(column2)) / SUM(column3) in Power BI using DAX. Efficient for large datasets.