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
Hello Darek, just a quick follow up:
Power Bi is giving me an issue with the declared variables with the following error:
A single value for column 'Account Number' in table 'Accounts Receivable' cannot be determined. This can happen when a measure formula refers to a column that contains many values without specifying an aggregation such as min, max, count, or sum to get a single result.
Same thing happens with the other variables. Do you happen to know any fix for this?
Regards
I did say: This is your formula for the Classification column in your table. This is not a measure.
Are you trying to use any of the formulas as measures? If you do, then you'll get this error.
One last thing: READ WHAT I'VE WRITTEN BEFORE WELL AND TRY TO UNDERSTAND IT. Then you'll have no problems. When you read things, read them carefully with a full understanding. Don't glance over text. READ UNTIL YOU FULLY UNDERSTAND WHAT'S BEEN EXPRESSED IN THERE.
Best
Darek
- misunika957 years agoFrequent Visitor
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
- Anonymous7 years agoNot applicable
Good :)
Best
Darek