DAX model invoice running balance

大兔子大兔子 提交于 2019-12-08 05:44:30

问题


I have a table with invoices and a table with payments. The Invoice table consists of invoice id, amount, invoice date, expiry date. The payment table consists of invoice id, amount, payment date. The invoice table have an active relationship with the date table on the expiry date column. The payment table have an active relationship with the invoice table on the invoice id columns.

I would like to be able to show the invoice balance on an arbitrary day. Ie if I filter the report or page on a particular date I'd like to see the acual balance on that day per invoice. Anyone know how to acomplish this without creating a new table and programmatically fill it with invoice balance per day entries?


回答1:


Here you go:

InvoiceTotalAmount:=
CALCULATE(
    SUM(Invoice[Amount])
    ,ALL(DimDate) // The active relationship between Invoice[ExpiryDate]
                  // and DimDate[Date] would cause this to only be valid
                  // on the expiry date - we don't want that.
)

PaymentTotalToDate:=
CALCULATE(
    CALCULATE( // We'll manipulate the relationship in the inner
               // CALCULATE() before modifying context based on it
        SUM(Payment[Amount])
        ,USERELATIONSHIP(Payment[Date], DimDate[Date])
   )
    ,FILTER( // Now that that we're looking at the right relationship to
             // DimDate, we can alter the date range in context
        ALL(DimDate)
        ,DimDate[Date] <= MAX(DimDate[Date])
            // Here, we take all dates less than the latest date in
            // context in the pivot table - current date if 1 date in
            // context, else last of week, month, quarter, etc....
    )
)

InvoiceBalanceToDate:=[InvoiceTotalAmount] - [PaymentTotalToDate]

If you're not utilizing that active relationship between Invoice[ExpiryDate] and DimDate[Date], I'd mark it as inactive and the relationship between Payment[Date] and DimDate[Date] as the active one. You could then dispense with the CALCULATE() and ALL() in [InvoiceTotalAmount] and the inner CALCULATE() in [PaymentTotalToDate].

My model diagram:




回答2:


You probably want to create a measure in the date table that uses CALCULATETABLE function to calculate the remaining balance of an invoice on that day.

https://technet.microsoft.com/en-us/library/ee634760(v=sql.105).aspx



来源:https://stackoverflow.com/questions/32902756/dax-model-invoice-running-balance

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!