Counting Unique records by Month in Tableau over a year - tableau-api

I have a list of product codes and their sale dates. I want to only count the first ever sale of that product, then SUM the first sales by month of sale.
So if Product A was sold in Feb, Mar and Nov, it only gets counted in Feb.
Here is a badly copied table
Product Code Sales Date WHAT I WANT
-------------------------------------------
A1234 01/02/2014 COUNT THIS
B3333 03/02/2014 COUNT THIS
C5555 06/03/2014 COUNT THIS
A1234 09/03/2014 DON’T COUNT
F567 15/04/2014 COUNT THIS
P000 25/05/2014 COUNT THIS
B3333 01/01/2015 DON’T COUNT
A1234 06/01/2015 DON’T COUNT
P000 19/01/2015 DON’T COUNT
B3333 26/01/2015 DON’T COUNT
K5678 20/02/2015 COUNT THIS
I want my end result to be a graph with months down the side and then a count of first times a product is sold.
Does this make sense? I tried using MIN, but it makes everything AGG, which stops you comparing a date with a date.

UPDATE: commenter pointed out there were two questions in the initial question. Both would be answered with an LOD.
To answer the question: "I want my end result to be a graph with months down the side and then a count of first times a product is sold."
Use a Fixed formula:
count(if
DATETRUNC('month',[Sales Date])={fixed [Product Code]:min(datetrunc('month',[Sales Date]))}
then
[Product Code]
END)
This formula compares the month of the date at each row level to the minimum date for all sales of that same product code. Then it counts up the records where this condition is true.
To answer the question: I want to only count the first ever sale of that product, then SUM the first sales by month of sale.
Since the OP didn't provide sales #'s in the original table I didn't answer the sum part of the question. However, this would also be an LOD and it would look like:
sum(if
DATETRUNC('month',[Sales Date])={fixed [Product Code]:min(datetrunc('month',[Sales Date]))}
then
[Sales]
END)
This is very similar to the above except it sums up sales instead of counting products.
And here is a picture (I added sales and one extra row of sales for product A1234 so you can see that it gets summed as part of the product's first months sales.)
To deep dive into the LOD (Level of Detail) part of this, {fixed [Product Code]:min(datetrunc('month',[Sales Date]))}, Tableau allows you to do calculations based on a specified scope. In this case, the formula gets read like "return the minimum date's month of the sales date for each product code". You can read much more about LOD's on Tableau's site or many Google search results.

Related

Dates don't compare properly - Power Bi

I would like to SUMX my Employees Wages, but without Employees who have any sickness record, in which selected date is. So, if Employee was sick from March 3rd to March 5th and Selected Date is March 4th, we don't take that Employee into an account.
That's How I Get A CurrentDay From Slicer:
That's How I Was Trying To At Least Sum Rows, In Which CurrentDay Is Before EndDate of Sickness Record:
And all I get is... The number of TOTAL rows in my sickness records table:
How can I filter that table, to get a valid result?
The structure of your tables is not clear. So, looking through your code you can try following:
COUNTROWS(
FILTER(
table -- ALL(table) as variant, depends on your report ect.
,table[StartDate] <= SELECTEDVALUE(Calendar[Date]) & table[EndDate] >= SELECTEDVALUE(Calendar[Date])
)

How to count just a first sale by a client in tableau

I have a spreadsheet with clients first purchase I want to see how many new clients a month we are getting. However some of the clients we sell too could have the same name in a different zip code for example miami clinic could be in both florida and ohio so I want them counted individually. I also want to see the total new clinics per month. but if a clinic purchases in january and again in march i only want that january purchase counted
Let us assume your sample data be like
Now create a calculated field first purchase date as
{Fixed [Clinic Name], [Zipcode]: min([Sale Date])}
This field will give each clinic its first purchase date only. Check it
Now to create a filter for first purchase date create a calculated field say first purchase filter as
[first purchase date] = [Sale Date]
Adding TRUE value filter (and don't forget to add the filter to context by right clicking it) will give sale amount in first purchases
OR use this filter for differentiation also
For any further query please share your actual data structure with a few rows and desired output in respect of those sample rows.

mdx query to calculate difference between measure at nearest quarter date and measure today

I am trying to calculate a measure which is the difference between a measure (say, profit) today and the nearest preceding quarter.
E.g.: if the quarter months are March, June, September and December, then:
If the current month is May, then the calculated measure = Profit(May)-Profit(March)
If the current month is November, then the calculated measure = Profit(November)-Profit(September)
If the current month is December, then the calculated measure = Profit(December)-Profit(September)
I'm new to MDX and would really like some help on this.
I'm aware of .CurrentMember.PrevMember to get the previous member, but that doesn't help if you don't know how many previous members calls you should use.
Welcome to MDX. To solve the problem you need to have basic understanding of "User Hierarchy".Plus you should know how the functions "currentmember", "parent", "lag()" and "lastchild".
Based on this follow the example below. I am trying to re-produce your example on AdventureWorks for year 2013.
Lets first see all the internet sales amount for 2013
select
{[Measures].[Internet Sales Amount]}
on columns,
{
[Date].[Month of Year].[Month of Year]
}
on rows from
[adventure works]
where
[Date].[Calendar Year].&[2013]
Result
Now lets make a measure that will result value for the last month of previous quater
with
member [Measures].[LastMonthOfPreviousQuater] as
([Date].[Calendar].currentmember.parent.lag(1).lastchild,
[Measures].[Internet Sales Amount])
select
{[Measures].[Internet Sales Amount],[Measures].[LastMonthOfPreviousQuater]}
on columns,
{
[Date].[Calendar].[Month].&[2013]&[09]
}
on rows from
[adventure works]
Result

Average day of month range for bill date

Our billing system has a table of locations. These locations have a bill generated every month. These bills can vary by a few days each month (ex. billed on 6th one month then 8th the next). They always stay around the same time of the month, though. I need to come up with a "blackout" range that is essentially any day of month that location has been billed over the last X months. It needs to appropriately calculate for locations that may bounce between the end/beginning of months.
For this the only relevant tables are a Location Table & a Bill Table. Loc Table has a list of locations with LocationID being the PK. Other fields are irrelevant for this I believe. There's the bill table that has a document number as the PK and LocationID as a FK. Also, with many other fields such as doc amount, due date, etc. It also has a Billing Date, which is the date I'd like to calculate my 'Blackout' dates from.
Basically we're going to be changing every meter, and don't want to change them on a day where they might be billed.
Example Data:
Locations:
111111, Field1, Field2, etc.
222222, Field1, Field2, etc.
Bills (DocNum, LocationID, BillingDate):
BILL0001, 111111, 1/6/2018
BILL0002, 111111, 2/8/2018
BILL0003, 111111, 3/5/2018
BILL0004, 111111, 4/6/2018
BILL0005, 111111, 5/11/2018
BILL0006, 111111, 6/10/2018
BILL0007, 111111, 7/9/2018
BILL0008, 222222, 1/30/2018
BILL0009, 222222, 3/1/2018
BILL0010, 222222, 4/2/2018
BILL0011, 222222, 5/3/2018
BILL0012, 222222, 6/1/2018
BILL0013, 222222, 7/1/2018
BILL0014, 222222, 7/28/2018
Example output:
Location: 111111 BlackOut: 6th - 11th
Location: 222222 BlackOut: 28th - 3rd
How can I go about doing this?
As I understand it you want to work out the "average" day when the billing occurs and then construct a date range based around that?
The most accurate way I can think of doing this is to calculate a distance function and try to find the day of the month which minimises this distance function.
An example function would be:
distance() = Min((Day(BillingDate) - TrialDayNumber) % 30, (TrialDayNumber - Day(BillingDate)) % 30)
This will produce a number between 0 and 15 telling you how far away a billing date is from an arbitrary day of the month TrialDayNumber.
You want to try to select a day which, on average, has the lowest possible distance value as this will be the day of the month which is closest to your billing days. This kind of optimisation function is going to be much easier to calculate if you can perform the calculation outside of SQL (e.g. if you export the data to Excel or SSRS).
I believe you can calculate it purely using SQL but it is pretty tricky. I got as far as generating a table with a distance result for every DayOfTheMonth / Location ID combination but got stuck when I tried to narrow this down to the best distance per location.
WITH DaysOfTheMonth as(
SELECT 1 as DayNumber
UNION ALL
SELECT DayNumber + 1 as DayNumber
FROM DaysOfTheMonth
WHERE AutoNumber < 31
)
SELECT Bills.LocationID, DaysOfTheMonth.DayNumber,
Avg((Day(Bills.BillingDate)-DaysOfTheMonth.DayNumber)%30) as Distance
FROM Bills, DaysOfTheMonth
GROUP BY Bills.LocationID, DaysOfTheMonth.DayNumber
A big downside of this approach is that it is computation heavy.

Reporting and filtering dates in access 2007?

I'm having a customer look up a report between FROM and TO dates. Basically for any month a client can request a report, what I need to do is show the QTY of units in the inventory as of the last day of previous month. So if client is looking to make a report for 12/23/2015 - 12/28/2016 then I need to show the QTY of units in inventory (beginning inventory) on 11/30/2015. I had someone recommend I use DateSerial, but it doesn't seem to do what I want it to do. Basically the FROM AND TO dates are values that I pass on from a Visual Basic application into an empty form and they get used in the report.
I'm not sure if I need to make changes to my queries. Here's what the report does..
With the report, I need to be able to show the client the following...
ItemID
Beginning Inventory (as of last day of previous month)
Unit Price
Bought Units (for report date)
Sold Units (for report date)
Returned Units (for report date)
Subtotal (of bought +sold+returned)
Inventory on hand (beg + subtotal)
So what I do is in my first query I get the Unit Price, and Beginning Inventory (currently not filtering by last day of previous month).
Then I have 3 more queries, one for Bought, Sold and Returned where I link the ItemID's and I filter the TransactionDate (between dates form!formdates!txtFrom and form!formdates!txtTo).
Then I have the last query where I combine the first one with the other 3. Now I'm pretty sure I should include the DATESERIAL() function in the first query? And filter the TransactionDate (this is the column i need to look through to count beginning inv, as well as look for Bought, Sold, Returned Items).
Please help!
The DateSerial() function should return the last day of the previous month. Here is an example query for your problem:
SELECT ID, myDate
FROM Table1
WHERE (((myDate)=DateSerial(Year([enter start date:]),Month([enter start date:]),0))) OR (((myDate) Between [enter start date:] And [enter end date:]));
Documentation: https://msdn.microsoft.com/en-us/library/bbx05d0c(v=vs.90).aspx