DAX model invoice running balance - powerpivot

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?

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:

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

Related

how to collect custom data from 3 table in database

i have 3 table - first one about supplier details second one about payment for supplier third one orders
all of them have relationship
table payment for supplier include 3 column for three date for payment with
amount in ever date
the question is how to collect date from three table like that:
the header of table
supplier name
first date (month)
from payment date in database
second date
from payment date in database
table contain
first supplier - [sum ] amount for first date - [sum ] amount for second date
second supplier - [sum ] amount for first date - [sum ] amount for second date
and this for all supplier
how to do that??
select
sup.suppliername, pay.firstdate, pay.seconddate
from suppliers sup
left join payments pay
on sup.supplierid = pay.supplierid
or somthing like that?

SQL query solution for getting statistics from my tables

I have been trying to get statistics from my tables. In the following, I have tried to draw my table structure:
In the above, I have my Transaction table where each transaction is recorded for each user Profile(profile_id). As well as, I record transaction created date pub_date, transaction_type(type options are shown in the circle) and transaction amount. When transaction created I give Bonus to each transaction with appropriate Profile(profile_id).
So, I want to get statistics from above tables within the date range. More precisely:
Total transaction sum amount of each profile transaction within the date range with the transaction_types of WITHDRAW and WITHDRAW_MANUAL.
Total transaction sum amount of each profile transaction within the date range with the transaction_types of DEPOSIT and DEPOSIT_MANUAL.
Total Bonus sum amount of each profile transaction within the date range.
Visually, I want this result.
Here chosen date range means, I will give startDate and endDate period to the query
I could manage to solve 1. and 2.. But, I couldn't find way (for 3.) to SUM of each profiles' BONUS amount within the date range. My solution is as followings:
SELECT mtd.profile_id, sum(mtd.amount) AS summa_deposit,
(
SELECT sum(mtw.amount) AS summa_withdraw
FROM public.main_transaction AS mtw
WHERE mtw.profile_id=mtd.profile_id AND mtw.pub_date>='2017-01-01' AND mtw.pub_date<='2017-10-01' AND mtw.transaction_type IN ('WITHDRAW','WITHDRAW_MANUAL')
)
FROM public.main_transaction AS mtd
WHERE mtd.pub_date>='2017-01-01' AND mtd.pub_date<='2017-10-01' AND mtd.transaction_type IN ('DEPOSIT','DEPOSIT_MANUAL')
GROUP BY mtd.profile_id
ORDER BY mtd.profile_id;
Getting total amount of each profiles bonus is not a problem here. The problem is to get those amount within the date range. Because, I don't record date to my Bonus table. I only have my transaction_id and profile_id
P.S. my table is in the PostGreSQL.
Do you just need another join in a subquery?
(SELECT SUM(b.amount) AS summa_bonus
FROM public.bonus b JOIN
public.main_transaction mtw
ON b.transaction_id = mtw.id
WHERE mtw.profile_id = mtd.profile_id AND
mtw.pub_date >= '2017-01-01' AND
mtw.pub_date <= '2017-10-01' AND
mtw.transaction_type IN ('WITHDRAW', 'WITHDRAW_MANUAL')
)
I don't know if the filter on transaction_type is necessary.

SSAS - Movement between from and two dates

we have a very specific problem on calculating movement using two dates from and to in cube and details are as follows:
Product group, sub group and asset and liability are derived using client type, product type and closing balance being in credit or debit on a particular day.
Product group, sub group and asset/liability can be selected from a dimension which has a surrogate key that is stored in the fact table against the date key and account key.
Fact table is "account's daily snapshot" which has every day's data starting from the date account was opened till today and the granularity is DateKey and AccountKey.
Consider the scenario that on 15th Jan a particular account was categorised as "Asset" based on associated product type, client type and closing balance value but on the 31st because of the movements after 15th till 31st; it went into "Liability". Now consider that the user would like to see the movements between these two dates and selects "from" and "to" dates in the cube using two date hierarchies.
The "to" date hierarchy runs on a date hierarchy where as "from" date hierarchy is dependent on floating date dimension that does not have any relationship and used with linked member function to get the starting point to perform the sum on "to" date hierarchy.
Now when issuing the two dates 15th and 31st on the cube; behind the scene data from 15th till 31st comes in scope of the selection which has key for asset and liability attribute values.
So when the user choose to show dimension attributes Product group, sub group and asset/liability the cube shows values against attribute values "Asset" and "Liability" which is correct on two rows.
Users would like to see that the asset or liability should be from the "to" date only but should also be able to pass from date to get the sum of movement.
We are wondering if we can keep the closing balance from "to" date and show Product group, sub group and asset/liability dimension attributes using this closing balance and using "from" date just to calculate the sum of movements but not to have impact on Product group, sub group and asset/liability attribute values and subtract it from closing balance to get the desired result.
Any help will be greatly appreciated.

List Dimension Members if selected date falls between Start Date and End Date in fact records SSAS MDX

I have a fact table that contains invoice line items, and since these line items are subscriptions, there is a Start Date and an End Date involved
LineItem Customer Product OrderDate StartDate EndDate
1 Customer A Product A 1/1/2013 1/1/2013 3/1/2013
2 Customer A Product B 1/1/2013 1/1/2013 4/1/2013
3 Customer B Product A 1/1/2013 2/1/2013 6/1/2013
The client wants a list of Active Customers for a selected date in Excel(PivotTable). They want to select a date, and if the date falls between the Start Date and End Date of any Invoice Line Item record, then the Customer should be displayed. For example:
If '1/5/2013' is selected, the Customer List should return (LineItem: 1, 2):
Customer A
If '2/10/2013' is selected, the Customer List should return (LineItem: 1,2,3):
Customer A
Customer B
If '5/15/2013' is selected, the Customer List should return (LineItem: 3):
Customer B
Next, the client wants to filter by Products as well, so:
If '3/20/2013' is selected and Product A is selected, the Customer List should return (LineItem: 3):
Customer B
In SQL this is very easy:
Select Distinct Customer from Fact where #SelectedDate between StartDate and EndDate
I am unsure on how to approach this problem in SSAS and what to do with the 'Selected Date' as in, should this be another dimension? if so how is it going to relate to the Fact Table?
Or can this be done on Excel/PowerPivot side using in some other way?
Also my initial approach is to create a Named Set of customers - but I am not sure how to create it based on date range etc.
Any help will be appreciated!
Thanks
If you are able to write the MDX, then you can do this as follows, assuming there is a date dimension table with two foreign keys to it from the fact table, the role playing dimensions are named [Start Date] and [End Date], and #SelectedDate is a string matching the format of your date keys:
SELECT {}
ON COLUMNS,
[Customer].[Customer Name].Members
ON ROWS
FROM [Cube]
WHERE (null : StrToMember('[Start Date].[Date].[' + #SelectedDate + ']'))
*
(StrToMember('[End Date].[Date].[' + #SelectedDate + ']'): null)
The WHERE clause is a cross product of two sets: one set of start dates that contains all from the first one appearing in the dimension to the selected date, and one of end dates that contains all end dates from the selected date to the last one in the cube.
However, I do not think it is possible for users to get Excel to run this type of statement somehow, except via a VBA or Excel plugin solution. I think that should be possible, but have no experience with that.

Sql query help, grabbing dates, counts, averages

I have a table called payhistory, and from this table I need to grab the Year, Month when a payment was made. I then need to get an average of payments grouped by Year and then month.
select AVG(totalpaid) from payhistory
This table has a column called datepaid that is where I need to grab the date from. It has a column called totalpaid which is where I need to grab the average from.
I then need a count of every payment within that month.
And finally a column displaying which Year/Month it was from.
Important columns in the table:
number (account number can use this to get a count)
datepaid (contains the date the payment was entered '2009-09-28 00:00:00.000')
totalpaid (contains the specific payment for that date)
This should work:
select AVG(totalpaid),
COUNT(number),
MONTH(datepaid),
YEAR(datepaid)
from payhistory
group by
MONTH(datepaid),
YEAR(datepaid)