Show year to date sales for previous years - excel

I am making a report that counts the amounts of offers, uniquely identified with SALE_ID, containing data from different products starting first of January 2015 ranging up to todays date (18/12/2017 at the time of asking). I am counting the amounts of offers with a simple measure called 'Distinct':
Distinct := DISTINCTCOUNT(dOffers[Sale_ID])
This gives me satisfactory results, as in, I am receiving the right counts for the considered period. I am also calculating year-over-year changes, defining the previous year offers with the following measure: (dCalendar contains the datekey table).
PY Offers :=
SUMX (
VALUES ( dCalender[YearMonthNumber] );
IF (
CALCULATE ( COUNTROWS ( VALUES ( dCalender[FullDates] ) ) )
= CALCULATE ( VALUES ( dCalender[MonthDays] ) );
CALCULATE (
[Distinct];
ALL ( dCalender );
FILTER (
ALL ( dCalender[YearMonthNumber] );
dCalender[YearMonthNumber]
= EARLIER ( dCalender[YearMonthNumber] ) - 12
)
);
CALCULATE (
[Distinct];
ALL ( dCalender );
CALCULATETABLE ( VALUES ( dCalender[MonthDayNumber] ) );
FILTER (
ALL ( dCalender[YearMonthNumber] );
dCalender[YearMonthNumber]
= EARLIER ( dCalender[YearMonthNumber] ) - 12
)
)
)
)
The problem that I am having, is that the year-over-year change for the month december (the running month), considers the year-to-date sales for this year (2017) and compares this to full month sales in the previous years (2016 and 2015); this makes the last months comparison uninterpretable, as we are comparing offers from half a month to offers from a full month.
I would like to know how to fix this problem: i.e. consider the sales for the full year up to todays date, and compare this for the exact same periods last year and two years ago (2015: start Jan 1st and go up to Dec 18th; idem dito for 2016 and 2017). The SAMEPERIODLASTYEAR call might seem straightforward forthis issue, but I am receiving a contiguous dates errors...
Thanks in advance!

From your description, I understand that you're trying to perform year over year month to date comparisons.
Without relying on any time intelligence functions like SAMEPERIODLASTYEAR, you may want to try this version of the measure:
PY Offers :=
SUMX(
VALUES( dCalendar[YearMonthNumber] );
CALCULATE(
VAR currDate = MAX( dCalendar[FullDates] )
VAR currYear = YEAR(currDate)
VAR currMonth = MONTH(currDate)
VAR currDay = DAY(currDate)
RETURN
CALCULATE(
[Distinct];
ALL( dCalendar );
YEAR(dCalendar[FullDates]) = currYear - 1;
MONTH(dCalendar[FullDates]) = currMonth;
DAY(dCalendar[FullDates]) <= currDay
)
)
)

Related

Before previous month measure in (DAX) Power BI

I am struggling with DAX measure to get 2nd month from today, or in business terms, before previous month (so lets say May from now).
I am using this DAX:
Prev-2Months =
CALCULATE (
CALCULATE (
[Comp_Spend],
MONTH ( CCC[Date] )
= IF (
MONTH ( MAX ( CCC[Date] ) ) <= 2,
10 + MONTH ( MAX ( CCC[Date] ) ), // similar DAX is for Month
before (so June) with a
little tweak
MONTH ( MAX ( CCC[Date] ) ) - 2
),
YEAR ( CCC[Date] )
= IF (
MONTH ( MAX ( CCC[Date] ) ) <= 2,
YEAR ( MAX ( CCC[Date] ) - 1 ),
YEAR ( MAX ( CCC[Date] ) )
),
ALL ( V_Dim_Dates ),
KEEPFILTERS ( CCC[ClinicID] )
)
)
When it comes to February 2022 with slicer I am getting Blank values, assuming that Fiscal Year ends on 09/30. How can I solve this to not getting blanks for this "year transition" case?
Hmmm I guess I solved it using DATEADD, where my huge formula gives the same result.. :/
Prev-1M = CALCULATE ([Compliance_Spend],DATEADD(Commercial[Date],-1,MONTH),REMOVEFILTERS(V_Dim_Dates_Extended))
So I can easily adjust if I wanna 1 or 2 or 3 months to have as previous..
It is good to know that there are XYZ options in DAX how you can obtain the same thing.
Credits for suggesting: #Anonymous

DAX sum of multiplication (sumproduct) across multiple tables

I am struggling with what I think is a really silly problem.
I am trying to get "MTD target" values from an arbitrary date selection. I have a table 'out' which has many dimensions +date, as well as a target table which gives me a constant target value per day, for each month. My goal is to get the number of days selected per EACH month and multiply it by the corresponding daily target for the relevant month.
For example, month 1 daily target is 2, month 2 daily target is 4. I have 4 days in month 1, and 3 days in month 2. My cumulative target should be 2*4+3*2 = 14.
I can do this for a single month no problem. But as soon as I have a date range selected that crosses 2 or more months it all goes to hell.
Table 'out' has date, country, and other dimensions.
Table 'targets' has a year, month, and country dimensions.
I am trying to get some join and multiplication that is something like SUM(month_country * selected_days)
Here are the DAX measures I've tried:
mtd_inv_tgt :=
CALCULATE (
SUM ( targets[daily_spend] ),
FILTER (
targets,
targets[market] = FIRSTNONBLANK ( out[co_market], "" )
&& targets[yyyymm] >= MIN ( out[yyyymm] )
&& targets[yyyymm] <= MAX ( out[yyyymm] )
)
)
* DISTINCTCOUNT ( out[date] )
mtd_inv_tgt :=
SUMX (
FILTER (
targets,
targets[market] = FIRSTNONBLANK ( out[co_market], "" )
&& targets[yyyymm] >= MIN ( out[yyyymm] )
&& targets[yyyymm] <= MAX ( out[yyyymm] )
),
targets[daily_spend] * DISTINCTCOUNT ( out[date] )
)
This works fine if the dates selected belong to one month. If I select 2 months it will add the daily spend across 2 months and then multiply it by the number of dates covering the 2 months. Like from the above example it would be (2+3)*(4+2) = 30, which is obviously wrong.
The caveat is I can't use SUMX on the 'out' table because there are many records per date+country, whilst the targets are a single entry per month+country.
I think something similar to this should work:
mtd_inv_tgt :=
SUMX (
VALUES ( out[date] ),
LOOKUPVALUE (
targets[daily_spend],
targets[yyyymm], SELECTEDVALUE ( out[yyymm] )
)
)
This iterates over each distinct out[date] value in the current filter context and adds up the daily_spend for each of those dates that it looks up by matching on yyymm.
Iterating over only the yyymm might be more efficient:
mtd_inv_tgt :=
SUMX (
VALUES ( out[yyymm] ),
DISTINCTCOUNT ( out[date] )
* LOOKUPVALUE (
targets[daily_spend],
targets[yyyymm], EARLIER ( out[yyymm] )
)
)
Note: If these don't work as expected, please provide sample data to check against as I haven't actually tested these.

Power bi How can i display items without data with a zero AND hide the months in the future

I'm using the following measure in order to make the charts display the periods that don't have a value. But the problem now is that it is displaying the months in the future.
x Count Tickets =
IF
(
ISBLANK
(
DISTINCTCOUNT('Tickets DB'[Número de Ticket])
)
,0,
DISTINCTCOUNT('Tickets DB'[Número de Ticket])
)
How can I avoid the months in the future from displaying?
I also tried the following measure:
x Count Tickets =
IF
(
DATEDIFF(MAX('Tickets DB'[Fecha]),TODAY(),DAY)>0,
IF
(
ISBLANK
(
DISTINCTCOUNT('Tickets DB'[Número de Ticket])
)
,0,
DISTINCTCOUNT('Tickets DB'[Número de Ticket])
)
)
But it is not displaying months without data
Thanks in advance.
1. First create a calculated column(calendar dimension table), to return if there is a record on all dates of this dimension
example of calculated column
2.
Create the measurement below (
Where you will change the context of the measurement to bring only the true fields from the column you created earlier)
Medida Stack =
CALCULATE(
DISTINCTCOUNT(
fVendas[CLIENTE/LOJA]);
CALCULATETABLE(
dCalendario;
dCalendario[Has Sales?]=TRUE()
)
)
fvendas[Cliente/loja] = 'Tickets DB'[Número de Ticket]
dCalendario = calendar dimension table
If it is always the dates in the future you want to hide, you could opt for relative date filtering on your visual. More info here
IF (
MAX ( 'DimDate'[Date] ) > TODAY (),
BLANK (),
DISTINCTCOUNT ( 'Tickets DB'[Número de Ticket] ) + 0
)
Starting from your original measure, we check whether the date in context is after today's date. In that case, we blank the measure. Otherwise we do your DISTINCTCOUNT, which may possibly return a blank value. Note that DAX blanks are not equivalent to SQL NULLs - specifically addition with a blank returns a number, unless both addends are blank; so BLANK () + 0 = 0. This neatly solves your display for months with no data. But we only execute this logic for past months.
Note, I've written this assuming that you have a date table. If you don't, you should get one. I like mine, but there are plenty of good ones out there.
Create a calendar table using something like the following:
Calendar =
VAR MinDate = MIN('Tickets DB'[Fecha])
VAR MaxDate = MAX('Tickets DB'[Fecha])
VAR BaseCalendar =
CALENDAR ( MinDate, MaxDate )
RETURN
GENERATE (
BaseCalendar,
VAR Basedate = [Date]
VAR YearDate =
YEAR ( Basedate )
VAR MonthNumber =
MONTH ( Basedate )
VAR YrMonth = 100 * YearDate + MonthNumber
VAR Qtr =
CONCATENATE ( "Q", CEILING ( MONTH ( Basedate ) / 3, 1 ) )
VAR QtrYr = YearDate & " " & Qtr
RETURN
ROW (
"Day", Basedate,
"Year", YearDate,
"Month Number", MonthNumber,
"Month", FORMAT ( Basedate, "mmmm" ),
"Year Month", FORMAT ( Basedate, "mmm yy" ),
"YrMonth", YrMonth,
"Qtr", Qtr,
"QtrYr", QtrYr
)
)
If you have dates in the future in your table, replace the MAX('Tickets DB'[Fecha]) part to NOW() then just create a relationship from your 'Tickets DB' table to this calendar table on both date fields and that should correct the issue (and allow you to use time intelligence functions). Once this is set up correctly your measure should work just fine.
Hope it helps.

DAX Moving Average Reconciliation

Following tutorials I have a dax formula that calculates a 12 month moving average. However it seems slightly off from what users would be expecting.
12 Month Moving Average:=AVERAGEX (
FILTER (
ALL (dim_Calendar ),
dim_Calendar[Month_from_date_iso] > ( NEXTDAY ( SAMEPERIODLASTYEAR ( LASTDATE (dim_Calendar[Date_iso] ) ) ) ) && /* t1 */
dim_Calendar[Month_from_date_iso] <= MAX (dim_Calendar[Date_iso] ) /* t2 */
),[Total Month Sales])
Edit: adding Total Month Sales
Total Month Sales:=CALCULATE (
SUM([Sales Gross]),
FILTER (
ALL ( dim_Calendar),
dim_Calendar[Month_from_date_iso] = MAX (dim_Calendar[Month_from_date_iso] )
)
)
I would have expected Column F to match Column E.
Am I misunderstanding how AVERAGEX works or is this a rookie error?
Edit2: progress update. Demonstrating how the proposed solution calculates the overall average, not last 12 months.

Rolling 12 Month sum in PowerPivot

In PowerPivot Excel 2016 I write a formula for rolling 12 month sum of sales as below :
Rolling Sum:=CALCULATE (
[Sales] ,
DATESBETWEEN (
Sales[Date],
FIRSTDATE(DATEADD(Sales[Date],-365,DAY)),
LASTDATE (Sales[Date] )
)
)
But it seems not working correctly. for each month it shows me only sales of that month!
Does anybody knows how should I fix my problem?!
Thanks In Advance
If you don't have a Date/Calendar table you can't use Time Intelligence functions properly.
Despite the best practice would be have a Calendar/Date table and use Time Intelligence functions, you can get the desired result by using an explicit filter:
Rolling Sum :=
CALCULATE (
[Sales],
FILTER (
ALL ( Sales ),
[Date]
>= MAX ( Sales[Date] ) - 365
&& [Date] <= MAX ( Sales[Date] )
)
)
Let me know if this helps.
Using fixed 365 days does not properly account for leap days. In the following code, "[CurDatePerCalendar]" measure contains the date for which you want to calculate the average.
Sales TTM =
VAR base = [CurDatePerCalendar]
VAR StartDate = EDATE(base,-12) + 1
VAR EndDate = base
RETURN
CALCULATE(
[SalesSum],
FILTER ( _Calendar,
_Calendar[Date] >= StartDate
&&
_Calendar[Date] <= EndDate )
)

Resources