ACCESS SQL: multiple tables and form input issues - sql

I have created two list boxes with dates in an Access form to allow the user to select a date range. The user can pick a date in the first list and a later date in the second list.
The form is called Supplier History, the first list (the "from" date list) is called List3 and the second list (the "to" date list) is called List5. [Edit: List0 in code below contains suppliernames]
The dates in these lists are obtained by merging two tables. The tables involve supplier data, one table called Item Master focuses on general information on the supplier, the second table called ZMCE has transaction data.
The goal is to calculate the average price that the supplier charges, over the selected date range. The price column InfoRecPrice is in table Item Master.
For example let's say the lists have the following dates
These come from table Item Master: this table has dates (in column DateStamp) 8/24/2015, 9/1/2015, 10/1/2015, 11/1/2015, 12/9/2015, 1/4/2016, 2/1/2016, 3/1/2016, 4/1/2016, 5/1/2016, 6/1/2016. Table ZMCE has all the other dates, in column PODate.
Let's say the user picks the highlighted dates. I want the code to calculate the average price (price is in item master) over that time period even though the exact highlighted dates are not in that specific table.
The code below gives me a blank output, no matter what dates I select (even if I select those that are exactly the dates in the item master table):
SELECT Avg([Item Master].InfoRecPrice) AS Expr1
FROM [Item Master]
WHERE ((([Item Master].DateStamp)>=[Forms]![Supplier History]![List3] And ([Item Master].DateStamp)<=[Forms]![Supplier History]![List5]) AND (([Item Master].SupplierName)=[Forms]![Supplier History]![List0]));

Related

Microsoft Access- How to create dynamic variables that queries a selection of Columns

Example Data Picture
My main data table is constructed in the following way:
1.State
2.Product
3.Account Name
4.Jan-20
5.Feb-20
.
.
.
N.)Recent Month- Recent Year
My goal is to get 6 total sums based on 6 different contiguous that are user selected. For example, if someone wanted the value of an Account Given a State and Product for FY-2020, they would sum columns 4 to column 15 (Twelve Months).
I am going to be running joins and queries off of the State, Product, Account combinations (first three columns), but I need to create a method to sum the Data table given a list of Column Numbers.
At this point, I am not looking to put non-contiguous columns in a selected Time Period (i.e. all time period selections will be from Col.Beg_TPn to and including Col.End_TPn). The Data Table houses monthly data that will have one new column every month. The Column Number should stay consistent as we are not looking back further than FY-2020.
This a much easier problem in Excel as you can do a simple SumIfs with an Index of the Column Range as the SumRange and then you filter on Columns 1,2,3. My data table is about 30,000 rows, so Excel freezes on me when could calculations and functions on the entire data set.
What is the best way to go about this in Microsoft Access? Ideally, I would like to create a CTE_TimePeriodTotals that houses the First 3 Columns (State,Product,Account) and then 6 TP Columns (tp-1,tp-2...) that holds the sum of each time period for each row based on the Time Period Column Starts and Time Period Column End.

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.

Creating a calculated column (not aggregate) that changes value based on context SSAS tabular DAX

Data: I have a single row that represents an annual subscription to a product, it has an overall startDate and endDate, there is also third date which is startdate + 1 month called endDateNew. I also have a non-related date table (called table X).
Output I'm looking for: I need a new column called Categorisation that will return 'New' if the date selected in table X is between startDate and endDateNew and 'Existing' if the date is between startDate and endDate.
Problem: The column seems to evaluate immediately without taking in to account the date context from the non-related date table - I kinda expected this to happen in visual studio (where it assumes the context is all records?) but when previewing in Excel it carries through this same value through.
The bit that is working:I have an aggregate (an active subscriber count) that correctly counts the subscription as active over the months selected in Table X.
The SQL equivalent on an individual date:
case
when '2015-10-01' between startDate and endDateNew then 'New'
when '2015-10-01' < endDate then 'Existing'
end as Category
where the value would be calculated for each date in table X
Thanks!
Ross
Calculated columns are only evaluated at model refresh/process time. This is by design. There is no way to make a calculated column change based on run-time changes in filter context from a pivot table.
Ross,
Calculated columns work differently than Excel. Optimally the value is known when the record is first added to the model.
Your example is kinda similar to a slowly changing dimension .
There are several possible solutions. Here are two and a half:
Full process on the last 32 days of data every time you process the subscriptions table (which may be unacceptably inefficient).
OR
Create a new table 'Subscription scd' with the primary key from the subscriptions table and your single calculated column of 'Subscription Age in Days'. Like an outrigger. This table could be reprocessed more efficiently than reprocessing the subscriptions table, so process the subscriptions table as incrementals only and do a full process on this table for the data within the last 32 days instead.
OR
Decide which measures are interesting within the 'new/existing' context and write explicit measures for them using a dynamic filter on the date column in the measures
eg. Define
'Sum of Sales - New Subscriptions',
'Sum of Sales - Existing Subscriptions',
'Distinct Count of New Subscriptions - Last 28 Days', etc

Filter PowerPivot based on multiple Date Criteria

I am trying to apply some Time Intelligence functions in my PowerPivot workbook concerning projects and money received for them. I have three relevant tables; Matters, Payments, and a Date Table.
Each matter has a creationDate, and a closureDate(from a linked table). Likewise, each payment has a date. I have reporting set up decently, but am now trying to use Time intelligence to filter this a bit more clearly.
How can I set a PowerPivot Pivot Table up so that the only Matters which show are those which existed within the period selected. e.g. If I select a slicer for 2014, I don't want to show a matter created in 2015, or one which was closed in 2013. The matter should have been active during the period specified.
Is this possible?
You want to show all the matters EXCEPT those where the CreationDate is after the upper limit of the date range you are looking at or the ClosureDate is before the lower limit of the date range you are looking at.
Assuming you have a data structure like this, where the left-hand table is the Matters and the right-hand one is the Payments:
If you have a calculated field called [Total Payments] that just adds up all the payments in the Payments table, a formula similar to this would work:-
[Payment in Range]:=IF(OR(MIN(Matters[Creation Date])>MAX('Reporting Dates'[Date]),MAX(Matters[Closure Date])<MIN('Reporting Dates'[Date])),BLANK(),[Total Payments])
Here is the result with one month selected in the timeline:
Or with one year selected in the year slicer:
NOTE: in my example, I have used a disconnected date table.
Also, you will see that the Grand Total adds up all the payments because it takes the lowest of all the creation dates and the highest of all the closure dates to determine whether to show a total payment value. If it is important that the Grand Total shows correctly, then an additional measure is required:
[Fixed Totals Payment in Range]:=IF(COUNTROWS(VALUES(Matters[Matter]))=1,[Payment in Range],SUMX(VALUES(Matters[Matter]),[Payment in Range]))
Replace the [Payment in Range] in your pivot table with this new measure and the totals will show correctly, however, this will only work if Matters[Matter] is used as one of the fields in the pivot table.
Use filters & the calculate function.
So, if you're Summing payments, it would look like.....
Payments 2014:= CALCULATE( SUM([Payments]), DateTable[Year]=2014)
The Sum function takes the entirety of payments & the filter function will only capture payments w/in 2014, based on the data connected to your date table.

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.