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 usingSUMX(@table, [@table][column2])
, and sum of the division column usingSUMX(@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.
Description
The function calculates (SUM(DISTINCT column1) + SUM(column2)) / SUM(column3) in Power BI using DAX. Efficient for large datasets.
More Code Generators
Apache Flink Code GeneratorApache Pig Code GeneratorAzure Data Factory Code GeneratorC/C++ Code GeneratorCouchDB Code GeneratorDAX Code GeneratorExcel Code GeneratorFirebase Code GeneratorGoogle BigQuery Code GeneratorGoogle Sheets Code GeneratorGraphQL Code GeneratorHive Code GeneratorJava Code GeneratorJavaScript Code GeneratorJulia Code GeneratorLua Code GeneratorM (Power Query) Code GeneratorMATLAB Code GeneratorMongoDB Code GeneratorOracle Code GeneratorPostgreSQL Code GeneratorPower BI Code GeneratorPython Code GeneratorR Code GeneratorRedis Code GeneratorRegex Code GeneratorRuby Code GeneratorSAS Code GeneratorScala Code GeneratorShell Code GeneratorSPSS Code GeneratorSQL Code GeneratorSQLite Code GeneratorStata Code GeneratorTableau Code GeneratorVBA Code Generator