Forum Discussion

misunika95's avatar
misunika95
Frequent Visitor
7 years ago
Solved

DAX statement with Ifs and nested SUMIFS

Hi!   So I have a table with acccounts receivable info per account that has the following columns:   group: Credit/Cash accountNumber debtUSD invoiceType collectionsAgent timeInterval I'm...
  • Anonymous's avatar
    Anonymous
    7 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
        __classification

    This 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