Extract highest date per month from a list of dates - postgresql

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)

Related

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

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?

How to pull info from following month, for each month

I am having trouble with a task in Tableau. I need to pull in a value for each month, but the value originates in the following month (month + 1). For example, in January, I need to pull a beginning value from February, and then in February I need to pull a beginning value from March. The idea is to take the beginning value from the following month to calculate the difference from the ending value from the current month. The data I have is snapshot based, so there is one column which is the Date column, which can be 1/1/2020, or 2/1/2020 etc. (its done by month, not days).
My instinct is to create a calculated field and say something along the lines of 'where snapshot = snapshot (current) + 1 month, take in this value' but that does not work because they are not equal. I need to create a separate column or LOD for the future month I think? or a parameter?
ex.
{fixed [date], [ID]: SUM( [Date] = Dateadd('month',1,[Date]) Then [Begin Value] End)}
I found the answer. I needed to use the LOOKUP() function.

How do we calculate difference and percent difference between two columns in tableau

]Difference and percent Difference must be calculated.
I cannot do Apr20-MAy20 because it is not always the same. I need to show the current month and previous month
So I did a relative filter to just show the current month and previous month.
So the difference of two columns should automatically change when the month changes.
Now how do I get the same month of prior year, how do I filter ?
I also need to calculate the difference of current year same month and previous year same month.
Thank you in advance for any help!
When I do table across difference, the difference value is overwriting the existing May and Apr month values as the below screen shot, how to show the difference in another column
Currently:
Below is Expected:
Sounds like you should create a custom filter for the dates. You want:
This month this year
This month last year
Last month this year
There are a number of ways you could do this. I'll give one example and will assume there aren't any future dates in your data set.
[DateFilter]: DATETRUNC('month',[YourDateField])>=DATETRUNC('month',DATEADD('month',-1,TODAY())) OR DATETRUNC('month',[YourDateField])=DATETRUNC('month',DATEADD('year',-1,TODAY()))
Put the to the filters shelf, set to True, and it should keep the months you want.
Then you can just use the standard table calculations to calculate Difference and Percent Difference.
Note, the formula isn't tested, just typed directly into here, let me know if it doesn't work
Based on your comments look at creating separate calculations for to YoY / MoM / etc calculation. That also means creating calculated fields to isolate the Current Month, Previous Month, etc.
For example, the current month:
[isCM]: DATETRUNC('month',[YourDateField]) = DATETRUNC('month',TODAY())
The previous month:
[isPM]: DATETRUNC('month',[YourDateField]) = DATETRUNC('month',DATEADD('month',-1,TODAY()))
Then month on month, something like:
[MoM]: (SUM([Measure])*INT([isCM]))/(SUM([Measure])*INT([isPM]))
To make your table check this article about using the placeholder technique to create tables in Tableau

Difference between two values - same Dimension, same Measure

As you can see from the picture above I am trying to add new column and to calculate the difference between =2014-2017.
Is there any way to make this because Tableau's option "Table Calculation" doesn't play role for me.
Working out the difference between the first and last periods with table calculations:
First you need to get minimum year's values (i'm calling the field "Min Year Select"):
IF DATETRUNC('year',[Order Date]) =
{FIXED: MIN(DATETRUNC('year',[Order Date]))}
THEN 1 END
The above field named Min Year Select is saying that it should return a 1 if the year of the order date is the minimum year in your date range
Now we are flagging the smallest years, we can create a field to get the values (i'll call this "Min Year Segment"):
IF SUM([Min Year Select]) >= 1 THEN [Sales] END
Here we're saying that if the year is flagged as the smallest (as classified by the previous calc field we made), then get the value
But before we can compare the two values, you have to work out the number of time periods between the min and current year so that the difference calculation lookup field is comparing the right values (i'll call this "Number Years in Range"):
{FIXED [Segment]: COUNTD(DATETRUNC('year',[Order Date]))}
What we're doing is fixing the query at the category level (segment), think of this as removing the date pill from your report, then performing a calculation. Here it's COUNT DISTINCT years. So if a segment has data for 2011,2012,2013; then the query returns 3
We can now get the difference between your latest and your minimum Segments (called: "Difference from First Last Segment"):
[Segment] -
LOOKUP([Min Year Segment],
-1*(Number Years in Range)-1)
Firstly we get the first year's sales for each segment (Min Year Segment will be null for all years that aren't the first, so we need to lookup the first by going backwards by the number of years in our range:
We do -1 * because we want the lookup to lookup backwards, then we add in ("Number Years in Range" - 1) because we want to lookup to the period that had the earliest data. We do minus one so we're excluding the current year/latest year in your dataset
This is a lot to digest, I think it's easier to present as a picture too:
Here we calculate the difference between the first and last month, with the value in the last month
If this helped or you have any more questions, please vote on my answer/let me know

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