Forum Discussion
calculate function
I need some help understanding how the SUMIFS function works in DAX for power pivot. I've read that some combination of CALCULATE and FILTER works, but I can't get it to work dynamically with my 6,000,000 row data set. Here is a simple example of how I'd do this in Excel:
| Client Number | Month | Transaction Amount | Net relationship transaction total per month |
| 1 | Jan | -$100 | -$300 |
| 1 | Jan | -$100 | -$300 |
| 1 | Jan | -$100 | -$300 |
| 1 | Feb | $100 | $300 |
| 1 | Feb | $100 | $300 |
| 1 | Feb | $100 | $300 |
| 2 | Mar | $50 | $300 |
| 2 | Mar | $50 | $300 |
| 2 | Mar | $50 | $300 |
| 2 | Mar | $50 | $300 |
| 2 | Mar | $50 | $300 |
| 2 | Mar | $50 | $300 |
| 3 | Jan | $10 | $20 |
| 3 | Jan | $10 | $20 |
| 3 | Feb | $10 | $30 |
| 3 | Feb | $10 | $30 |
| 3 | Feb | $10 | $30 |
In the Excel table above, I used the SUMIFS formula to calculate column D. The exact formula is: =SUMIFS($C$2:$C$18,$A$2:$A$18,A2,$B$2:$B$18,B2). Taking the first row of data as an example, the formula in cell D2 says "SUM the totals in column C for all rows where the client number (column A) matches the client number in cell A2, and where the Month (column B) matches the month in cell B2. So in this example, cell D2 totals to -$300, because there are three rows with client number 1 in the month of January, and the three transactions add up to -$300. Cells D3 and D4 repeat the same value, because they are calculating the total based on the referenced client number and month in row A3 and B3, and row A4 and B4 as the formula is dragged down (notice how the reference cells are unanchored in the SUMIDS formula).
Now, the issue I have is that DAX will not allow me to create this type of calculated column in a power pivot table. I tried the formula below, and it simply sums my entire 6,000,000 row dataset. Note, the table name in power pivot is "Query", but otherwise I am referring to the same table that you see above:
CALCULATE(
sum([Transaction Amount]),
FILTER(Query,[Client Number]=[Client Number]),
FILTER(Query,[Month]=[Month])
)
This formula just recognizes that all of the client numbers match all of the clients numbers, and all the months match all the months, and it adds every row together. I can write somthing more specific such as:
CALCULATE(
sum([Transaction Amount]),
FILTER(Query,[Client Number]=1),
FILTER(Query,[Month]="Jan")
)
And this will accurately return the sum of only transactions for client number 1 from the month of January (-$300 in the example above). But, I need the formula to be dynamic like the SUMIFS function in Excel that I displayed above. Please help!
Try this...
Column or Measure = CALCULATE ( SUM ( 'Table'[Transaction Amount] ), ALLEXCEPT ( 'Table', 'Table'[Client Number], 'Table'[Month] ) )Hope this helps! :smileyhappy:
Ok I read your post too fast... here it is you were actually very close
Column / Measure = CALCULATE ( SUM ( 'Table'[Transaction Amount] ), FILTER ( ALL ( 'Table'[Transaction Type] ), 'Table'[Transaction Type] = "Withdrawal" || 'Table'[Transaction Type] = "Addition" ), ALLEXCEPT ( 'Table', 'Table'[Client Number], 'Table'[Month] ) )Good Luck! :smileyhappy:
https://www.sqlbi.com/articles/filter-arguments-in-calculate/
10 Replies
- SeanCommunity Champion
Try this...
Column or Measure = CALCULATE ( SUM ( 'Table'[Transaction Amount] ), ALLEXCEPT ( 'Table', 'Table'[Client Number], 'Table'[Month] ) )Hope this helps! :smileyhappy:
- tryanFrequent Visitor
That worked perfectly! Thank you!
- SeanCommunity Champion
There will be an Excel to PBI webinar by Avi Singh on 3/16/2017 Thursday
https://powerbi.microsoft.com/en-us/blog/community-webinars-feb-23-april-6/
I actually may try to catch this too! :smileyhappy:
- jaaferFrequent Visitor
Hi, If I have a 100 columns, how can i type all the column names in ALLEXCEPT. is there any other function . thank you