Dynamically compare sum of last n entries to sum of n entries depending of selected timespan - postgresql

I have booking information containing booking values. Every booking has a createdAt date. What I have been doing so far, is to extract the month of the createdAt date, sum up the bookingValue and then compare it to the sum of the previous month using lag function. I used string input to select the corresponding month e.g. 2022-04. My code will execute and show the sum of bookingValue of the month April 2022 and also compare it to March 2022 using lag function.
What I´d like to do is to dynamically select a date range using a datepicker. Like this I can select a date range e.g. from 10th of May 2022 to 20th of May and get the sum of bookingValue. Now the only problem I have, is that my lag function won´t work anymore. What can I do?

Related

Tableau Fixed Max date registered by month

I want to create a graph line in tableau with a count distinct id per month.
I use an excel spreadsheet that is updated a few times per month but never on the same date, so I have different dates registered as months go by.
I want to use the last date registered per month so I can use that date to show month's trend through the years. Each id represent a different construction building, so it is expected that the same id can be found on different months.
This is what I tried: Tableau Fixed (LOD) formula the get last date registered per each month:
{ FIXED [id_constructionbuilding], MONTH([date_registered]): MAX([date_registered])}
Then I use rows and columns in tableau but I get more than one max date (my excel has 2020 and 2021 dates, so it's picking dates from October 2020 and October 2021 for example). Dates are order according to Spanish so you will see month and days shifted:
I would suggest updating your calculated field to:
{ FIXED [id_constructionbuilding],DATETRUNC('month', [date_registered]), DATETRUNC('year', [date_registered]):MAX([date_registered])}
This will keep the level of detail (LOD) to the id_constru..., month and year.

Extract highest date per month from a list of dates

I have a date column which I am trying to query to return only the largest date per month.
What I currently have, albeit very simple, returns 99% of what I am looking for. For example, If I list the column in ascending order the first entry is 2016-10-17 and ranges up to 2017-10-06.
A point to note is that the last day of every month may not be present in the data, so I'm really just looking to pull back whatever is the "largest" date present for any existing month.
The query I'm running at the moment looks like
SELECT MAX(date_col)
FROM schema_name.table_name
WHERE <condition1>
AND <condition2>
GROUP BY EXTRACT (MONTH FROM date_col)
ORDER BY max;
This does actually return most of what I'm looking for - what I'm actually getting back is
"2016-11-30"
"2016-12-30"
"2017-01-31"
"2017-02-28"
"2017-03-31"
"2017-04-28"
"2017-05-31"
"2017-06-30"
"2017-07-31"
"2017-08-31"
"2017-09-29"
"2017-10-06"
which are indeed the maximal values present for every month in the column. However, the result set doesn't seem to include the maximum date value from October 2016 (The first months worth of data in the column). There are multiple values in the column for that month, ranging up to 2016-10-31.
If anyone could point out why the max value for this month isn't being returned, I'd much appreciate it.
You are grouping by month (1 to 12) rather than by month and year. Since 2017-10-06 is greater than any day in October 2016, that's what you get for the "October" group.
You should
GROUP BY date_trunc('month', date_col)

How to return the last day of each month

I am creating a view where i have multiple records showing up for each month.
Example: January is showing 20 records and February is showing 30 records. I only want the last record of EACH MONTH to show up. So i want to see 1 record for each month.
I have already tried the first() and last() table calculation, but it does not filter by month, but by column.
If you just apply first and last on the dataaset then you won't get the correct output, If you need for every month then you need to divide the partition to year and month and then apply the max on the specific partition.
Try this way:
Place the order date in Exact date format and change the property to discrete
Now extract the year and month in separate calculated fields and place in detail.
Year:
year(Date)
Month:
Month(date)
Now create one more calculated field and write below code:
WINDOW_MAX(MAX([Order Date]),FIRST(),LAST())
Try This
1 Calculated Field - DateMonth
Datetrunc('month',Date)
2 Calculative field - LastDate
{ FIXED DateMonth: max(Date)}
3 Calculative Field- Filter
if lastDate=Date then 'Yes' else 'No' end

Filter SSRS subreport for 12 months from first day of month to last day of month

I have a SSRS report that has typical Start Date / End Date parameters that will filter out Sales Order information based on the OrderDate field. With this report, I have a subreport. This subrebort is basically nothing more than an aggregation of the same info, by month. This aggregate information should be from the first day of the month, one year prior, through the last day of the End Date's parameters month. This does not take the main subreports Start Date parameter into consideration at all.
This is where it gets a little tricky. For example, lets say I made the start date / end date parameters on my main report:
10/15/2016 - 11/15/2016
(just remember, for this purpose, the start date is irrelevant)
I would want the subreport to show the sales totals, per month, for the ENTIRE month of December 2015 through the ENTIRE month of November 2016, even though my end date was 11/15/2016.
If I were to put in those same dates for my parameters in the report right now, for the aggregation of December 2015, I would only get sales from the 15th through the 31st. Currently I have my subreport to filter on the OrderDate by:
Fields!OrderDate.Value>= DateAdd(DateInterval.Month, -12,Parameters!EndDate.Value)
I know that this filter is not currently set up to have an end date for the parameter, just a greater than argument, which is wrong since I want the 12 month history to stop at the last day of the month for the month of my End Date parameter, but I don't know how to make that happen either.
I hope I have explained this well. Any help on this would be greatly appreciated.
You can set a filter to get only dates between a specific range in your subreport dataset, tablix and some visualizations:
In DataSet properties or Tablix Properties in the Filter tab use these settings:
In the Value textboxes you should use the expressions to calculate your date range.
StartDate
=DateSerial(Parameters!EndDate.Value.Year-1,Parameters!EndDate.Value.Month,1).AddMonths(1)
EndDate
=DateSerial(
Parameters!EndDate.Value.Year,Parameters!EndDate.Value.Month,1).AddMonths(1).AddDays(-1)
It should filter your data from 12/01/2015 to 11/30/2016 if your EndDate parameter is set to 11/15/2016
Let me know if this helps.

How to populate all cells with a value taken from last day of the month?

I'm new to Spotfire and I need to populate all cells in a column with a value taken from the last day of the month. Example as below.
Original data:
Required output is that there will be another column that takes 32212 for the month of November ie this column to take the volume that corresponds to the last date 11/20/2009 and for the month of December it will take 12231.
Here is what I did:
Created a calculated column to find the last entry of the month
Max([Date]) OVER UID
and UID is an unique id for each month integer(Year([Date])*100 + Month([date]))
Another calculated column to find the value for the last entry in each month
if(([Date]) = ([Max Date per Month]),[Volume],0)
Now this is the problem. I tried to use
Sum(if(([Date])=([Max Date per Month]),[Volume],0)) OVER ([Max Date per Month])
but Sum doesn't work.
Any idea?
What do you mean by "Sum doesn't work"?
Here is what I did:
Created two calculated columns as
Month([Date])
Year([Date])
Created Last day in month column which simply gives me latest date in each month
Last([Date]) OVER ([Year],[Month])
Created column which calculates what you need
Sum(if([Last Day in Month]=[Date],[Volume],0)) OVER ([Year],[Month])