Tableau - Fixed calculated field depending on date filter - filter

I'm new to Tableau. I am trying to make an inventory report which tells the user how much of certain product he/she should buy in advance.
Depending on the amount of days selected on the filter, the difference in days of the complete period should be calculated. For example: If the filtered dates are from 1/03/2021 to 09/03/2021, the result should be equal to 9. The formula I used is the following: date_difference = DATEDIFF("day",MIN(DATE([Fecha])), MAX(DATE([Fecha]))) + 1
The problem comes when I try to use the value given by such date filter. My next calculation should be:
calc = Quantity Inventory / (Units sold / date_difference). Both Quantity Inventory and Units sold are calculated fields in which I have no problem. However, instead of having a fixed value of 9, date_difference changes as shown in the image, giving me incorrect results for the desired calculation.
How can I make sure that the calculated field date_difference has the value of 9 on all rows?. Actually, if I add date_difference field by itself in a different Page it does show the proper value. The problem occurs when calculating calc and trying to add it to the table.
Note: Remember that the value of date_difference will change, depending on the range of time selected on the date filter
Thanks a lot in advance.

Step-1 Use this calculation for date_difference instead
DATEDIFF('day', {min(DATE([Fecha]))}, {max(DATE([Fecha]))}) +1
Step-2 Add Fecha filter to context, by right clicking it in filters shelf.
This will solve your problem. See the GIF

Related

DAX ignore row context in measure. Calculate for a defined set of dates and show the value for all dates in visual

I am trying to create a measure which calculates the average daily revenue per customer, but only using days in a 6 months period prior to a specific date (where some type of conversion happens).
This specific date can be different for each customer.
The intention is to use the measure as a baseline for indexing daily average in the days/months/years after said conversion date.
If I put my current version of the measure in a card it works just fine (circled in green). But I will eventually have to visualize this over time as well. Thus I need the value to stay the same regardless of the row/date context in a table or timeline (circled in orange).
I suspect I need to use one of the ALL/ALLSELECTED/ALLEXCEPT filter modifiers but I can't really get anything to work.
The measure looks like this for now:
Average daily rev before conversion = CALCULATE (
AVERAGEX(
VALUES('Date'[Date]),
[HI & Acc Rev]
),
FILTER('poc vFact_SalesLine','poc vFact_SalesLine'[OrderDate_ID] IN DATESINPERIOD('Date'[Date],FIRSTNONBLANK('poc vDim_Customer'[DSE first conversion date],1),-6,MONTH)))
I've tried adding REMOVEFILTERS('Date'[Date]) just before the filtering of order dates, but that doesn't work. Gives me the exact same values as shown below.
All help is very welcome? Is my approach all wrong?

Netsuite Formula Calcs

So I was able to create a search that shows historical unit rates at an item level then I can filter by customer to show any price increases that may have taken place to create a price tracker. It works as advertised. Below is a screen shot of what the output is. "Base Price" is the minimum item rate for lifespan since we transitioned to NetSuite. Then 1-6 months back it will pull the item rate and as you can see, around the 3rd month, it increases and shows change.
Here is how the search looks when executed
What I am attempting to do now subtract the values from the formula from the "base price" because that is at the end of the day the total impact value. Eventually I want to bring in quantity so we can see the total impact of these changes to track if we are seeing an increase or not.
EX. If base is $2 and we sell 20 a month. $40 in sales. Now we up it to $3, sales would be $60. But we want to show the $20 in increase impact instead.
Below is from the results tab to generate the above.
Here is the view from the Results Tab
Is there a way to create that calculation somehow in NS? I am almost thinking it is because I used the DECODE instead of the CASE WHEN ?
Thank you
You can generally combine supported functions; you just need to make sure that any functions applied in the Summary or Function columns are replicated in the formula. For example, your first column "Item Rate" has a Summary function or "Minimum" applied - this needs to be included in the formula, something like:
DECODE(...) - MIN({rate})

Spotfire Calculation using previous rows calculated data

I have been struggling with the following calculation. I have tried a few previous, next and overs but I cant seem to get the syntax correct.
Basically i need to subtract demand from stock on hand, to get a new column. the the next row will use the newly created column as stock on hand and the subtract the demand for that row, then that result becomes the new stock on hand etc. i cant get it to loop. I have ranked the demand in order of date required per plant. AS the data set will have multiple Plants, SOH and demand.
The attached pic shows A020 only has one QTY short so that is straight forward, but for A030 opening SOH is 152, and the 1st date QTY short is 12, so i need 152-12 = 140. then the second date QTY which is ranked 2, needs to be 140 - 12 = 128, so then rank 3 uses 128 - 12 and so on. ie the SOH needs to dynamically update.
data set
It might not be natively possible in point-and-click Spotfire (happy to be corrected if this is incorrect).
You should consider writing a data function using R to do this groupby-loop operation.

Setting parameter controls and using filters

I currently have a dataset of speeding fines in NSW.
I've created a bar chart that displays the top 10 frequencies of speeding offenses. These offenses are split up between school zone offenses and normal speeding offenses.
I'm trying to set a parameter control where the end-user can toggle showing the top 10 normal speeding offenses (pink), top 10 school zone offenses (purple) or both together (pink and purple).
I've gotten the below parameter control:
However nothing happens to the bar-chart.
My Dataset looks like the following:
I have a School Zone Indicator boolean that is a Y if the speeding offense occurred during school zone time.
Can someone help me figure out in filtering this out when setting parameter controls?
The answer is based on this article showing how to add all to a parameter in Tableau.
We need to create a parameter with the same values as the field. As your field is either Y or Null let's create a new field to convert the Null to N. Call it isSchool with the formula:
IFNULL([SCHOOL_ZONE_IND],"N")
Create a parameter based on that field with the values Y, N and All. Display As whatever you want, so you can display parameter value All as Show Both if you wish.
Put [isSchool] to filters.
In the filter you want to filter Condition - by Formula. Enter this formula:
IFNULL([isSchool],"1")=IF [YourParam]!="All" THEN [YourParam] ELSE IFNULL([isSchool],"1") END
That should do it.

Rank only for latest month

I've a calculated field "Total Revenue" which blends revenue from multiple data sources in Tableau.
Based on this field (aggregate field) on monthly basis, I would like to show only TOP 5 items with highest revenue as of latest month.
As shown in above table, items have to be filtered out (as they are TOP 5 in March) based on revenue data as of the latest month (March). How can this be achieved using RANK()? I'm not able to rank only for latest month as formulated below because it shows error as I cannot mix aggregate and non-aggregate functions.
IF DATETRUNC('month'),ReportDate)=//March 1st date given//
THEN RANK(Total Revenue)
END
My solution is a kind of workaround but seems to be working:
Create a calculated field with below formula:
IF DATEPART('month', {MAX([date])}) = DATEPART('month', [date])
and DATEPART('year', {MAX([date])}) = DATEPART('year', [date])
THEN
[revenue]
ELSE
0
END
The {MAX([date])} part on the code gets the maximum date in your data, it is fixed with { and } characters so that the value is not effected by the filters, date partitions etc. If you want the sorting month to be the one we are in then you should change that part with NOW()
Now we have a value containing only the sum of the latest month and we can sort our visual with this Measure.
You can drag your new measure (I called my measure: 'last month revenue') to the details and right click your item pill on the Rows, sort it by your new field.
And finally drag your item to the filters and go to Top tab, make filter your data according to sum of "last month revenue" measure.
Below screenshot shows the excel data and the final Tableau table:

Resources