Report - Periodic - tsql

How to design a periodic report in SSRS. I need just logical steps. report looks like below.
Period 1 - 1/1/2014 - 1/15/2014
Origin Destination # of loads Average Billable Average Expenses Average Profit
a b 2 100 50 50
c d 3 300 50 250

-- First create a report with two parameter startdate and enddate and then create a Dataset with Query like (change logic according to requirement)... Map the parameter value at DataSet Property
SELECT Origin,Destination,loads,Average(Billable),Average(Expenses),Average(Profit)
FROM Table1
WHERE DateCol BETWEEN #startdate and #enddate
Group by Origin,Destination,loads

Related

How can I calculate the number of publications per month?

There is a table of posts on social networks with the date and title of the publication.
id
created_at
title
1
2022-01-17 08:50:58
Sberbank is the best bank
2
2022-01-17 18:36:41
Visa vs MasterCard
3
2022-01-17 16:16:17
Visa vs UnionPay
4
2022-01-17 18:01:00
Mastercard vs UnionPay
5
2022-01-16 16:44:36
Hadoop or Greenplum: pros and cons
6
2022-01-16 14:57:32
NFC: wireless payment
I need to calculate the number of publications per month, indicating the first date of the month and the percentage
of increase in the number of posts (publications) relative to the previous month. The data in the resulting table should be arranged in chronological order. The percentage of the increase in the number of messages can be negative, and the result should
be rounded to one decimal place with the addition of the % sign.
Table results
dt
count
prent_growth
2022-02-01
175
null
2022-03-01
338
93.1%
2022-04-01
345
2.1%
2022-05-01
295
-14.5%
2022-06-01
330
11.9%
I read documentation, but i don't understand how to do that..
step-by-step demo: db<>fiddle
SELECT
*,
(count * 100 / prev_count - 100)::text || '%' -- 4
FROM (
SELECT
*,
lag(count) OVER (ORDER BY pub_month) as prev_count -- 3
FROM (
SELECT
date_trunc('month', pub_date)::date as pub_month, -- 1
COUNT(*) -- 2
FROM mytable
GROUP BY 1
) s
) s
Normalize all dates to the first day of the month ("truncates" the day part if you like to see it that way)
Group all normalized dates and count all entrys per normalized date/month
Using lag() window function to shift the previous count result to the current row. Now you can directly compare the previous and current month count
Calculate the percentage. The result is a numeral type. So can cast it into text type to add the percentage character afterwards.

Date difference between pairs of dates per ID

I have data with 2 columns, in the following format:
ID
Date
1
1/1/2020
1
27/7/2020
1
15/3/2021
2
18/1/2020
3
1/1/2020
3
3/8/2020
3
18/9/2021
2
23/8/2020
2
30/2/2021
Now I would like to create a calculation field in Tableau to find per ID the difference between the different dates. For any value e.g. days.
For example for ID 1 the difference of the two dates according to calendar is 208 days. Next the difference of the second to third date for the same ID is 231 days.
A table calc like the following should do if you get the partitioning, addressing and ordering right — such as setting “compute using” to Date.
If first() < 0 then min([Date]) - lookup(min([Date]), -1) end

why my calculated cant be applied in my filters tableau

this is my tableau workbook
so i want to calculate day different between each transaction for each users, the users on this case are in filters PUL: True with this calculation
{Fixed [User Id]: sum(
if [Created At]<=[END_DATE] then 1 else 0 end)}>=2
AND
{FIXED [User Id]: sum(
IF [Created At]<=[END_DATE] AND
[Created At] >= [START_DATE] THEN 1 ELSE 0 END)}>=1
it means the users with more than 1 transaction before last range and atleast doing 1 transaction in date range are on the list.
so after that i made calculated field to count day different with this formula [CF]
DATEDIFF('day',LOOKUP(MIN([Created At]),-1), MIN([Created At]))
and also i made filters date range
lookup(min(([Created At])),0) >= [START_DATE] and
lookup(min(([Created At])),0) <= [END_DATE]
so not only count the time difference in date range for selected users, but each users also count day different on last transaction before date range (if any)
so this is the results (please take a look at user_id 86886)
i didnt understand why user_id 86886 as my experiments only have 1 transaction instead i already made the filters that only take user who doing transaction more than 1, after i check it, that user_id 86886 are doing more than 1 transaction in a day. this is the screenshoot
my questions is
why the tableau cant visualize all of the transaction in same day (but different hour) like this
and how to visualize it so it will appear 2 records with same day but different hour.
and also why if the transaction on the same day count time different with NULL instead it should be 0 because there's no time different
EXPECTED RESULTS (let's take a case in user_id 86886)
+---------+-----------------------+-------------+
| user_Id | dayOfCreatedAt | CF diff day |
+---------+-----------------------+-------------+
| 86886 | 1/25/2020 11:25:28 AM | |
| | 1/25/2020 11:39:42 AM | 0 |
+---------+-----------------------+-------------+
explanations : the first one of CF diff day become NULL because it's first transaction of this user, and the user is not doing transaction again before that, and the second was "0" because there's no day different, the different was only in hour, but although it was same day, so there will be not different day and it count 0
based on #Anil advice, this is my link of workbook tableau https://public.tableau.com/profile/fachry.dzaky#!/vizhome/simulation_data/Sheet14
I think your PUL field has some errors. Probably you should change the calculation of this field as
{Fixed [User Id]: sum(
if [Created At]<[START_DATE] then 1 else 0 end)}>=1
AND
{FIXED [User Id]: sum(
IF [Created At]<=[END_DATE] AND
[Created At] >= [START_DATE] THEN 1 ELSE 0 END)}>=1
Because, if you are trying to get non-null difference there must be at least one transaction before the [start_date]. Check it please.
Now please follow these steps (carefully please)
Step-1 Drag user id and created at at Rows Shelf.
Step-2 Change created at to exact date and thereafter to discreet (Both are important and you view shows that you haven't displayed it as exact date and as day of created_at instead)
Step-3: Set you date parameters
Step-4 Drag all three desired fields to filters shelf.
Step*5: Add PUL to context (again an important step)
Step-6 double click the CF field (check its calculation as
DATEDIFF('day', LOOKUP(MIN([Created At]),-1), MIN([Created At]))
Step-7 Change table calculation options of CF as table down (check this is also important)
Step-8 double click other CF_max/Min fields to add these to measure values
Step-9 change table calculation options in each of these four fields as discussed earlier (i.e. nested calculation of CF as specific dimensions restarting at every user_id). And nested calculation of CF_Max(as the case may be) to table down.
NOte Regarding you specific problem of user_id 86886 (if I will remove PUL revised condition as TRUE I am getting the same view as desired). Have a look please

Calculated Field to Count While Between Dates

I am creating a Tableau visualization for floor stock in our plant. We have a column for incoming date, quantity, and outgoing date. I am trying to create a visualization that sums the quantity but only while between the 2 columns.
So for example, if we have 9 parts in stock that arrived on 9/1 and is scheduled to ship out on 9/14, I would like this visualization to include these 9 parts in the sum only while it is in our stock between those 2 dates. Here is an example of some of the data I am working with.
4/20/2018 006 5/30/2018
4/20/2018 017 5/30/2018
4/20/2018 008 5/30/2018
6/29/2018 161 9/7/2018
Create a new calculation:
if [ArrivalDate]>="2018-09-01" and [ArrivalDate]<"2018-09-15"
and [Shipdate]<'2018-09-15"
then [MEASUREofStock] else 0 end
Here is a solution using UNIONs written before Tableau added support for Unions (so it required custom SQL)
Volume of an Incident Queue at a Point in Time
For several years now, Tableau has supported Union directly, so now it is possible to get the same effect without writing custom SQL, but the concept is the same.
The main thing to understand is that you need a data row per event (per arrival or per departure) and a single date column, not two. That will let you calculate the net change in quantity per day, and you can then use a running total if you want to see the absolute quantity at the close of each day
There is no simple way to display the total quantity between the two dates without changing the input table structure. If you want to show all dates and the "eligible" quantity in each day, you should
Create a calendar table that has all dates start from 1990-01-01 to 2029-12-31. (You can limit the dates to be displayed in dashboard later by applying date filter, but here you want to be safe and include all dates that may exist in your stock table) Here is how to create the date table quickly.
Left join the date table to stock table and calculate the eligible quantity in each day.
SELECT
a.date,
SUM(CASE WHEN b.quantity IS NULL THEN 0 ELSE b.quantity END) AS quantity
FROM date a
LEFT JOIN
stock b on a.date BETWEEN b.Incoming_Date AND b.Outgoing_Date
GROUP BY a.date
Import the output table to Tableau, and simply add dates and quantity to the chart.

Grouping by date difference/range

How would i write a statement that would make specific group by's looking at the monthly date range/difference. Example:
org_group | date | second_group_by
A 30.10.2013 1
A 29.11.2013 1
A 31.12.2013 1
A 30.01.2015 2
A 27.02.2015 2
A 31.03.2015 2
A 30.04.2015 2
as long es there isnt a monthly date_diff > 1 it should be in the same second_group_by. I hope its clear enough for you to understand, the column second_group_by should be generated by the user...it doesnt exists in the table.
date diff between which rows though?
If you just want to separate years (or months or weeks) use
GROUP BY DATEPART(....)
That's Sybase or SQL Server but other SQLs will have equivalent.
If you have specific data ranges, get them into a table with start and end date-time and a monotonically increasing integer, join to that with a BETWEEN and GROUP BY the integer.