DAX parallelperiod until today - report

I'm trying to create a YTD DAX statement that gives me the parallel period from last year until a specific date.
Currently I have :
SumQty = SUM('Order Table'[Quantity])
YTD LY = CALCULATE([SumQty];PARALLELPERIOD('DIM Dates'[Date];-12;MONTH))
But this calculates me values for the whole last year instead of from January till August(now)
So in the end i need a Table which shows me the Qunatity of every month compared to the quantity last year in the same time period.
Any ideas?

In order to get the table you specified, you need to create 2 YTD measures:
Current YTD = CALCULATE ( [SumQty]; DATESYTD ( 'DIM Dates'[Date] ) )
PY YTD = CALCULATE ( [SumQty]; DATEADD ( DATESYTD ( 'DIM Dates'[Date] ); -1; YEAR ) )
Then use a matrix visualization and put measures [Current YTD] and [PY YTD] under Values section and 'DIM Dates'[Year] and 'DIM Dates'[Month] (I suppose you have them in your DIM Dates table) under Lines section.
That would give you the following table:
Please remember to have your 'DIM Dates' relationship to the 'Order Table' correctly configured in order to have your time intelligence calculations done right. You can find more info here.

Related

sqlite, inserting column data from one table to another

I am new to sql and I have the following problem,
In sqlite, I am interested to find how to add data from one table to another where data from column x should correspond to data in column Y.
For example lets say my table (Eruptions) consists of 4 columns:
Eruption Number (PK)
Volcano Number
Volcano name
start date
I have created a new table called Eruptions_since_2000 and interested in adding values from the original table to the new table.
The new table (Eruptions_since_2000) consist of 4 empty columns:
eruption_number(PK,unique,not null)
volcano_number
volcano_name
start_date
In my fist step I have added the data from Eruption number (from original table) to eruption_number in new table with the following code:
INSERT INTO Eruptions_since_2000 (Eruption_number) SELECT Eruption Number
from Eruptions where start year >= 2000
Now I want to add the next few columns as well (volcano number, volcano name and start date). But each entry need to of course correspond to the respective eruption number. This is where I am stuck, I am not sure how to do that.
It should be something similar to my code line above, but how does the respective/corresponding volcano number to the eruption number get added.
Thanks
The most efficient way to achieve what you want is:
(1) Delete all the rows of Eruptions_since_2000:
DELETE FROM Eruptions_since_2000;
(2) Insert all the rows with all the column values of Eruptions to Eruptions_since_2000:
INSERT INTO Eruptions_since_2000 (Eruption_number, Volcano_Number, Volcano_name, start_date)
SELECT Eruption_number, Volcano_Number, Volcano_name, start_date
FROM Eruptions WHERE start_year >= 2000
I guess you have managed to calculate start_year since you have already used it.
The alternative is this UPDATE statement with the use of Row values which are supported from version 3.15.0 of SQLite:
update Eruptions_since_2000
set
(Volcano_Number, Volcano_name, start_date) = (
select e.Volcano_Number, e.Volcano_name, e.start_date
from Eruptions e
where e.Eruption_number = Eruptions_since_2000.Eruption_number
)
If your SQLite version is lower that 3.15.0, then use this less efficient UPDATE statement:
update Eruptions_since_2000
set
Volcano_Number = (select e.Volcano_Number from Eruptions e, whereVolcano_name, e.Eruption_numberstart_date) = Eruptions_since_2000.Eruption_number),
Volcano_name = (select e.Volcano_name from Eruptions e whereVolcano_Number, e.Eruption_number = Eruptions_since_2000.Eruption_number)Volcano_name,
start_date = (select e.start_date from Eruptions e where e.Eruption_number = Eruptions_since_2000.Eruption_number)

How do you update an existing date to a random date in a range?

In one of my tables I have datetime field in which the data in the table column is populated with something like "2016-01-07 01:33:00".
What I want to do is change ONLY the date to a random date within a range (ie: 2016-02-01 thru 2016-02-28) without changing the time. The end result might be "2016-02-13 01:33:00".
What mysql command string would accomplish this task?
Something like
UPDATE someTable SET someDate = DATE_ADD(
someDate,
INTERVAL
DATEDIFF(rangeStart, someDate) +
ROUND(RAND()*DATEDIFF(rangeEnd, rangeStart))
DAY
);
where someTable.someDate is your existing data, and rangeStart and rangeEnd are the boundaries of your target date range.
Here you take the initial date, add enough days to it to reach the range start, and then further add a random number of days no greater than the number of days in your target range.
In MsSQL it could be:
select dateadd(day,cast((RAND() * 30) as int),getdate())
Substitute getdate() with your input date.
(RAND() * 30) is used to randomly generate a number of days up to 30.

Converting 10 minute interval data to hourly average

I have an excel file (.xlsx) that contains three columns, with headers:'datetime'(examples: 10/1/2008 0:10, 10/1/2008 0:20 etc), 'RH'(Example: 0.46) and 'wind_mps'(Example: 3.71). I wish to convert the 10 minute interval data into hourly average data for both RH and wind_mps columns.
I was unable to insert the Excel data into this question. Sorry about that. I can edit my question if someone tells me how to.
A similar question has been answered at how to convert by the minute data to hourly average data in R, but I'm new to R and unable to use the same technique for my data. I also tried to use the 'zoo', 'chron' and 'xts' packages to do this, as in http://rpubs.com/hrbrmstr/time-series-machinations but they seem to not work in R 3.02.
I tried to do this in Excel, but couldn't find a reasonably easy technique.
I was able to achieve a similar task of converting hourly data to daily average for another data set using an Excel macro, but I'm unable to do this for 10 minute data. The Macro is given below:
Global year As Integer
Sub Calculate()
Dim start_year As Integer
Dim end_year As Integer
Dim cell_count As Integer
start_year = Cells(19, "M").Value
end_year = Cells(48, "M").Value
year = start_year
cell_count = 19
Do While year < (end_year + 1)
Dim row As Integer
Dim sum As Double
Dim count As Integer
Dim init_row As Integer
init_row = 6
sum = 0
count = 0
Dim cv As Integer
cv = 3
Do Until cv = year
cv = Cells(init_row, "C").Value
init_row = init_row + 1
Loop
row = init_row - 1
Worksheets("Sheet1").Activate
Dim cv1 As Integer
cv1 = Cells(row, "C").Value
Do While cv1 = year
sum = sum + Cells(row, "F").Value
count = count + 1
row = row + 1
cv1 = Cells(row, "C").Value
Loop
Cells(cell_count, "N").Value = sum
Cells(cell_count, "O").Value = count
Cells(cell_count, "P").Value = sum / count
cell_count = cell_count + 1
year = year + 1
Loop
End Sub
It doesn't really matter to me whether I use R, Excel function, Macros or any other technique. It'd be great if someone can tell me how to convert this data set of about 50000 values for RH and wind_mps each to hourly average.
Thanks in advance.
This can be VERY easily done via Excel Pivot Tables...
Select your data and, in the top menu ribbon, go to Insert > Pivot Table
In the Pivot Table designer:
Select your datetime as your Row Labels
Select RH & wind_mps as your Values
For both your values, click on them and select Value Field Settings > Average
In the Pivot Table itself:
Now, go to your Pivot Table itself and right click on any of the dates in the first column
Select Group from the context menu that shows up and select Hours from the list that comes up
That should give you what you're looking to do with no programming at all
Hope this does what you wanted

Get RowCount With Date Comparasion in SSRS

I am new to SSRS.
I have a dataset, my dataset brings data from a stored procedure.
one of the parameters of my sp is StartDate and another one is EndDate. Their type is datetime
And the table has a dateTime Column called Date.
I have two gauges and I wanna bind integer values to my gauges.
First one is the count of rows where Date < DateAdd(DateInterval.Hour,24,StartDate)
and te second is count of rows where Date > DateAdd(DateInterval.Hour,24,StartDate)
How will I write the exact script. Whatever I wrote is not working.
I appreciate any help, thanks.
You need to set the gauge Pointer value as something like:
=Sum(IIf(DateDiff(DateInterval.Day, Parameters!StartDate.Value, Fields!Date.Value) >= 1
, 1
, 0))
This is counting rows where the time difference is less than a day compared to the parameter StartDate. Just change it slightly to get those where the difference is at least a day:
=Sum(IIf(DateDiff(DateInterval.Day, Parameters!StartDate.Value, Fields!Date.Value) >= 1
, 0
, 1))
Worked fine for me in a quick test:

Maintain a List of Daily Maxima Using a Trigger

I'm developing an application retrieving data from a small number of sensors in an interval of 10 seconds. Those values are stored in a single-table sqlite database.
One use case of this application is to print the daily maxima of each sensor which is read.
I don't like the idea of issueing a MAX...GROUP BY day like query every time I want to retrieve those values, so I thought of maintaining a second table containing thos daily maxima.
Is this the appropriate way of performing such a task?
I tried writing such a trigger, but it didn't work. I'm somehow missing the ability to do the comparison among new value, and value for the current hour already in the db (which might be no value, if a new hour just started)
CREATE TRIGGER update_maxima AFTER INSERT ON tick
BEGIN
INSERT OR REPLACE INTO maxima (time, device_id, lW)
VALUES (
strftime('%Y-%m-%d 00:00:00', 'now') ,
new.device_id,
(select case when new.lW > (select lW from maxima where device_id = new.device_id AND time = strftime('%Y-%m-%d 00:00:00', 'now'))
then
new.lW
else
(select lW from maxima where device_id = new.device_id AND time = strftime('%Y-%m-%d 00:00:00', 'now'))
end
)
);
END
You say you like the trigger better; it's hard to argue with that. ;-)
If you want a technical reason: the trigger is more efficient if you do the maximum-per-day query more often than some new value is inserted.
The select lW... subquery return a NULL value if there is no matching value in the table.
Any comparison with NULL will result in NULL, which makes the CASE return the ELSE value, which is NULL.
You have to wrap the subquery into an IFNULL call.
You can simplify your CASE into a MAX with two parameters:
... VALUES (
strftime('%Y-%m-%d 00:00:00', 'now'),
new.device_id,
max(new.lW,
ifnull((SELECT lW
FROM maxima
WHERE device_id = new.device_id
AND time = strftime('%Y-%m-%d 00:00:00', 'now')
),
0)
)
)

Resources