Qlikiew - Search for the closest date to the filter - qlikview

There are two tables:
Uploaded table with dates of product price changes:
03/01/2021 100
05/02/2021 120
05/04/2022 130
06/06/2022 160
Standard table with a calendar, which is used for the filter.
Necessary:
In the interface, when setting the filter by date, display the price closest to the filter date (the price that was valid on the filter date)
Example 1: filter value is "05/05/2022" , the nearest price to this date should be displayed: "130" (was set on 05/04/2022)
Example 2 : filter value "03/07/2021", the nearest price to this date should be displayed: "100" (was set on 03/01/2021)
It is undesirable to edit the download script, it is better through the interface.
I will be very glad to help

Related

Accessing data from multiple rows

So I am having some troubles writing a sql query. It is a financial stock problem and we have two table, one named A and one named B. The dates are divided into periods, so we want to calculate the investment value for the next period based on some criteria of the current period.
For instance, to calculate the investment value of period 2, we first need to compare the price of stock of the first date of period 1 from table A with the strike price of the same date from table B, and then take the max price and divide it by the investment of first date which is given as 10,000. After that, you just simply do 10000/max(price,strike), and then multiply the value by 10000. I know how to get the max which can be done by either using CASE or max, but the difficulty I am facing is that how can I actually get the investment value of the previous period. The example above is an exception because we know the value of the first day. However, if you want to calculate the investment value for period 3, you will first need the value of period 2, and this is where I am stuck at.
EDIT
Table A
Date Price
1/16/15 206
2/20/15 208
3/20/15 205
Table B
Date Strike
1/16/15 195
2/20/15 201
3/20/15 206
For example, the number of shares on 2/20/2015 is 10000(206) = 48.54
And the investment value is 48.54 * 208 = 10096.
206 is the max of 206 and 195, and 208 is the max of 208 and 201.
Thanks in advance!

Total Distinct Formula from current date

Please see below the following example:
Contract: Type of Contract: End Date:
Yamaha Active 01/01/2019
Suzuki Active 01/01/2014
Nissan Active 01/01/2018
Opel Disabled 01/01/2020
Mercedes Disabled 01/01/2013
Totals (distinct count) of the contracts that are marked as Active in the column Type of contract and have the column End Date showing a date greater than the current day. Hence, if the date today is 01/01/2017 the total distinct count should be 2 (Yamaha and Nissan).
Could you help in providing the correct MDX formula?
Thanks,
Shai
We don't see your cube structure to suggest something certain. There is no two or more contracts to see the different between regular one and distinct count from your example. Let's say you have Contract dimension with attributes (Contract,Type of Contract, End Date).
Set the value to your Contract attribute in YYYYMMDD format.
Add a calculated measure with the following MDX:
SUM(
existing [Contract].[Type of Contract].&[Active] * [Contract].[End Date].[End Date].Members,
IIF(
[Contract].[End Date].CurrentMember.MemberValue > Format(Now(),'yyyyMMdd'),
1,
NULL
)
)
If you need a distinct count of contractor name you may apply the same logic.

SQL Conditional select - calculate running total

I have a stored procedure that calculates requirements for customers based on input that we receive from them.
Displaying this information is not a problem.
What I'd like to do is show the most recent received amount and subtract that from the weekly requirements.
So if last Friday I shipped 150 items and this weeks requirements are 100 items for each day then I'd like the data grid to show 0 for Monday, 50 for Tuesday, 100 for Wednesday - Friday.
I have currently tried using with limited success the sample select statement -
Select Customer, PartNumber, LastReceivedQty, Day1Qty, Day2Qty, Day3Qty, Day4Qty, Day5Qty,
TotalRequired
FROM Requirements
Obviously the above select statement does nothing but display data as it is in the table. So when I add the case state as follows I get a bit closer to what I need but not fully and I'm unsure how to proceed.
Select Customer, PartNumber, LastReceivedQty,
"Day1Qty" = case When Day1Qty > 0 then Day1Qty - LastReceivedQty end
...
This method works ok as long as the LastReceivedQty is less than the Day1 requirements but it's incorrect because it allows a negative number to be displayed in day one rather than pulling the remainder from day2.
Sample Data looks like the following:
Customer PartNumber LastReceivedQty Day1Qty Day2Qty Day3Qty Day4Qty Day5Qty TotalRqd
45Bi 2526 150 -50 100 100 100
In the sample above the requirements for part number 2526 Day 1 are 100 and the last received qty is 150
The day1qty shows -50 as opposed to zeroing out day 1 and subtract from day2, 3, etc.
How do I display those figures without showing a negative balance on the requirement dates?
Any help/suggestions on this is 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.

How to search value range inside another range in sql

I have a form where i enter min price and max price when creating product details. In the product search i have also 2 fields called min and max. So how can i get the result of given range considering the range given when inserting products
Products
min max
10 15
15 30
20 30
In the search form i insert min as 5 and max as 16. Which products i will get in the result and whats the best theory for searching considering practical situations.
Compare opposite ends of each range to find products in the overlap:
select * from products
where min < $max and max > $min
This approach works well for date ranges too.
select * from products where min>=5 and max<=16
so based on this you will get
Row -> 10, 15
The BETWEEN operator selects a range of data between two values. The values can be numbers, text, or dates.
SQL BETWEEN Operator tutorial
For example:
SELECT * FROM Product
WHERE price
BETWEEN $min AND $max