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 )
)
Related
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.
I am pretty new in wokrin with data models within Excel, especially in Dax formulas.
I have the following two tables in the data model.
Table_HS:
Product Code - User
Table_IS:
Product Code - Work Order - Date Created - Date Started
Now I want to get an Pivot table with the Product Code (Tabel_HS) and I want to know, how many Work Orders were created (Date Created) this week, and how many were started this week.
How would you manage that?
Sure, I can add the Tabele_IS with =WEEKNUM the week number. But how can I filter the pivot automatically to the current week or the current and the last week?
I don't want to filter every week the specific week number.
Best Regards
Joshua
Create two measures:
Work Orders Created This Week:=
VAR WeekCommencing = TODAY() - WEEKDAY(TODAY(),3)
VAR WeekEnding = WeekCommencing + 6
RETURN
CALCULATE (
DISTINCTCOUNT ( Table_IS[Work Order] ),
Table_IS[Date Created] >= WeekCommencing && Table_IS[Date Created] <= WeekEnding
)
and
Work Orders Started This Week:=
VAR WeekCommencing = TODAY() - WEEKDAY(TODAY(),3)
VAR WeekEnding = WeekCommencing + 6
RETURN
CALCULATE (
DISTINCTCOUNT ( Table_IS[Work Order] ),
Table_IS[Date Started] >= WeekCommencing && Table_IS[Date Started] <= WeekEnding
)
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.
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
)
)
)
I have a "sales" table with team ID, payment type and date (see example below):
I want to write a DAX formula that will "lookup" the prior month's sales amount for a "Monthly" team and the 3 month prior sales amount for a "Quarterly" team. The lookup also needs to be filtered by team ID, so the prior sales belong to the same team.
Here's an example of the desired output (Change in Sales) :
PriorMonthSales =CALCULATE(
SUM(Table[Sales]),
FILTER(
Table[TeamID]&&
Table[Date] - 1
)
PriorQuarterSales =CALCULATE(
SUM(Table[Sales]),
FILTER(
Table[TeamID]&&
Table[Date] - 3
)
PriorSales = IF(Table[PaymentType] = "Monthly",PriorMonthSales, PriorQuarterSales)
These formulas obviously don't produce the desired result, but I am sharing them here to show my approach to solving this. Perhaps, someone more knowledgeable here can assist with the correct syntax and logic.
Thanks!
To calculate the total of the previous month/quarter you can add a calculated column. However you need to change your month column to a date.
jan 16 => 1/1/2016
formula:
=
IF (
myTable[paymenttype] = "monthly",
CALCULATE (
SUM ( myTable[sales] ),
FILTER (
myTable,
myTable[TeamID] = EARLIER ( myTable[TeamID] )
&& DATEADD ( myTable[date], 1, MONTH ) = EARLIER ( myTable[date] )
&& myTable[paymenttype] = EARLIER ( myTable[paymenttype] )
)
),
CALCULATE (
SUM ( myTable[sales] ),
FILTER (
myTable,
myTable[TeamID] = EARLIER ( myTable[TeamID] )
&& DATEADD ( myTable[date], 3, MONTH ) = EARLIER ( myTable[date] )
&& myTable[paymenttype] = EARLIER ( myTable[paymenttype] )
)
)
)