Measure that shows the oldest product in the table - DAX - database

DAX is still a little bit confusing and new for me and that's why I'm looking for your help.
I have a very simple table that shows the products that are in the production line. It contains 3 columns: One is for the product name, the other one contains the date in which the product went to the production line and the last one, the amound.
All I want is to show in a dashboard in Power BI the first product that still is in the production line. In another words, I want a measure that calculate the oldest date in my table and return the following product (or products ) related to that date.
One solution is to show a table sorted by the date but it is not really what I'm looking for.
Thanks in advance

Related

How could I only show the min in MS Access?

I am very new to access.
I tried to filter the data by using Totals and grouped the productPrice by min, however I get a range of all the product prices from min to max, here is a screenshot.
Also there is another column after MinOfproductPrice called storeName, I didn't include it for privacy reasons.
How could I make it so only the lowest price per product is shown? An example of what I am trying to do.
You could just do a summary query and do a group by with min (as you have done), but I suspect that the column you removed is to blame as it sounds like you want to be able to show that information as well.
There are a few ways to do this. In the first, you'll need to first create a summary query that gets just the product id and minimum price. Then, link this query back to your main table/query to get the additional columns like store, container, etc.
It is similar to finding just the most recent order row for each of your customers. The technique is the same. You can see a demo here:
https://youtu.be/5cnwHyn4dnI

Form with Subform - find record by updating one of two fields

step by step i am advancing with my Gasoline inventory database and it is actually about to start becoming fun to try out different new things on the database.
However i got stuck on one little issue.
I have a form with a Subform.
May main form has two fields. One is the Gas Station and one is the date of the report that comes in.
Each report of any station contains one or more products with gallons sold, delivered and physical inventory (has to be applied manually as gas expands when warm)
I tried to add a macro to each of the two fields which requeries after update, but that didn't do anything.
Anyways, im looking for a solution that lets me look for past records by just updating one of the two fields in the main form.
I guess you have a table containing the Station details - Station ID, Station Name, etc. Something like the image below.
You'll also have a table or query that shows sales, delivery, etc for each station based on the Station ID (it'll also have a report date - forgot to add that to my example table).
When you place the subform into the main form you need to link the two forms so they stay in sync using the Master/Child fields (your StationID) which are available in the property sheet for the subform container:

SQL Server BI: SIngle cube, multiple fact tables

I'm new to creating cubes, so please be patient.
Here's an example of my data
I have multiple companies, each company has multiple stores.
Sales are each linked to a particular company, with a particular store on a particular date.
ex:5 sales took place for Company A, Store 1, on 5/19/2011
Returns are linked to a particular company on a particular date.
ex: 3 returns took place for Company A on 3/11/2012
Sometimes my users want to see a list of stores, the date, and how many returns took place, and how many sales.
Sometimes they want to see a list of companies, the specific stores, and the number of sales.
I have a table that stores
COMPANY - DATE - STORE- SALES - RETURNS
I end up having the value for returns repeated for each store under a particular COMPANY - DATE pair.
so if I'm writing a query, and I want to find out returns, I just do a
select distinct company, date, returns from mytable
but I am not sure how to this into a cube (using SS BI and Visual Studio).
(I've only made a couple of cubes so far)
Thanks! (also, feel free to point me at appropriate references)
It sounds like Company is an attribute of the Store and should be in the Store dimension rather than the fact table. There may have to be a transformation on returns to convert the Company to a store.
Am I missing anything?

Combining data sets without losing observations in SAS

Hye guys,
I know, another post another problem :D :(.
I took a screenshot to easily explain my problem.
http://i39.tinypic.com/rhms0h.jpg
As you can see I want to merge two tables (again), the Base & Analyst table. What I want to achieve is displayed in the right bottom corner table. I’m calculating the number of total analysts and female analysts for each month in the analyst table. In the base table I have different observations for one company (here company Alcoa with ticker AA). When I use the following command:
data want;
merge base analyst;
by month ;
run;
I get the right up corner problem. My observations in the main table are being narrowed down to only 4 observations (for each different year one observation, 2001, 2002, 2005, 2006). What I want is that the observations are not reduced but that for every year the same data is being placed as shown in the right bottom corner. What am I missing in my merge command?
In both tables I have month as a time count variable ( the observations in my base table are monthly) on which I need to merge. For clarity I added 2 screenshots of my real databases in SAS.
The base table:
http://i42.tinypic.com/dr5jky.jpg
The analyst table:
http://i40.tinypic.com/eqpmqq.jpg
Here is what my merged table looks like:
http://i43.tinypic.com/116i62s.jpg
You can clearly see that the merged table only has four observations left for AA (one for each unique year) instead of the original 8.
Anyone an idea to solve this?
Ugh, it appears you can easily solve this by merging on both ticker and month.
Data ftest;
Merge ftest tryf1 ;
By ticker month;
Run;
/shame.

Maintain valid records in a table

I have a table which holds flight schedule data. Every schedule have an effective_from & effective_to date. I load this table from flat file which don't provide me an effective_from and effective_to date. So at the time of loading I ask this information from user.
Suppose user gave from date as current date and to date as 31st March. Now on 1st March the user loads a new flight schedule and user give from date as current date and to date as 31st May.
If I query table for effective date between 1st March to 31st March the query returns me two records for each flight whereas I want only one record for each flight and this should be the latest record.
How do I do this? Should I handle this by query or while loading check and correct the data?
You need to identify the primary key for the data (which might be a 'business' key). There must be something which uniquely identifies each flight schedule (it sounds like it shouldn't include effective_from. Once that key is established, you check for it when importing and then either update the existing record or insert a new one.
I assume that each flight has some unique ID, otherwise how can make the difference between them. Then you can add to the schedule thable extra field "Active".
When loading in new schedule - query first existing records with the same flight id and set them to Active=false. New record enter with Active=true.
Query is then simple: select * from schedule where active=1
I developed this solution but looking for even better solution if possible.
Table Schedule {
scheduleId, flightNumber, effective_from,effective_to
}
Data in Schedule table {
1, XYZ12, 01/01/2009, 31/03/2009
2, ABC12, 01/01/2009, 30/04/2009
}
Now user loads another record 3, XYZ12, 01/03/2009, 31/05/2009
select scheduleId from Schedule where flightNumber = 'XYZ12' and (effective_from < '01/03/2009' and effective_to > '01/03/2009' or effective_from < '31/05/2009' and effective_to > '31/05/2009')
If the above query returns me any result that means its an overlap and I should throw an error to user.
The problem description and the comment to one of the suggestions gives the business rules:
querying flights with an effective date should return only one record per flight
the record returned should be the latest record
previous schedules must be kept in the table
The key to the answer is how to determine which is the latest record - the simplest answer would be to add a column that records the timestamp when the row is inserted. Now when you query for a flight and a given effective date, you just get the result with the latest inserted timestamp (which can be done using ORDER BY DESC and take the first row returned).
You can do a similar query with just the effective date and return all flights - again, for each flight you just want to return the row that includes the effective date, but with the largest timestamp. There's a neat trick for finding the maximum of something on a group-by-group basis - left join the results with themselves so that left < right, then the maximum is the left value where the right is null. The author of High Performance MySQL gives a simple example of this.
It's much easier than trying to retroactively correct the older schedules - and, by the sound of things, the older schedules have to be kept intact to satisfy your business requirements. It also means you can answer historical questions - you can always find out what your schedule table used to look like on a given date - which means it's very handy when generating reports such as "This month's schedule changes" and so on.

Resources