How to calculate value for past periods in Google Data Studio - date

I have a page with a control filter witch is a list of years (I don't want to use a range period selector but allow users to click on year they want to analyse instead)
I am trying to set 4 scorecard in my GDS that show the value for the selected year, selected year-1, -3 and -maximum.
Here is my dataset :
Date | Value
31/12/2020 | 20
31/12/2019 | 10
31/12/2018 | 5
31/12/2017 | 2
30/09/2016 | 1
For example, in the date control list, if I choose 2019, I should have :
Score card °1 "Year" : value = 10 (corresponding to 2019)
Score card °2 "Year-1" : value = 5 (corresponding to 2018)
Score card °3 "Year-3" : value = 1 (corresponding to 2016)
Score card °4 "Year-max" : value = 1 (corresponding to 2016 since it is the older year)
Or for example, in date control list, if I choose 2020, I should have :
Score card °1 "Year" : value = 20 (corresponding to 2020)
Score card °2 "Year-1" : value = 10 (corresponding to 2019)
Score card °3 "Year-3" : value = 2 (corresponding to 2017)
Score card °4 "Year-max" : value = 1 (corresponding to 2016)
I have tried with filters, calculated fields, mixted data, etc... But never find a way to achieve this.
Does anyone have an idea ?
Thanks in advance!

You need to create a parameter "year" with the type number.
Create a field "year differences", which is the year of the date minus the value of the parameter. So the formular should look like: YEAR(Date)-year
For the Score card 1 to 3, a filter has to be created on the field "year differences"
In "Score card °4" you ask for the oldest year. This cannot be done by a filter. Is it ok for you to display this value in a normal table? The set the rows per page to 1 and the order by date ascending.

Related

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

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

LOD (Level Of Detail) where Year =

Trying to write a Calculation in Tableau that allows me to Fix Level of Detail on Category where year equal a designated date.
Imagine looks something like
{FIXED [REPORT CATEGORY] : AVG(DOLLARS PAID)} WHERE YEAR = 2015
You have a couple of options.
1) You add the YEAR dimension to the LOD calculation and then add the Year to the filter card and set to '2015'. Your calculation would look like this:
Since you have no YEAR, you need to create a calculated field for it then reference it.
"YEAR" : YEAR(APCD PAID ACCNTS DATE)
{ FIXED [REPORT CATEGORY], [YEAR]: AVG(DOLLARS PAID) }
2) You can restrict the aggregation to only a specific year in the calculation, like so:
{ FIXED [REPORT CATEGORY]: AVG(IF YEAR([APCD PAID ACCNTS DATE]) = 2015 THEN DOLLARS PAID END) }
-- EDIT --
Updated above because you stated you are using a DATE not a number of the year.

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.

crystal reports month date range for earnings

My client has a report that accepts a date range to get a report showing projected revenue. So, a user would enter a date range of '1/1/2015 to 1/31/2015' and the report should return data only in the range '1/1/2015 to 1/31/2015 grouped by week. I am instead for the week of 12/29/2014 (which 1/1/2015 fall into) and 2/1/2015 (which 1/31/2015 falls into). The report is intended to group by week, but I do not want days on the report that are earlier than the start date parameter or later than the end date parameter.
The sql statement for this report is:
SELECT job.job, job.status, job.customer_po, job.part_number, job.unit_price,
job.price_uofm, delivery.promiseddate, delivery.remaining_quantity, job.build_to_stock, job.description, job.make_quantity, job.pick_quantity, job.shipped_quantity, job.lead_days
FROM dbo.delivery as delivery RIGHT OUTER JOIN db.job as job on delivery.job = job.job
WHERE job.build_to_stock = 0 AND (job.status = 'active' OR job.status = 'hold' OR job.status = 'pending')
The date range is from this code and parameters:
Max – Maximum(?Date Range)
Min – Minimun(?Date Range)
Date Range - "From " & {#Min} & " to " & {#Max}
This is the group expression
Group 2 Name - GroupName ({#Adj Date 2}, "weekly") & " thru " & cdate(GroupName ({#Adj Date 2}, "weekly"))+6
This is the select expression
{#Date} = {?Date Range} and
not {Job.Build_To_Stock} and
{Job.Status} in ["Active", "Hold", "Pending"]
Do you know how I can prevent the "overflow" of dates outside of date range?
Thx
As long as you have date filtering in your record selection formula there will not be any "overflow" outside of that range. If you've got {Record.Date} in Minimum({?DateRange}) to Maximum({?DateRange}), which it sounds like you do, then your report will not contain any records outside of the parameter regardless of how you group them.
Your problem might stem from over-complicating or misinterpreting the grouping. All you need to do is group by {Record.Date} and select "Group by week" in the grouping options... you don't need any complicated formulas to break it out by week. But be aware that the way weeks are referred to is by their starting date. For example, if you had a record with a date of Feb. 19, 2015, that record would fall into the group labeled "Feb. 15, 2015" even if your {?DateRange} parameter was Feb. 18 - Feb. 15.