Calculate Age of Debt in SQL Server - sql

I have three tables:
Charges
Payments
Adjustments
Each has a value, called Amount. There are no allocations done on this data, we are assuming the oldest Payments are paying the oldest Charges or Adjustments. Each Amount could be +ve or -ve.
I need to produce a report which shows the age of the debt, based on the current balance being in debt, where the balance is the sum of each Amount in all tables. However, the age of the debt must be the Age of the current debt. If an account was in debit in October, but was zeroed in November and then in Debit in February, the Age of the Debt would be February. In need to provide a 30, 60, 90 day breakdown of each account whose balance is outstanding.
Sorry if this isn't clear, but if you've done it before you'll know what I mean. Any pointers?

Just been playing with a pen and paper. Is this as simple as:
Amnt Current Debt at Time = Sum(Debits to Time) - Sum (All Credits)
So in SQL:
Convert All +ve Charges, -ve Adjustments or -ve Payments to Debits (UNION)
Convert all -ve Charges, +ve Adjustments or +ve Payments to Credits (UNION)
For each of your age points, get the Sum of Debits to that point and subtract all of the credits for all time. (SUM and GROUP BY)
Any problems with this?

Related

how to create a MDX query to count customer retention

How can I get the retained customer from the previous year?
1. Customer Retention...the most recent trailing 12 months vs the previous 12 months...exclude new business...what % of customers are we still selling to? Ideally 100%...
explaining the above statement:-
I need the percentage of customers who were retained this year from the previous year depending on sales. for example- 1,2,3,4,5 (This year) are the customers doing business with us this year and 1,2,3,4,6 (previous Year) are the customers doing business in the previous year. so what I am looking for is the number of customers who have been retained from the previous year is 1,2,3,4 is (Four) divided by the total number of customers whom we are doing business within the previous year 1,2,3,4,6 is (Five). If I can get this somehow the percentage calculation is simple. So the percentage of customers who have been retained is (4/5)*100 = 80%. I want to calculate this as a measure for all the years.

Months Outstanding DAX Calculation

I think maybe the way I'm asking this question is making it unclear. Here is another attempt:
I am trying to create a measure that counts the number of months it takes to reach an ending accounts receivable balance. As an example, I start with a measure that calculates an ending AR balance which is a calculation of values from different columns and tables and must look at every AR transaction regardless of date (that measure is already available.) What I now need to do is figure out how many months, counting backwards and including any fractional amount, starting with the most recent month (either selected or today's date) it takes to accumulate fees that add up to the ending ar balance. As an example, assuming we're looking at an AR balance at the end of 2016 $1,000:
Ending December AR Balance $1,000
December Fees: $200
November Fees: $300
October Fees: $400
September: $300
Using these values, the Months outstanding would be 3.33 (200 + 300 + 400 + (100/300).)
-- Original Question --
I'm trying to create a measure in DAX that calculates a Months Outstanding value. Essentially, the months outstanding calculation takes the final accounts receivable for an individual and counts back each month that it took to get to that value. As an example, if the final AR balance is $100 and each prior month had AR billings of $25 each month, the months outstanding would be 4 months.
I have come up with something that I believe is a start, but probably isn't even close:
Months Outstanding:=
VAR TotalBalance = [Ending Balance]
RETURN
CALCULATE(
DISTINCTCOUNT('Date'[Month]),
FILTER(
ALL('Date'[Month]),
[WIP Billings] + [AR Billings] < TotalBalance
)
)
In the above example, I'm trying to count the number of months it takes before the sum of WIP Billings and AR Billings exceeds TotalBalance (WIP and AR Billings are simply measures that are the sum of their respective columns.)
Thanks,
Eric
Edit
Here is the calculation for Ending Balance. The fact tables (Billing, Disbursement, Time, BilledDisbursement, BilledFee, MatterCredit) have references to Date, Matters, Clients, and Personnel
WIP Balance:=
ROUND(SUM(Disbursement[ToBillAmount])
+SUM('Time'[ToBillAmount])
-SUM(BilledDisbursement[ToBillAmount])
-SUM('BilledFee'[ToBillAmount])
-SUM('Matter Credit'[WIPBallance]), 2)
End WIP Balance:=
CALCULATE(
[WIP Balance],
FILTER(
ALL('Date'[Period]),
'Date'[Period] <= MAX('Date'[Period])
)
)
Ending AR Balance:=
CALCULATE(
SUM(Billing[ARAmount]),
FILTER(
ALL('Date'[Period]),
'Date'[Period] <= MAX('Date'[Period])
)
)
Ending Balance:= [Ending AR Balance] + [End WIP Balance]

SQL (Access), subquery: Find saleperson that exceeds $2000 in sales by month and what date in the month sales exceed $2000

I'm having trouble creating a subquery in Access to cough up what I need:
Input: SalespersonID, Amount, Date
I need to find what SalespersonID exceeds $2000 in a month (easy) AND what day in that month the running sum of Amount for that month exceeded $2000 (argh!).
I can groupby and get the first month any salesperson sum Amount > $2000 but I just can't figure out how to get the first date in that month when the month running sum of Amount>$22000
So you've got running-total in your tags - what did you try?
I would create a temporary table and generate the running total, and derive the month using the 'Month' function, and also derive the day of the month. If you sort by month and day each month's data will give you a running total that is what you want. This means you go month-by-month, and calculate the temp table for all salespersons.
It's not super elegant, but it'll give you what you want.

MDX , Calculate Number of days when the cummulative sum of Revenues from end of a month date match with the given debt amount

I have a financial cube and i have to calculate Daily Sales Outstanding as :
Number of Days between the selected month last date and the earliest transaction date when cummulative sum of Revenue from last date of the month till the date where sum revenue <= the debt amount for the date .
e.g
On 31/12/2009 my debt amount = 2,500,000
31-Dec-09 30-Nov-09 15-Oct-09 31-Oct-09
Revenue 1,000,000 1,000,000 500,000 1,0000
Cummulative sum of revenue 1,000,000 2,00,000 2,500,000 4,000,000
No of Days 31 30 16
On 15/Oct/09 cummulative revenue is 2,500,000 which equals my debt amount on that day
Count of Days = 31 + 31 + 16 = 76 Days.
In other words Sum Revenue from the selected date backwards until sum total equals or exeeds the total to date balance of the debtors.
Any help will be highly appreciated .
If i haven't explained clearly enough or if you need more information then please let me know.
Thanks in advance .
Shuchi.
Have you examined this blog: http://consultingblogs.emc.com/christianwade/archive/2006/04/30/MDX-Sprocs-and-Scripting_3A00_-An-Interesting-Example.aspx
He covers a few ways of approaching this, it sounds to me like a recursive problem, in that you need to 'walk backwards up along the calendar' adding up revenue, until you find the day where the added up revenue meets/exceeds the initial debt?
The above link should give you a few different approaches to tackle this, shout if you get stuck.

Semi-additive measures problem - sum snapshots for each month, but not across months

Proficient with SQL but new to MDX, I am having trouble getting my head around this:
I have a fact table that contains snapshots of account balances monthly. I need to roll up the balances as a semi-additive measure - straight sum doesn't work, obviously, for balances. However, I DO need to sum all the balances within EACH month, separately, by adding together the balances for all accounts, and so the "lastnonempty" notion isn't working for me either. Example, if the facts look like this:
Date AccountNo Balance
2009-01-31 1111 $100
2009-01-31 2222 $100
2009-01-31 4444 $100
2009-01-31 5555 $100
2009-02-28 1111 $100
2009-02-28 2222 $200
2009-02-28 3333 $500
2009-02-28 5555 $50
etc.
And I have an account dimension that groups accounts into a major category / minor category / account hierarchy, I need output like this that sums the balances for each month across all accounts:
Month Total
January 09 $400
February 09 $850
And by broken out by account type:
Month Total
January 09 $400
Type 1 $200
1111 $100
2222 $100
Type 2 $200
February 09 $850
Type 1 $300
Type 2 $550
BUT, the balances should NOT sum across months, quarters or years, because it makes no sense, and they'd be counted twice. Any longer time interval should show the close of period:
Quarter WRONG Correct
Q1 $1250 $850 // should be the sum of balances for the *last* month in Q1
Type 1 $500 $300
Type 2 $759 $550
If I use the stock "lastnonempty" aggregation, I seem to get only the one, literal last account row for a month, not the sum of the account balances for the last month. It's as if the total for the month is showing just the balance taken from one random account present in that month, and not the total. (I'm sure it's not really random, probably is picking one based on storage order or something)
I'm sure I am just doing something simple, wrong...
I think I solved this one: my source data having the balances was very sparse - there was a row only for non-zero balances, and NO rows for accounts when the balance is zero. That made everything screwy (imagine as inventory, where you have counts of parts, and those counts are often zero, but when they are 0 then the row is just missing from the fact table)
I was able to create a view at the data source that would "add back in" all the additional rows, with zero balances, and after that the ClosingPeriod() function started working as expected.