TABLEAU: calc field to get the last value available - field

I'm using Tableau Desktop, my data are like this:
KPI,date,monthValue
coffee break,01/06/2015,10.50
coffee break,01/07/2015,8.30
and I want to build a table like this
KPI, year(date), last value
coffee time, 2015, 8.30
How can I set a calculated field in order to show me the last value available in that year? I tried to do:
LOOKUP([MonthValue], LAST())
But it didn't work and tells me 'cannot mix aggregate and non-aggregate', so I did:
LOOKUP(sum([MonthValue]), LAST())
But it didn't work too. How should I proceed?

If you are using Tableau 9 then you can do this with an LOD calc that looks for the max value in your date field and then checks if the current date value is the same as the max date value.
[Date] == {fixed: max([Date])}
As you can see in the example below when you use the calc as a filter you will only get the last row from your example above.
UPDATE: to get the values per year you can do something like:
Here I am using a table calculation to find the max date per year and then ranking those dates and filtering down to the latest date in each year (which will be the one that has a rank equal to 1).
!max date is WINDOW_MAX(ATTR(Date))
!rank is RANK(Date)
You need to make sure that the table calculations are computer in the correct way (in this case across the values of each year).

Related

I need to define a custom work week in MS access

I am trying to create a custom function on a form to define a week Number.
I have created a table that defines the week number.
Example WeekNo, StartDay, End Day
example: WeekNo 1 StartDay = 3/29/2020, End Day 4/4/2020
I have a Date box on my form if I enter a date of 3/29/2020
I would like 1 to be populated in my week number box.
On my form in the row source I have designed a Dlookup query
=DLookup("[WeekNumber]", "tblWeekNumber", "[Startdate] >= " & frmSearchNew.dt_Date & "") & [EndDate] <= frmSearchNew.dtDate
When I change to from view I get the error the record source specified on this form does not exist.
The table tblWeekNumber has the fields ID, WeekNo, StartDay and EndDay.
Where am I going wrong? any help is appreciated.
There are quite a few issues with the DLookup that you have put together.
Firstly, the field that you are looking for and the fields that you are using as criteria do not appear to match those in the table - WeekNumber/WeekNo, StartDate/StartDay, EndDate/EndDay;
Next, the logic for the lookup is wrong. You are trying to find a the week number that has a start date that is greater than the entered date, and an end date that is less than the entered date. What you should be looking for is a start date before the entered date, and an end date after the entered date.
Finally, dates are a bit funny in Access. You need to wrap them in '#' so that Access knows they are dates, and you should also take care to disambiguate them - 03/04/2020 could be either 3rd April or 4th March depending on you nationality.
Putting it all together, the final control source should look like:
=DLookUp("WeekNo","tblWeekNumber","StartDay<=#" & Format([dt_Date],"dd-mmm-yyyy") & "# AND EndDay>=#" & Format([dt_Date],"dd-mmm-yy") & "#")
Regards,

Find if a month belongs to a date range

I have a date range and a corresponding value.
On a the right-hand side of this data table, I want to separate the value monthly.
Available Data
Result Expected
*I have the limits of the date range any row can have. Ex: Mar 1st to Jun 30th.
I can take the number of days in a month and divide the value by that to get the value to fill in.
Problem is automatically deciding the cells which needs to be filled and which has to be marked as 0.
The solution I'm looking for is a formula that can be dragged into many more months.
My approach was to check at each cell if the Month Code("Mar", "Apr", "May"....etc) includes in the date range in A and B columns.
I have searched ways to check this and have failed. Is there a way to check if a month includes within a particular date range?
Or is there any other way I can fill the cells from D2 to G6?
*Actual scenario has more than 4 months and more than 6 Rows of data.
This is a formula giving the exact amounts for first row - not sure if that is what you want though:
=(max(0,min($B2,eomonth(datevalue("1-"&left(D1,3)&"2020"),0)))-max($A2,datevalue("1-"&left(D1,3)&"2020"))+1)*$C2/($B2-$A2+1)
Alternatively
=if(min($B2,eomonth(datevalue("1-"&left(D1,3)&"2020"),0))-max($A2,datevalue("1-"&left(D1,3)&"2020"))>=0 [your formula] ,0)

How to create a single date variable from separate month and year variables in SPSS

I have a data set in which the date of an event is represented by two variables: month of the event and the year of the event. I would like to turn these two variables into a single variable, preferably formatted as a date/time variable. How would the syntax for doing this in SPSS look?
NB. I don't have a variable specifying which day (1-31) of the month a date is (and the day isn't important either). I was thinking of either just using a date format that only include the month and year, or specifying that all events happened on the first of each month.
Image illustrating how the variables look:
Image illustrating how the new date variable should look
From the menu:
Transform/Date and Time Wizard/Create a Date/time variables from...
Then select your Month variable to the Month field, Year variable to Year field
Press Next
Put a name into the 'Result Variable` field (e.g.: "MonthYear")
From the "Output format" choose whichever format you want (e.g.: "mmm yyyy")
I would suggest to "Paste" the syntax, in case you need it later ;)
Press Finish and you will get a syntax looking like this (based on the above inputs):
COMPUTE MonthYear=DATE.DMY(1, Month, Year).
VARIABLE LABELS MonthYear "".
VARIABLE LEVEL MonthYear (SCALE).
FORMATS MonthYear (MOYR8).
VARIABLE WIDTH MonthYear(8).
EXECUTE.

Getting data from a google sheet for the previous XX days

I'm logging calls, emails and meetings using Google sheets. I am trying to get a chart of results based on a number of days in the past, 7, 30, last month, current month, etc. I am able to do exactly what I need to do using a fixed date but I can't figure out how to convert that to a date range.
Here is what is working:
={ARRAYFORMULA({UNIQUE(FILTER(CRM!M2:M2510,CRM!N2:N2510>=VALUE("2018-06-01 00:00:00"),CRM!N2:N2510<=VALUE("2018-08-01 23:59:59"),CRM!M2:M2510<>"")),ARRAYFORMULA(COUNTIF(FILTER(CRM!M2:M2510,CRM!N2:N2510>=VALUE("2018-06-01 00:00:00"),CRM!N2:N2510<=VALUE("2018-08-01 23:59:59")),SUBSTITUTE(SUBSTITUTE(UNIQUE(FILTER(CRM!M2:M2510,CRM!N2:N2510>=VALUE("2018-06-01 00:00:00"),CRM!N2:N2510<=VALUE("2018-08-01 23:59:59"),CRM!M2:M2510<>"")),"*","~*"),"?","~?")))})}
I need to change the value from a fixed date to a number of days in the past. What do I need to change in this formula? I tried using TODAY() -7 but I keep getting an error saying that I am missing brackets. When I use (TODAY()-7) I just get an #ERROR.
How can I change the VALUE("fixed date") to VALUE(TODAY -7)?
If your data looks like "2018-06-01 00:00:00".
Then use this conditions for filter to filter the last 7 days:
Arrayformula(datevalue(left(CRM!N2:N2510),10))>=datevalue(today()-7)
,
Arrayformula(datevalue(left(CRM!N2:N2510),10))<=datevalue(today())

reformating now() function so it works in GoogleFinance function

I'm using GoogleFinance function in Google spreadsheets to display a chart over the last 5 years. I'm using it as follows:
=GoogleFinance(A1;"all";Date(2012,02,08);Date(2013,02,11); "weekly")
This works fine. However, I have to increment the two dates each day if I want the chart to be up to date. I want to change the above so that it automatically updates each day and always shows a chart of the last 5 years. I tried using the now() function like so
=GoogleFinance(A1;"all";Date(2012,02,08);Date(now()); "weekly")
but it seems now() returns the Date in format d/MM/yyyy instead of the required yyyy,MM,dd format.
I also tried.
=GoogleFinance(A1;"all";Date(2012,02,08);Date(Text(Now(), "yyyy,MM,dd")); "weekly")
This did not work either.
Ideally I need something like the following:
=GoogleFinance(A1;"all";Date(now()-5years);Date(now()); "weekly")
The above does not work, how can I change it so my 5 year chart is always up to date.
I have now managed to work out how to fix my problem.
I used the today() function to solve it as follows
=GoogleFinance(A1;"all";TEXT(TODAY()-YEAR(5);""); TEXT(TODAY();""); "weekly")
A1 contains the stock in my case it was GOOG
I can generate the data to create a 5 year chart of any stock now just by changing the value in A1.

Resources