How to calculate fraction in datediff function in Tableau - tableau-api

I am not really an expert in Tableau. I have a need to calculate a timedifference in hours, but also want to see fraction of an hour. I am using Tableau 9.
I used the function
IF DATEDIFF("hour", [CL2_Start_Time_ST], [CL2_End_Time_ST]) > 8 then NULL
ELSE DATEDIFF("hour", [CL2_Start_Time_ST], [CL2_End_Time_ST])
END
If the time difference between CL2_Start_Time_ST and CL2_End_Time_ST is less than 1 hour (for example 30 minutes) the result is 0, but I want to see 0.5 in result.
I dont want to calculate in time difference in minutes since all my other calculations are in hours and hence it is easier to create a relative plot with other calculations.
Please help.

I found the answer to the above question. The simple formula below worked. I was using DIV function and that caused the issue.
IF DATEDIFF("hour", [CL2_Start_Time_ST], [CL2_End_Time_ST]) > 8 then NULL
ELSE (DATEDIFF("minute", [CL2_Start_Time_ST], [CL2_End_Time_ST])) / 60
END

Related

SSRS 2008 Calculated Chart X-Axis Interval

I am trying to create a chart where the interval on the X-axis will redefine itself depending on the 2 time parameters I have set up for the report; I have a #StartTime and a #StopTime. If the difference between the dates is one day, I want the graph axis to show a mark at each whole hour during that day. But any more than a day then the interval can be automatically determined by the program.
I have manually set the difference between the 2 parameters to be 1 day and set the interval and interval type to "24" and "hours", respectively, which gives me the desired results.
I have tried the following function for the interval:
=IIf(DateDiff("d",Parameters!StartTime.Value,Parameters!StopTime.Value)=1, "24", "Auto")
And I have tried the following function for the interval type:
=IIf(DateDiff("d",Parameters!StartTime.Value,Parameters!StopTime.Value)=1, "Hours", "Auto")
I created 2 random textboxes into which I inserted these functions to test if the functions are actually working, and they are. So I can't figure out why the functions won't execute properly when inserted into the interval properties fields.
Why is this not displaying the desired outcome?
Instead of using the text "Auto", use a Nothing to represent Auto.
eg:
=IIf(DateDiff("d",Parameters!StartTime.Value,Parameters!StopTime.Value)=1, 24, Nothing)
If I recall correctly, you can't change the the axis type at render, which seems like what your trying to do. Honestly I think this might be a case for cheating, make the chart twice, one for each axis type you want, and then conditionally hide them based on a similar Iif statement to the ones you have above.
Other thoughts, 24 is not the interval you want, interval is how much of the x-axis is displayed as tic marks. 1 means 1 tic for every 1 value, 2 means every other one, and 24 if it works would be showing every 24th value (haven't tried it). You want a number interval with a type of hour and an interval of 1 because you want to show every hour. You'll also have to change your x axis to:
=datepart(hour, Fields!YourDateTime.Value)
Otherwise the numbers axis type won't work. You can use similar expressions to create day over day time comparisons. Anyway, good luck!

Calculating time of simulation

I am doing simulations for
time step = 1.0 e^-7 &
total number of steps ==> nsteps = 1.0 e^8
I have to find the total time of simulation at which n# steps is achieved.
Is it ok to multiply both to get the time of simulation?
time of simulation = time step * total number of steps
time of simulation = 1.0 e^-7 * 1.0 e^8
time of simulation = 10
Is this right or wrong?
Thanks in advance.
This is a yes/no question, so:
No, (or yes, depending on how accurately you want the answer to be)!
But you are really close... You need to subtract one time_step, thus the answer is really:
time_of_simulation = time_step * total_number_of_steps - time_step;
You will see the reason if you consider counting seconds. Start with a number and see how far you get if you count one second at a time.
1, 2, 3 => Three measurements, but only 2 seconds.
However, in your case, I guess you are close enough without the last subtraction, because
time of simulation = 9.999999 is pretty close to 10

Iterate for loop by hour in MATLAB

I am writing a for loop to average 10 years of hourly measurements made on the hour. The dates of the measurements are recorded as MATLAB datenums.
I am trying to iterate through using 0.0417 as it is the datenum for 1AM 00/00/00 but it is adding in a couple of seconds of error each time I iterate.
Can anyone recommend a better way for me to iterate by hour?
date = a(:,1);
load = a(:,7);
%loop for each hour of the year
for i=0:0.0417:366
%set condition
%condition removes year from current date
c = date(:)-datenum(year(date(:)),0,0)==i;
%evaluate condition on load vector and find mean
X(i,2)=mean(load(c==1));
end
An hour has a duration of 1/24 day, not 0.0417. Use 1/24 and the precision is sufficient high for a year.
For an even higher precision, use something like datenum(y,1,1,1:24*365,0,0) to generate all timestamps.
To avoid error drift entirely, specify the index using integers, and divide the result down inside the loop:
for hour_index=1:365*24
hour_datenum = (hour_index - 1) / 24;
end

How to convert a MATLAB serial date number to a calendar date?

This is not so much a programming question as it is a desire for clarification to understand the way the Matlab serial date number format works. I'm referencing Matlab serial date numbers and I would like to know how to convert them into calendar dates using the simplest means possible. Many of the references out there only instruct how to work with these numbers within Matlab itself, but I want to be able to make sense of these by hand using arithmetic to derive the dates they represent without the use of Matlab (if this is even possible).
From the research I have done already, I do understand that a Matlab timestamp is essentially a count of the number of days since January 1, 0000 and that the floating point numbers appended to it further represent specific hours, minutes, etc. I'm not as clear on how these break down however. The reference information out there is not very informative nor comprehensive and I'm struggling to understand how it works.
Say I have the timestamp 735071.64930556. Could anybody provide me with a formula to be able to derive the calendar date that this represents, or guide me through the process to convert it? From a programming standpoint, my purpose for wanting to understand this is so I can eventually write a converter to be able to handle this format within the application I'm developing. For now though, I simply wish to understand how the process works.
Thanks
Use datevec or datestr:
>> datevec(735071.64930556)
ans =
1.0e+003 *
2.0120 0.0070 0.0210 0.0150 0.0350 0.0000
>> datestr(735071.64930556)
ans =
21-Jul-2012 15:35:00
What makes your question a little bit difficult to answer is mostly the leap year / leap second problem. If you expect date values to exist only over a limited range of values, the problem is somewhat simpler (actually I am not sure how Matlab deals with the "lost days" when the world went from Julian to Gregorian calendars, for example - see http://en.wikipedia.org/wiki/Conversion_between_Julian_and_Gregorian_calendars for a hint of what I am talking about).
The hours-minutes-seconds part is relatively easy. When you have
datevalue = 735071.64930556;
You can compute:
rem = datevalue - floor(datevalue);
hours = floor(24*rem);
rem = 24 * rem - hours;
minutes = floor( 60 * rem );
rem = 60 * rem - minutes;
seconds = 60 * rem;
To get the year / month / day, your best bet is to use some built in function in the language of your choice (for many examples, see http://www.epochconverter.com/epoch/daynumbers.php ), or knock yourself out and create some lookup tables. If you have the date of the first of every month, the lookup would be fast; and from 0 CE until today, that would be only 2013 * 12 entries - not a very large table. Such a table could be created in Matlab and exported...

Generate a periodic value based on dates

I was hoping someone that is good with math and loops could help me out. I'm writing a program in Objective C where I need to come up with a way to do a cycle. If you don't know Objective C I would appreciate any help in pseudo code just to help me figure this out.
What I need is a scale that is based on two dates. I know this will be some sort of loop but not sure how to figure it out.
For instance, lets say that the first date is 5/25/1976 and the second date is 9/25/2009. Every 25 days there will be a "peak" so it's value will be 100. If I divide 23 in half I get 12 (rounded) so it would be the opposite or "valley" so it's numerical value would be 0. In other words on the 23rd day it would be at 100 but then on the 24th day it would start going back down and then bottom out 12 days later and then start the cycle back up and top out again at 23 days.
What I need to be able to do is find the numerical value for any given date in between any two given dates.
Thanks for any help you can offer!
value = 100*cos(2*pi*(numDays/25))
Or something like that.
Calculate the difference in days (optionally in fractional days too) between the starting point and the day you want the value for.
Divide by the cycle period (could be 23 or 25 according to the question).
Take the fractional part.
Apply the correct periodic function - for example, either sin() or cos(), appropriately scaled for the trigonometric functions (multiply the fraction by 2π).
You could simulate the shape by values out of a table describing the values indexed on days into the period (so you would use waveform[Δt mod period] to determine the value).
The NSDate class has a method timeIntervalSinceDate that will give you then number of seconds between two dates. You could calculate the number of days between two dates like this:
- (double) daysBetweenStart:(NSDate*)start end:(NSDate*)end
{
return [start timeIntervalSinceDate:end] / 86400.0; // seconds in a day
}
You could use this to compute a step function based on that:
- (double) someDescriptiveFunctionName:(NSDate*)date fromDate:(NSDate*)start
{
double days = [self daysBetweenStart:start end:date];
if ((int) days % 23 == 0)
return 100.0;
else
return 0.0;
}
This function returns 100.0 if the given date is between 23 and 24 days from the start, and 0.0 otherwise. You could substitute 23 for whatever period you like. I'm not sure if this is what you wanted, so clarify your question if it wasn't.
Disclaimer: This is Cocoa. Hopefully it's the same as iPhone Cocoa?