We have the fact table with multiple dimensions date, product, customer and measures like revenue, sales...
We would like to calculate the difference between the revenue, sales number between the last week vs this week ( between two dates).
Kindly help to create a sql query that can help to achieve .
Thanks.
to_number(to_char(DateField,'WW'))
will give you a week number within year. More...
Related
I have a dataset with the structure below.
I want to calculate a monthly average of the views.
I attempted to calculate the yearly frequency with the following code and I believe it is correct
SELECT
EXTRACT (YEAR FROM TO_DATE("date",'Month YYYY') ) AS "year",
AVG("views")
FROM talks
GROUP BY EXTRACT (YEAR FROM TO_DATE("date",'Month YYYY') )
ORDER BY "year" DESC
When it comes to the monthly analysis I have the problem that there several records for the same month in a year and there several years with the same months (in reality the dataset has information for many years - this a reduced version).
How can I go to implement this?
If you want the average per month then just group by your current date field.
If you want the average per month regardless of year then you would have to extract the month part of the current date field and group by that.
But your date field now appears to be having string data type; it would be better to use proper date data type. Then your analysis would be much easier, more flexible, better performing.
I am using standard SQL and am trying to add the weekly sum for product usage by week.
Using code below, I was able to add to each row the respective week and year it falls into. How would I go about summing the totals for an item by week and outputting it in columns, say up to the last 8 weeks.
extract(week from Metrics_Date) as week, EXTRACT(YEAR FROM Metrics_Date) AS year
Image is my raw data with the week and year next to an item:
This image is of above raw data being analyzed further(grouping them together). Here is where I would want to add columns, current_week & firstday of week date, and a sum of that weeks totals.
Any help would be appreciated.
You don't need the extract() by the way, you can do truncation DATE_TRUNC(your_date, WEEK) and it will truncate it to the week, usually easier.
Also, because the result of the truncation is a date, you will have the first day of the week already.
The rest I believe you have it figured out already, but just in case:
SELECT DATE_TRUNC(your_date_field, WEEK) AS week, SUM(message_count) AS total_messages FROM your_table GROUP BY 1
I have a table with the sales from last 2 years, and I want to compare the sales from this year with the same natural day last year. For example, Sunday 1st of April 2018 will be compared with Sunday 2nd April 2017.
In order to do that I have created the measure
sales_last_year = CALCULATE(Sales[Revenue]); SAMEPERIODLASTYEAR(DATEADD('Calendar'[Date];+1;DAY)))
And I have created another measure where I have the value from the same day last year:
Prueba_sales_last_year = CALCULATE(Sales[Revenue]); SAMEPERIODLASTYEAR('Calendar'[Date]))
The result is the following:
Sales last year
As you can see the sales per day shows 5.316€ and 3.546€, which is correct, but the total is 111.796 €, which is not correct. However, the measure with the formula without the natural day the sum of the two rows is correct. How could I solve this?
Thank you very much in advance
I just changed the order to calculate the date and it was solved.
sales_last_year = CALCULATE(Sales[Revenue]);DATEADD( SAMEPERIODLASTYEAR('Calendar'[Date]);+1;DAY))
I have a Date dimension that I would like to check current month and current year, and then display a different Measure depending on that evaluation.
example:
MM - 04
YY -2016
Calculated member determines current month then for months prior(1-3) it shows measure Qty Shipped. For current month and beyond(4-12) it shows measure Forecast. This logic is only working with current year.
Thanks.
IIF(([02 Dates].[MM].currentmember,[Measures].[Qty Shipped])<
(
STRTOMEMBER('[02 Dates].[MM].&['+format(now(),'yyyy')+']&['+format(now(),'MM')+']')
,[Measures].[Qty Shipped]),
[Measures].[Qty Shipped],
[Measures].[Current Forecast])
Seems to do the Current Month on correctly, but doesn't move qty shipped to months 1-3.
Thanks
Does this logic look correct.
Any help much appreciated.
I have a situation where the metric calculation(Month to Date Sales) is based on the month from the report date filter.
We want the month to date filter to be for the report month and not the current month - in other words, if the user selects 03/10/2015 for the report date, we want the month sales to be the sales for March,
and not the sales for June.
Is there a way to parse the report date to extract the report month into a variable that can be used in the ‘where' clause of the metrics in order to generate the desired results.
Thanks in advance.
I am not entirely sure if this answers your question, but achieving something like this may be possible when using disconnected timeline date filter. Your metric should look like this:
SELECT Sales_metric BY Month/Year (Date) ALL OTHER WHERE Month/Year (Date) = (SELECT MAX(Month/Year (Timeline)) BY ALL OTHER EXCEPT Date (Timeline))
I hope this helps, in case if it doesn't feel free to log a ticket to GoodData Support at support#gooddata.com.