Forum Discussion
DAX statement with Ifs and nested SUMIFS
- Anonymous7 years ago
OK.
Classification = var __currentAccountNumber = 'Accounts Receivable'[Account Number] var __currentDebt = 'Accounts Receivable'[Debt USD] var __currentInvoiceType = 'Accounts Receivable'[Invoice Type] var __currentTimeInterval = 'Accounts Receivable'[Time Interval] var __sumOfDebtForCurrentClient = SUMX( FILTER( 'Accounts Receivable', 'Accounts Receivable'[Account Number] = __currentAccountNumber ), 'Accounts Receivable'[Debt USD] ) var __classification = SWITCH( TRUE(), __sumOfDebtForCurrentClient < 0, "Credit Balance", __currentDebt < 0 && __currentInvoiceType in {"AA", "BB", "CC", "DD"}, "Unapplied Payment", // else __currentTimeInterval ) return __classificationThis is your formula for the Classification Column in your table.
If this is not performant enough, then here's a variation on this topic. Just change these lines:
var __sumOfDebtForCurrentClient = SUMX( FILTER( 'Accounts Receivable', 'Accounts Receivable'[Account Number] = __currentAccountNumber ), 'Accounts Receivable'[Debt USD] )to these
var __sumOfDebtForCurrentClient = CALCULATE( SUM( 'Accounts Receivable'[Debt USD] ), ALLEXCEPT( 'Accounts Receivable', 'Accounts Receivable'[Account Number] ) )Not sure which one would be faster on a big table.
By the way, here's the Excel formula (which seems to implement a different logic than the one you've described), courtesy of http://excelformulabeautifier.com/:
=IF( IF( [@group] <> "CASH", SUMIF( [accountNumber], [@accountNumber], [debtUSD] ) ) < 0, "Credit Balance", IF( AND( [@[debtUSD]] < 0, OR( [@[invoiceType]] = "AA", [@[invoiceType]] = "BB", [@[invoiceType]] = "CC", [@[invoiceType]] = "DD", [@[invoiceType]] = "EE", [@[invoiceType]] = "WV" ) ), "Unapplied Payment", IF( OR( [@[collectionsAgent]] = "JohnDoe" ), "Legal", [@[timeInterval]] ) ) )But I've implemented the logic you've described :)
Best
Darek
Hi Darek!
Yeah I see the mistake. When I was trying to add it as a column it returned the error "Token Eof expected". But I was trying to add the column from the query editor rather than with the Add column button in the ribbon tab, in my mind they were the same thing.
Thanks again now it's fully working.
Regards
Good :)
Best
Darek