'Roll up 1 column using function in DAX
I am trying to roll up a table in DAX as seen below. It's just some operations. I can do this in SQL with joins or in Power Query with merges or pivots, however I was just wondering if this is possible using DAX. Would this even be a good idea? Would it better to perform such an operation using SQL/Power Query/Python/R?
| name | type | quantity |
|---|---|---|
| t1 | a | 5 |
| t1 | b | 10 |
| t1 | c | 5 |
| t2 | a | 5 |
| t2 | b | 5 |
| t2 | c | 10 |
f : (a + b)/c
| name | quantity |
|---|---|
| t1 | 15/5 |
| t2 | 10/10 |
Solution 1:[1]
This is a perfectly fine and simple calculation for DAX.
F = DIVIDE( CALCULATE( SUM('Table'[quantity]),'Table'[type] IN {"a","b"}),
CALCULATE( SUM('Table'[quantity]),'Table'[type] = "c") )
Always use DIVIDE when there is a chance that the divisor ("c") could be 0. Use SUM just like you would in Excel, but wrap it in a CALCULATE if you need to filter values first. Use IN if you have a list of values to filter for the same column.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | TheRizza |
