Tableau - compare average quantity before and after variable date - tableau-api

I have a dataset containing client names, license codes, billing dates, and billing quantity. What I need to do is analyze average quantity before and after a specific date. How I find that particular date per client is by looking for specific billing codes and returning the minimum date in that subset of data. I have attached a sample of the data.
For example, I need to find the minimum date for each name where the Billing code begins in “E”. For “Allen” this date would be August 2015. For “Ama” is would be May 2015. I then want to compare the average monthly quantity for those codes NOT beginning with “E” before the minimum date, and the average monthly quantity for those codes that do begin with “E” following that minimum date. For example, “Allen” would show approximately 50 units on average before August 2015 and approximately 78 units on average after August 2015, inclusive. “Ama” would show 19 and 24, respectively.
Ideally I would like to run a regression of the average quantities for each firm.
NAME|BILLING DATE|BILLING CODE|QUANTITY
----|------------|------------|--------
Allen|Jan-15|A11|64
Allen|Feb-15|A11|64
Allen|Mar-15|A11|64
Allen|Apr-15|A11|64
Allen|May-15|A11|65
Allen|Jun-15|A11|1
Allen|Jul-15|A11|1
Allen|Aug-15|A11|1
Allen|Sep-15|A11|1
Allen|Oct-15|A11|1
Allen|Nov-15|A11|1
Allen|Dec-15|A11|1
Allen|Jan-16|A11|1
Allen|Feb-16|A11|1
Allen|Mar-16|A11|1
Allen|Apr-16|A11|1
Allen|May-16|A11|1
Allen|Jun-16|A11|1
Allen|Jul-16|A11|1
Allen|Aug-16|A11|1
Allen|Jan-15|A22|4
Allen|Feb-15|A22|4
Allen|Mar-15|A22|4
Allen|Apr-15|A22|4
Allen|May-15|A22|4
Allen|Jun-15|A22|4
Allen|Jul-15|A22|4
Allen|Aug-15|A22|4
Allen|Aug-15|E11|38
Allen|Sep-15|E11|36
Allen|Oct-15|E11|40
Allen|Nov-15|E11|40
Allen|Dec-15|E11|40
Allen|Jan-16|E11|40
Allen|Feb-16|E11|40
Allen|Mar-16|E11|38
Allen|Apr-16|E11|38
Allen|May-16|E11|40
Allen|Jun-16|E11|40
Allen|Jul-16|E11|40
Allen|Aug-16|E11|39
Allen|Oct-15|E22|40
Allen|Nov-15|E22|40
Allen|Dec-15|E22|40
Allen|Jan-16|E22|40
Allen|Feb-16|E22|40
Allen|Mar-16|E22|38
Allen|Apr-16|E22|38
Allen|May-16|E22|40
Allen|Jun-16|E22|40
Allen|Jul-16|E22|40
Allen|Aug-16|E22|40
Ama|Jan-15|A11|21
Ama|Feb-15|A11|20
Ama|Mar-15|A11|20
Ama|Apr-15|A11|20
Ama|May-15|A11|20
Ama|Jun-15|A11|20
Ama|Jul-15|A11|20
Ama|Aug-15|A11|20
Ama|Sep-15|A11|18
Ama|Oct-15|A11|18
Ama|Nov-15|A11|18
Ama|Dec-15|A11|18
Ama|Jan-16|A11|18
Ama|Feb-16|A11|18
Ama|Mar-16|A11|18
Ama|Apr-16|A11|18
Ama|May-16|E11|24
Ama|Jun-16|E11|24
Ama|Jul-16|E11|28
Ama|Aug-16|E11|21

First, create a LOD calc that determines the cutoff date per client. Say start with a fixed LOD calc. See the docs on LOD calcs. For example, define Cutoff_Date as:
{ fixed Name : min(if startswith([Billing Code], "E") then [Billing Date] end) }
Then write a pair of calculated fields that return the quantity if the date is before (after) the cutoff date or null otherwise. Hint, in an if statement without an else clause, the default implicit else condition returns null.
For example, define Quantity_Before_Cutoff as:
if [Billing Date] < [Cutoff_Date] then [Quantity] end
and define Quantity_After_Cutoff similarly -- deciding which calculation should include quantities that were billed on the cutoff date.
Finally, you can use your 2 new measures as desired to calculate avg quantities before and after the cutoff date.
For your regression model, you might also want a Boolean valued calculated field that indicates whether a record is before or after the cutoff. Make it a dimension instead of a measure

Related

How to count events after set date with COUNTIFS?

I'm creating this report in spreadsheet
https://docs.google.com/spreadsheets/d/1U48MybVshKT3eRYE9goRCab9k1KCoXe7Zsr9Ogkkm4Y/edit?usp=sharing
In "BD" you can find the records and in "NB-month" the KPI that I want to analyze; I would like to create DATE filter for the numbers in the columns, ex for the col: "nº Contract closed" I'm thinking to use this formula:
=COUNTIFS(BD!R:R;A5;BD!G:G;">"&DATE(B2))
but I get a value: "0" that is incorrect because for this agent "Carla Vaello" the number of contracts closed after 2020-02-01 should be "2".
your usage of DATE formula is wrong (DATE requires 3 parameters). either change DATE to DATEVALUE or use:
=COUNTIFS(BD!R:R; A5; BD!G:G; ">"&B2)

Calculate product rate of previous day in tableau

I have a sample dataset:
DATE: 11-01-2015, 12-01-2015, 13-01-2015
SALE: $120, $0 , $100
In tableau I want if Today's sale is 0 as given above for 12-0-2015 then previous day sale should be considered as today's sale.
Can anyone help?
Create a calculated field with this formula:
IF SUM([Sale])=0
THEN LOOKUP(SUM([Sale]),-1)
ELSE SUM([Sale])
END
And compute using Date.
IF FIRST() <> 0 AND SUM([Sale])=0 THEN LOOKUP(SUM([Sale]), -1)
ELSE SUM([Sale]) END
Setting Compute Using to Date
Checking First() in the condition handles the case when the first day had zero sales

Tableau - Divide values from 2 dates

I need help making a division with a relative date range.
For example:
Date Range: 01-01-2015 & 20-01-2015
Divison: 1.209,812 / 1.207,810 = 1,0016575
You can do this with a combination of LOD calcs and a Context Filter.
Select Add to Context on your relative range date filter.
Create Level of Detail calcs to isolate the min and max dates based on your filter
{max([Order Date])}
and
{min([Order Date])}
Create min and max values based on your measures. I'm using the Superstore data set in this example. The calculation states if the date equals the max date, return the Sales value. Repeat for min values.
if [Order Date] = [max date] then [Sales] end
You should have something like this:
Now just create a division between the max and min. You'll need to remove the date from the view for the calculation to render.
sum([max value]) / sum([min value])
See attached sample workbook if needed. https://www.dropbox.com/s/1ed15pwhihmjkdv/181227%20stack%20question.twbx?dl=0

Create a measure to return the maximum date about the order in a fact table. (SSAS Multidimensional)

I want to create a measure which return the maximum date about Orders but before the actual day
I will write an example :
My tables here
(In my table Calendar i have the year 2016,2017,2019, and in my Order table, i have an order for 2016 and 2019,
I want the last date order but before the actual day (18/05/2017), so i want the Date 01/01/2016).
I have 2 table, a dimension Calendar and a fact table Order.
I was thinking about the function filter, so i search how to use filter in
google, and all the solutions i found use 'With' and 'Select'.
(I can't use 'With' and 'Select' when i create a measure in SSAS multidimensional).
Hope i will see your advice.
Just like this similar case in adv cube?
[max order date] return the maximum date about [Internet Sales Amount]
with member [max order date] AS
tail(NONEMPTY([Date].[Date].[Date],[Measures].[Internet Sales Amount])).item(0).item(0).PROPERTIES( "name" )
select {[max order date] } on 0 from [Adventure Works]
if yes, then you can create a measure in your cube like this:
Create Member CurrentCube.[Measures].[max order date]
As tail(NONEMPTY([Date].[Date].[Date],[Measures].[Internet Sales
Amount])).item(0).item(0).PROPERTIES( "name" );
if only till current day, then(following is refer to adv cube, you need do some code changes per your cube):
Create Member CurrentCube.[max order date] AS
Tail
(
NonEmpty
(
{
Head([Date].[Date].[Date]).Item(0).Item(0)--the first day in your Date dim
:
StrToMember("[Date].[Date].&[" + Format(Now(),"yyyyMMdd") + "]")-- as of current day
}
,[Measures].[Internet Sales Amount]
)
).Item(0).Item(0).Properties("name")
IDE to Write, Analyze, Tuning, Debug MDX efficiently (www.mdx-helper.com)

Merging average of time series corresponding to time span in a different data set

I have two datasets, one with contracts and one with market prices. The gist of what I am trying to accomplish is to find the average value of a time series that corresponds to a period of time in a cross-sectional data set. Please see below.
Example Dataset 1:
Beginning Ending Price
1/1/2014 5/15/2014 $19.50
3/2/2012 10/9/2015 $20.31
...
1/1/2012 1/8/2012 $19.00
In the example above there are several contracts, the first spanning from January 2014 to May 2014, the second from March 2012 to October 2015. Each one has a single price. The second dataset has weekly market prices.
Example Dataset 2:
Date Price
1/1/2012 $18
1/8/2012 $17.50
....
1/15/2015 $21.00
I would like to find the average "market price" (i.e. the average of the price in dataset 2) between the beginning and ending period for each contract on dataset 1. So, for the third contract from 1/1/2012 to 1/8/2012, from the second dataset the output would be (18+17.50)/2 = 17.75. Then merge this value back to the original dataset.
I work with Stata, but can also work with R or Excel.
Also, if you have a better suggestion for a title I would really appreciate it!
You can cross the contracts cross section data with the time series, which forms every pairwise combination, drop the prices from outside the date range, and calculate the mean like this:
/* Fake Data */
tempfile ts ccs
clear
input str9 d p_daily
"1/1/2012" 18
"1/8/2012" 17.50
"1/15/2015" 21.00
end
gen date = date(d,"MDY")
format date %td
drop d
rename date d
save `ts'
clear
input id str8 bd str9 ed p_contract
1 "1/1/2014" "5/15/2014" 19.50
2 "3/2/2012" "10/9/2015" 20.31
3 "1/1/2012" "1/8/2012" 19.00
end
foreach var of varlist bd ed {
gen date = date(`var',"MDY")
format date %td
drop `var'
rename date `var'
}
save `ccs'
/* Calculate Mean Prices and Merge Contracts Back In */
cross using `ts'
sort id d
keep if d >= bd & d <=ed
collapse (mean) mean_p = p_daily, by(id bd ed p_contract)
merge 1:1 id using `ccs', nogen
sort id
This gets you something like this:
id p_contract bd ed mean_p
1 19.5 01jan2014 15may2014 .
2 20.31 02mar2012 09oct2015 21
3 19 01jan2012 08jan2012 17.75