How to create MTD, QTD and YTD Sum of metric - amazon-web-services

I have table with data by Region, Country, Date, month, quarter, year and sales. I am trying to design a AWS Redshift query where I could have columns for MTD (full month of sales), QTD (quarterly aggregation based on months) and YTD (yearly aggregation of months) by Region, Country, Year , Quarter and Month. Right now I am struggling with how to do running sum by quarter and by year for previous months.
Sample data:
Output:

For each year/quarter are you trying to show what the xTD values were up until that period? Seems like a strange way to present the data, usually xTD figure are shown 'as of now' and not historically as that can be very confusing.
If you are looking for regular xTD figures then use this:
select country,
sum(case when date >= date_trunc('month', CURRENT_DATE) then sales else 0 end) as mtd,
sum(case when date >= date_trunc('quarter', CURRENT_DATE) then sales else 0 end) as qtd,
sum(case when date >= date_trunc('year', CURRENT_DATE) then sales else 0 end) as ytd
from data
group by country
Interactive example here.

Related

DAX Rolling Average Applying at Year Level, But Not Month Level

I am trying to get a rolling 3/6/12 month average of revenue by month using DAX in a SSAS Tabular model. There is a date table in my model and I have created inactive links between my fact table and my date table for each date in the fact (Date of Service, Invoice Date, etc.). I have created a measure that uses USERELATIONSHIP to activate the specific link I need.
Revenue by DOS:=
CALCULATE( [Total Gross Revenue],
USERELATIONSHIP(D_DATE[W_DT_ID],Fact_Charge[W_SERVICE_DT_ID])
)
Here is the code I am using to create the rolling average, which I copied from a SQLBI video on the same subject:
Net Revenue R3M:=
VAR NumOfMonths = 3
VAR LastSelectedDate = MAX(D_DATE[Calendar_Date])
VAR Period =
DATESINPERIOD( D_DATE[Calendar_Date], LastSelectedDate, -NumOfMonths, MONTH)
VAR Result =
CALCULATE(
AVERAGEX(
VALUES(D_DATE[Month Year]),
[Revenue by DOS]
),
Period
)
Return
Result
For some reason when I deploy the model and try to use it in Power BI, it runs this rolling average at the year level, but not the month. For instance, the Power BI matrix visual will show the year 2021 and will have a value that is equal to the average of the last 3 months of 2021, but the individual months show the same value that is contained in the Revenue by DOS measure itself.
Snip From Power BI
The goal is to have each individual month in the Revenue R3M column to be the 3 month average of the Revenue by DOS column.

GCP Bigquery WORKDAY function

How do I go about calculating number of workdays in a MONTH based on a date in another column?
Example:
Column 1 - 2020-06-30
Column 2 (Calculated) - 22 (i.e number of workdays in the month of June Mon to Friday)
Does BQ have a WORKDAY function?
You can use below approach
create temp function workdays(input date) as ((
select count(*)
from unnest(generate_date_array(date_trunc(input, month), last_day(input, month ))) day
where not extract(dayofweek from day) in (1, 7)
));
select column1,
workdays(column1) as column2
from your_table
if applied to sample data in your question - output is

Calculate cumulative sum from beginning of year to filter selection

I have two tables
Calendar - Contains columns for year, quarter, YearQuarter
Sales - contains columns for Entity sold, sale price, date of sale, quarter of sale, year of sale.
I have a filter in my dashboard for Year and Quarter. I would like to create a measure that calculates the cumulative sum of sales from the beginning of the selected year to the selected quarter.
e.g. if Year = 2020, and Quarter = Q3, the measure should calculate the sum of sales from the beginning of 2020 until the end of Q3 2020.
If you had a well-formed date table, you could use time-intelligence functions DATESYTD or TOTALYTD as explained in The hidden secrets of TOTALYTD.
However, your calendar table doesn't qualify, so you need to do things a bit more manually.
Sales YTD =
VAR SelQtr = SELECTEDVALUE ( Calendar[Quarter] )
RETURN
CALCULATE (
[Sales],
ALLEXCEPT ( Calendar, Calendar[Year] ),
Calendar[Quarter] <= SelQtr
)
This removes any calendar filtering except for the year and filters for quarters up to the selected quarter.

How to include QTD and YTD in Redshift query

I am working on SQL script in Redshift. As per the requirements, I have to show the month but the amounts need to be calculated QTD and YTD. How to ignore month dimension from the calculation and how to have YTD calculation for each month?
Example:
Month February: I need to show sum of amount for Jan and Feb as QTD and YTD
Month June: I need to show sum of amount April, May and June in QTD and Jan-June in YTD
ETC.
In selection, I need to show month, year with other dimension.

Compare totals for the same partial date range year-over-year in DAX / Power BI

I'm trying to create a table which shows a sum of monthly values for one year compared to the last year's totals (structured as the screenshot below):
Monthly Comparison
However, the caveat I'm dealing with is comparing the most current month, which will always contain partial month data (unless it's the last day of the month), to the same date range of the previous year. In the screenshot I attached, our data for January 2018 only goes through January 22nd. However, it's comparing it to the full month of January from 2017, whereas we want that total to be January 1st - 22nd, 2017: Value That Needs to be Updated.
I've tried messing around with various MTD and cumulative totals, but I can't seem to get the logic to work while keeping the aggregation to the monthly level. Any idea what type of logic needs to used in order to compare year-over-year totals, but only do a partial sum for the same date range of a month that is currently in progress?
Thanks in advance.
In my short example, this seems to work:
Total Sales LY 2 =
VAR MaxDate = EDATE(CALCULATE(MAX(Sales[Date]);ALL(Sales));-12)
RETURN
CALCULATE(
[Total Sales];
FILTER(SAMEPERIODLASTYEAR('Date'[Date]);'Date'[Date]<=MaxDate)
)
I calculate Total Sales for the same period last year, with the max of the last available sales date this year.
Total Sales LY = Comparing last year full month (wrong)
Total Sales LY 2 = Comparing last year month, with max of last sales date
PBIX file