Tableau Calculated Field of Running Average for the last month - tableau-api

Hey Guys I am trying to do a calculation for a Running Average over the last month in a calculated field in Tableau. However I struggle with defining the last month range and adding it to the calculation. I thought of using
IF [date] >= DATEADD('month', -1, TODAY()) THEN
RUNNING_AVG(SUM( IF [Entry Type] = 'Sale'
THEN [Invoiced Quantity]
ELSE 0
END )) END
However this is not working at all. I hope someone can help me out. Cheers

This worked out:
(SUM( IF [Entry Type] = 'Sale' AND [Posting Date] >= DATEADD('month',-1,TODAY())
THEN [Invoiced Quantity]
ELSE 0
END
))

You do not have to write this into calculated field. Just filter date column and set that to be always filter for last 30 days, or last month. Then, use "calculated field" section just for calculation independently from the "date" which is already considered in filter section.
Good luck

Related

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)}

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

How to Create Calculated field for last 7 days sales

I am trying to create one calculated field for last 7 days and want to display same info for like total order value,Total orders, Total Ordering retailers
My Formula for Calculated Field
IF ATTR([CreatedDate])>=TOTAL(MAX([CreatedDate]))-6 THEN
SUM([OrderAmount])
END
Create a new calculated field called [Day Index] that indexes your date field by day:
DATEDIFF('day', [Date], today())
Then a new field per measure to get the value for the last 7 days:
IF [Day Index] <= 6 THEN [Total Orders] END
The 6 assures you data source includes the current day also, if it doesn't then you may want to adjust this to 7.
create calculation :
[order Date] >= (TODAY()-7)
drag this calculation into filter and select TRUE

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)

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.