Tableau - Divide values from 2 dates - tableau-api

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

Related

DAX Calculate Billing Days Between Two Variable Dates

I have a dimdate table that is represented below. I have each day flagged as BusinessDay Y/N. I also have a DimSalesRep table that has a daily goal for each rep. I want to be able to allow users to input a StartDt and EndDt with filters on the report and have a calculated column look at the business days between those dates. I can calculate daysbetween with defined dates but I am unsure how I would use DAX with variable dates that are applied through Report filters.
I should also note I am not sure how best to handle a startdt and enddt filter based of the column, TheDate
Cheers!
Reference your dimdate table twice
StartDate = 'dimdate'
EndDate = 'dimdate'
and use this measure:
Num BusinessDays =
CALCULATE(
COUNTROWS('dimdate'),
'dimdate'[BusinessDay] = "Y",
'dimdate'[Date] >= SELECTEDVALUE(StartDate[Date]),
'dimdate'[Date] <= SELECTEDVALUE(EndDate[Date])
)

Get value that is outside scope of filter

I'm working on a tableau project with long format table data displaying start months, end months, and monthly costs. I'm trying to display a visual of a (dynamic) range of dates' monthly costs, and then overlay a reference line of the multi-month row's total cost. I suspect the answer lies in using a level of detail (LOD) expression but can't seem to figure this out.
example csv:
start,end,month length,x,a,b,c,d,total
1/1/2019,1/1/2019,1,1.2,0.08,0.01,0.1,0.299,0.489
2/1/2019,2/1/2019,1,1.1,0.08,0.01,0.1,0.295,0.485
3/1/2019,3/1/2019,1,0.9,0.08,0.01,0.12,0.285,0.495
1/1/2019,2/1/2019,2,2.3,0.08,0.01,0.1,0.297086956521739,0.487086956521739
2/1/2019,3/1/2019,2,2,0.08,0.01,0.109,0.2905,0.4895
1/1/2019,3/1/2019,3,3.2,0.08,0.01,0.105625,0.2936875,0.4893125
ignore x,a,b,c,d - they are to show that the chart has multiple measures equaling the monthly totals.
For identical start and end months, we have a month length: 1. The values associated with these rows are the monthly values. For graphing a dynamic range of months, I can graph these monthly values by filtering the month length: 1 and adding a calculated field filter for setting the start date to end date range [Start Date] >= [Selected Start Date] AND [Start Date] <= [Selected End Date]
Given this table, how might I show a reference line for the total value that exists for the start to end range selected (start date: 1/1/2019, end date: 3/1/2019, month length: 3), even though there is a filter of month length: 1? The end result here would be a line of 0.4893125
Yes, you are correct, you need to use LoD.
If you need to bypass the filter then use LoD Fixed, below code snippet will give you the data for month = 3 though the filter is month=1. Change the code as per your requirements:
{ FIXED : Max(IF [Month Length] = 3 THEN [A] END)}
Edit----------------------------------------------------------------------
{ FIXED :
Max(
IF ([Start Date] >= [Selected Start Date] AND
[Start Date] <= [Selected End Date]) AND
[Month Length] = 3 THEN [A] END)}

How to dynamically calculate the YTD for current year without using a table calculation in Tableau?

How do I dynamically calculate the Year To Date (YTD) for the current year without using a table calculation in Tableau?
I have used the below formulas to calculate YoY for the current year:
if datediff('year',[Date],TODAY())=0 then [Sales] END
For the previous year:
if datediff('year',[Date],TODAY())=1 then [Sales] END
YoY:
sum(current year)/sum(previous year)-1
create a calculated field:
[date] >= MAKEDATE(Year(today()),1,1) and
[date]<= today()
Drag this to filter and select True
It depends on what you're trying to achieve. If you want to filter dates to show only values in the current year without a table calculation, then you could create a calculated field like below and filter on the result:
if Year([Date]) = YEAR(TODAY()) then "YTD" else "Not" END

Tableau - compare average quantity before and after variable date

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

Using a single Calculated field , can I get the number of weekdays calculated from the current month in Tableau?

I have a requirement to do % of (Workdays till date) / (Total Workdays of the month). How can I create a "Calculated field" for this logic. I don't need to consider holidays or any sort. Any help is highly appreciated.
A quick google search turned up this:
http://kb.tableau.com/articles/knowledgebase/calculating-the-number-of-business-days
In the Calculated Field dialog box, do the following and then click
OK: Name the calculated field. In the example workbook, the calculated
field is named Number of Weekdays. In the formula field, create a
calculated field similar to the following:
DATEDIFF("weekday", [Start Date], [End Date])
- 2 * (DATEPART('week', [End Date]) -DATEPART('week', [Start Date]))
+ (IF DATENAME('weekday',[End Date]) = 'Saturday' OR DATENAME('weekday',[Start Date]) = 'Sunday'
THEN 0 ELSE 1 END)
In your example you take the difference between the first and the last of a month and calculate the working days by subtracting 2 * [number of weeks] for the weekends. Once you have that value you can easily create the ratio you wanted.