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.
Related
I have created a measure which calculates the percent of completion for each project. But my client wants it to display blanks after the project is completed, i.e. after the first 100%.
I already tried using an If function, but it is returning the same value for every month. I also looked online, but did not find a solution. Here is my %OfCompletion measure and the measure that it depends on.
% of Completion:=
VAR sproject =
IF ( HASONEVALUE ( Project[Project] ), VALUES ( Project[Project] ) )
RETURN
CALCULATE (
DIVIDE (
[S Expenses Running Total],
CALCULATE (
[Total Sales Costs] + [Total Sales Hours],
ALL ( Sales ),
Sales[Project] = sproject
)
),
Project[Classification] = "IN"
)
Expenses Running Total:= CALCULATE (
[Total Sales Costs] + [Total Sales Hours],
FILTER (
ALL ( Dates ),
Dates[Current Month Offset] <= MAX ( Dates[Current Month Offset] )
)
)
Example Values,
And a screenshot of my model.
This checks if current month completion is 100% AND prior month completion is also 100%, and returns BLANK - otherwise returns the actual completion value:
Monthly Completion Measure:=
IF (
[% of Completion] = 1 &&
CALCULATE (
[% of Completion],
PARALLELPERIOD (
Dates[Date],
-1,
MONTH
)
) = 1,
BLANK,
[% of Completion]
)
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
)
)
)
IIF(SUM
(
[Calendar].[Month].CurrentMember.Lag(11) :
[Calendar].[Month].CurrentMember,
[Measures].[Qty]
) = 0, 0,
SUM
(
[Calendar].[Month].CurrentMember.Lag(11) :
[Calendar].[Month].CurrentMember,
[Measures].[Num]
) /
SUM
(
[Calendar].[Month].CurrentMember.Lag(11) :
[Calendar].[Month].CurrentMember,
[Measures].[Qty]
) )
This is formula from multi dimensional model i am trying to convert this MDX formula to DAX formula to use in Tubular model.
12 Month Avg :=
IF (
CALCULATE (
SUM ( [QTY] ),
FILTER (
ALL ( Calendar[Month] ),
Calendar[Month] - 11
= ( Calendar[Month] - 11 )
)
)
= 0,
BLANK (),
CALCULATE (
SUM ( [Num] ),
FILTER (
ALL ( Calendar[Month] ),
Calendar[Month] - 11
= ( Calendar[Month] - 11 )
)
)
/ CALCULATE (
SUM ( [QTY] ),
FILTER (
ALL ( Calendar[Month] ),
Calendar[Month] - 11
= ( Calendar[Month] - 11 )
)
)
)
So i made this DAX formula to convert MDX formula at the top. However it seem not working properly when i select month in pivot table. Those two formula don't match when i filtered by month. How can i resolve that problem?
1) Create three base measures:
TotalNum := SUM([Num])
TotalQty := SUM([Qty])
Avg := DIVIDE ( [TotalNum], [TotalQty], 0 )
2) Create a calculated measure to calculate the average over the prior year including the current selected month:
AvgLastYear:= CALCULATE (
[Avg] ,
DATESINPERIOD (
Calendar[Date] ,
MAX(Calendar[Date]),
-1, year
)
)
Explanation:
First, you don't need that divide by zero rigmarole, both MDX and DAX have a DIVIDE() function which handles that implicitly.
Second, with DAX, the idea is to build a base measure and then use CALCULATE() to shift the context of that measure as needed - in this case the time period.
Here we're looking at the current selected month (represented as MAX(Calendar[Date]), though you could use any aggregation function) and then using DATESINPERIOD() to choose a set of dates in our Calendar table which represent the time period T-1 year to the current month.
I've looked everywhere for this but any other example is not exactly what I want to achieve. I have data that looks like that:
Day Group Sales
14 1 15
13 0 22
14 1 17
and so on. Now I create calculated field of Running Total using following formula:
CALCULATE (
SUM(Table[Sales]),
FILTER (
ALL (table),
Table[Date] <= MAX(Table[Date])
)
)
I create Pivot Table and put Date and Group as rows, then Running Total as Values. Why running total is not aggregated separately for each group? I would like Group 1 to have its calculation, and Group 0 too.
The reason that running total is not aggregated separately for each group is because your formula is incomplete. You have just used the ALL function for FILTER. The ALL will mean that you will ignore the existing filter contexts. So on a row with Group 0 you will ignore the Group 0 filter.
What I think you want is to retain the Group filter. You can achieve this by replacing ALL (table), with either ALLEXCEPT(table,table[Group]), or ALL(table[Date]),. Both of these will give a running total just for the Group row so you will have a running total for Group 0 and one for Group 1.
If what you are looking for is a running total where the totals accumulate by both Date and Group you can keep ALL and add an extra expression for FILTER:
CALCULATE (
SUM ( table[Sales] ),
FILTER (
ALL ( table ),
table[Date] <= MAX ( table[Date] )
&& table[Group] <= MAX ( table[Group] )
)
)
I use code bellow and it works for me
CALCULATE (
SUM ( table[Sales] ),
FILTER (
ALL ( table ),
table[Date] <= MAX ( table[Date] )
&& table[Group] = MAX ( table[Group] )
) )