How to Create Calculated field for last 7 days sales - tableau-api

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

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

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

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.

Qlikview Automate past 12 months selection

I have a combo chart which shows the average days it took for a person to pay their bill.
The Dimension of the Chart is = [Pay Month Year last 12 months]
There are no Dimension limits
There is 1 expression which is called Average and its definition is:
avg({< InvoicefromSqlType = {'Invoices'},[Is Invoice Paid] = {'Y'},[Is Positive Amount] = {'Y'},[Is Paid last 12 months] = {'Y'},DueGroups=,[Pay Month Year last 12 months]=>}[Days to Pay])`
Its is sorted by expression, which is [Pay Month Year last 12 months]
Now the above fields are built up like this:
[Pay Month Year last 12 months]
If([Pay Date] >= '$(vPeriodS12)',[Pay Month year]) as [Pay Month Year last 12 months],
PayLoadOrder:
Load * Inline [Pay Month Year last 12 months
May-2014
Jun-2014
Jul-2014
Aug-2014
Sep-2014
Oct-2014
Nov-2014
Dec-2014
Jan-2015
Feb-2015
Mar-2015
Apr-2015
May-2015
];
Now what is happening is every month when it reaches the end, the next month is needing to be manually added and the first month removed (e.g. in the above I would delete the line May-2014 and at the end add the line Jun-2015)
Also if there are months defined which there is no data for yet i.e. you have Jun-2015 hard coded and the current month this May-2015 then Jun-2015 will show data from 2014 and the order of the months will get mixed up.
What I want to do is completely remove the need to hard code the months above and have this done its self.
If there are any more information you need let me know
Instead of using a "sort order" table that you may have to manually update, it may be worth doing the following:
Create a new field derived from [Pay Date] that returns a month and year that you can sort. For example:
dual(date(makedate(year([Pay Date]),num(month([Pay Date]))),'MMM-yyyy'),
year([Pay Date]) * 100 + num(month([Pay Date]))) as PayMonthYear
Here, the dual function allows you to associate a different representation of a field value to its underlying value. For example, here we set the underlying data to be the year of [Pay Date] added to the month, but state that it should be displayed as MMM-yyyy. For example, internally, QV still sees the value 201502, but displays it as Feb-2015. This means you can sort it correctly based on its underlying value.
Using dual is a big topic, please consult the built-in help for QV for more information.
Change your chart dimension from [Pay Month Year last 12 months] to use PayMonthYear and set the sort to ascending. This would then mean that your months would be sorted correctly, even if a new month was added.
Remove table PayLoadOrder from your script.
Alternative Method
An alternative would be to use a calendar table which joins on your Pay Date field. This would achieve the same thing, however, you could also integrate your "year to date" indicator into the calendar as well and remove it from your main table. An example I quickly threw together is shown below:
MinMax:
LOAD
Max([Pay Date]) AS MaxDate,
Min([Pay Date]) AS MinDate
RESIDENT MyData;
LET varMinDate = Num(Peek('MinDate',0,'MinMax')); // 0 is first record
LET varMaxDate = Num(Peek('MaxDate',-1,'MinMax')); // -1 is last record
LET varToday = Num(Today());
MasterCalendar:
LOAD
monthstart([Pay Date]) >= monthstart(AddMonths(Today(),-12)) as PaidInLast12MonthsFlag,
dual(date(makedate(year([Pay Date]),num(month([Pay Date]))),'MMM-yyyy'),year([Pay Date]) * 100 + num(month([Pay Date]))) as PayMonthYear
[Pay Date];
LOAD
date($(varMinDate) + RecNo() - 1,'DD/MM/YYYY') as [Pay Date]
AUTOGENERATE num($(varMaxDate)) - num($(varMinDate)) + 1;
DROP TABLE MinMax;
So, in the above, the field PaidInLast12MonthsFlag is equal to -1 if the value of the [Pay Date] field occurs in the last 12 months, 0 otherwise. You could use this in your set analysis expression as a filter. Furthermore, you can use PayMonthYear as your chart dimension.