how to collect custom data from 3 table in database - vb.net

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?

Related

How to create an R12 on a distinct count measure in SSAS/MDX?

I have a distinct measure "No of Customer Id" that counts the amount of unique Customer Id's that occurs for each month. What I wish to do, is to create a Calculated member that distinct Count Customer Id over a 12 month period i.e Rolling 12. If you look at "Nr of Customer Id" in 201911 you have 378 unique Id's that month but between 20181201 and 20191130 there are 1020 unique Customer Id's (R12 Nr of Customer Id).
Is this possible to do and if so how ?
I tried with (But didn't work):
sum(parallelperiod([D_Time].[Year-Month-Day].[Year],1,[D_Time].[Year-Month-Day].currentmember).lead(1) : [D_Time].[Year-Month-Day].currentmember , [Measures].[Nr of Customer Id])
Best regards,
Rubrix
Do you need to use parallel period? Could you do something like:
(D_Time].[Year-Month-Day].currentmember.lag(12) :D_Time].[Year-Month-Day].currentmember,[Measures].[Nr of Customer Id])

DAX model invoice running balance

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

Counting records in a week with matching fields but ignoring matches on the same day

I have 4 columns in the table Shipping: CustomerID, CustomerName, Shipping Address and Invoice Date. Customer ID is the unique identifier in this table.
I have a 6 month date period, 02-13-2014 to 08-13-2014, that holds 60,000 records. Each record has those four columns and they are NON-nullable columns. I need to split this 6 month period into 7 day week blocks of time spanning the whole 6 months, and then count the number of records that appear more than once with the same CustomerID and Shipping Address per the 7 day weeks.
My end goal is to find how many of our customers get deliverys more than once a week to the same shipping address. If there is any record that appears more than once with the same customer ID and shipping address, it will mean that it is a delivery.
The kicker is, I don't care if there are multiple records with the same customer ID and shipping address if they are on the same day. I still need to know a record is there for that day, but I don't care how many are there.
Something along these lines might get you started:
SELECT ShippingAddress, DATEPART(wk, InvoiceDate), COUNT(*) AS countForWeek
FROM shipping
GROUP BY ShippingAddress, DATEPART(wk, InvoiceDate)
HAVING COUNT(*) > 1

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.

Merging two SQL tables

I have a table called Store Sales with the following columns
Date
Total Qty Sold
RRP
Total Value Sold
Branch No.
Barcodes
Unit Cost
Then I have another table called ESales that contains this
Inv Date
Our Ship Qty
Unit Price (RRP Inc VAT)
Line Total
Invoice
Order
Line
Brand
Part
Description
Our Order Qty
Unit Price (Exc VAT)
Discount %
Discount Amt (Inc VAT)
Discount Amount (Exc VAT)
Tax Category
Tax Exempt
Group
Sales Cat
Cust. ID
Title
Customer
Name
Tax ID
Rep. ID
Credit Memo
Unit Price
Amount
Category ID
Cust. Amount
Number01
ShortChar01
ShortChar02
Clubcard
There are matching fields but none with the same name. They are
Inv Date = Date
Our Ship Qty = Total Qty Sold
Unit Price (RRP Inc VAT) = RRP
Line Total = Total Value Sold
What I want to do in merge the values in StoreSales to ESales and create additional columns for the data that is not there, these are
Branch No
Barcodes
Unit Cost
Any ideas how to insert the matching values and create the three new ones?
Instead of creating a new table, I would just start with a query that shows you all the data you are describing. The way you merge data from multiple tables together is by using a JOIN command. Here is a example query that shows everything from the ESales table and the additional columns from the StoreSales table you described:
SELECT e.*, s.[Branch No], s.[Barcodes], s.[Unit Cost]
FROM StoreSales s
INNER JOIN ESales e ON
e.[Inv Date] = s.[Date] AND
e.[Our Ship Qty] = s.[Total Qty Sold] AND
e.[Unit Price (RRP Inc VAT)] = s.[RRP] AND
e.[Line Total] = s.[Total Value Sold]
Once you get a query you like, you can save it as a VIEW which essentially lets you interact with the result of this query as if it were a separate table.
I will caution you that you have to be very careful doing this. Usually, data will have some kind of common field like a Order ID or a product SKU that makes it very clear what unique item you are referencing in the database. Joining by things like date, order total, price, etc is bad practice. This is because there is no way to guarantee that there aren't two orders with the same date or the same order total. Those things are not unique to any one particular order.