I need help creating a measure that will count the total merch booked from a previous working day.
I currently have the following:
dimDate table
A. This table contains following:
i. Date Column
ii. Dayofweek column: 1=Sunday, 2=Monday, 3=Tuesday, 4=Wednesday, 5=Thursday, 6=Friday, 7=Saturday
iii. Working Day column: indicates whether it is a "Working" or "Non-Working" day based on the combination of "Dayofweek" and "Holiday" Column.
Total Merch Booked measure
Here are the conditions that this previous day measure should follow:
Weekday (TODAY()) = 2 (Monday), then it should look whether the Friday before was a working day, if so, then it should calculate Total Merch booked on that day, otherwise keep repeating to the previous day to it until it finds a working day and then calculate Total Merch Booked.
Weekday(TODAY()) =1 OR Weekday (TODAY()) =7 (Saturday or Sunday), then skip and do not calculate the Total Merch booked.
Weekday(TODAY()) = any other number besides 1, 2 or 7 (Tuesday thru Friday), then it should look at the previous day to see if it is a Working day, if so, then it should calculate Total Merch booked on that day, otherwise going in backwards until it finds a working day and then calculate Total Merch booked.
I tried to use the following, but i also need to count 'holidays' in to the mix:
IF(
WEEKDAY(TODAY()) = 2,
CALCULATE(
[Total Merch Booked],
'dimDate'[Date]= (TODAY()-3)
),
IF(
WEEKDAY(TODAY()) = 1,
BLANK(),
IF(
WEEKDAY(TODAY()) = 7,
BLANK(),
CALCULATE(
[Total Merch Booked],
'dimDate' [Date] = (TODAY()-1)
)
)
)
)
This is really difficult to suggest without sample data and expected output. But you can you try with this below measure-
total_merch_booked =
VAR previous_working_day =
CALCULATE(
MAX('dimDate'[Date]),
FILTER(
ALL('dimDate'),
'dimDate'[Date] < TODAY()
&& 'dimDate'[Working Day] = "Working"
)
)
RETURN
IF(
WEEKDAY(TODAY()) IN { 1, 7 },
BLANK(),
CALCULATE(
[Total Merch Booked],
FILTER(
ALL('dimDate'),
'dimDate'[Date]= previous_working_day
)
)
)
Related
I have the following fields:
Year
Category
Maker
Month
Month Number
Sales Volume
Sales
Date
So, I have in my dash a filter for "Month Number" and "Year":
My goal is to create two new measure; first with the Rolling Year that need to sum 12 months, ending in the moment that the user select in the mencioned filters. For example if y select Year 2021 and Month 01. The Rolling Year need to sum the sales of a selected category since 2020-02 to 2021-01 (thats mean always 12 months since a pivot month).
For thesecond is exactly the same, a measure called Rolling Last Year, it need to be a rolling sum too, but for the last period in order to compare. Taking the same example if I have the period 2020-02 to 2021-01. The Rolling Last Year for the last period should be 2019-02 to 2020-01.
I tried with this DAX code, that extracted from Microsoft page but without success:
Rolling Year =
CALCULATE (
SUMX ( Table, Table[Sales] ),
FILTER (
ALL (Table[Date] ),
AND (
Table[Date] <= MAX ( Table[Date] ),
DATEADD ( Table[Date], 12, MONTH ) > MAX ( Table[Date] ))))
I share you an extract of my base:
Thanks in advance !!!
Based on the table and code you have shared, it is unclear from where the date filters are being applied. In case you have not done it, I strongly suggest to delete the [Month] and [Month Number] field from your Sales table and keep them in a separate Calendar table, from where the filters should be selected. Then, a simple tweak on you current formula should do the trick:
Rolling Year =
CALCULATE (
SUMX ( Table, Table[Sales] ),
FILTER (
ALL ('Calendar'[Date] ),
AND (
Table[Date] <= MAX ( 'Calendar'[Date] ),
DATEADD ( 'Calendar'[Date], 12, MONTH ) > MAX ( 'Calendar'[Date] ))))
However you can try with this variation for the code, a little bit optimized so as not to scan your whole Sales table each time:
Rolling Year =
VAR EndSelectedDate = MAX ( 'Calendar'[Date] )
VAR StartSelectedDate =
CALCULATE (
MAX ( 'Calendar'[Date] ),
ALL ( 'Calendar'[Year] ),
'Calendar'[Year]
= MAX ( 'Calendar'[Year] ) - 1
)
RETURN
CALCULATE (
SUM ( Table[Sales] ),
ALL ( 'Calendar' ),
'Calendar'[Date] <= EndSelectedDate,
'Calendar'[Date] > StartSelectedDate
)
Hi I need to calculate day over day
I attached the pbix file https://1drv.ms/u/s!Amd7BXzYs7AVhBBCo_Ls7q5IkrXH?e=d1nNmA
I've tried the following calculated measure but actually it returns wrong values, it returns sales for current date, but I need to return sales for previous day. How to correct it?
sales day over day = var _maxdate=CALCULATE(max('Date'[Date]), ALLSELECTED('Date'[Date]))
var _mindate=CALCULATE(min('Date'[Date]), ALLSELECTED('Date'[Date]))
var dates=filter(values('Date'[Date]), 'Date'[Date]<=_maxdate && 'Date'[Date]>=_mindate)
return if(ISEMPTY('Date'), BLANK()
, calculate(sum(Sales[Sales]), FILTER(Sales, Sales[Date]<=_maxdate && Sales[Date] >=_mindate && PREVIOUSDAY('Date'[Date])))+0
)
It seems that your measure works. I've got sales for previous days.
You may need to use simple measures as follows:
TotalSales =
SUM ( Sales[Sales] )
PreviousDaySales =
CALCULATE ( [TotalSales], PREVIOUSDAY ( 'Date'[Date] ) )
DayOverDayChange =
DIVIDE ( [TotalSales], [PreviousDaySales], 0 )
The result (not on your data):
I have the below measure which calculates the headcount. I would like to create a new measure which calculates the running total for the past 12 months.
TIA
Employee Count =
VAR selectedDate = MAX('Date'[Date])
RETURN
SUMX('Table1',
VAR employeeStartDate = [DATE_OF_EMPLOYMENT]
VAR employeeEndDate = [DATE_OF_LEAVING]
RETURN IF(employeeStartDate <= selectedDate &&
OR(employeeEndDate >= selectedDate, employeeEndDate=BLANK()
),1,0)
)
If I get you right, you want something that is called Moving Annual Total (MAT). Assuming that you have a Date table (Calendar) you can use the following pattern. Replace the [Sales Amount] measure with your favorite one.
Sales MAT :=
CALCULATE(
[Sales Amount],
DATESINPERIOD(
'Date'[Date],
MAX( 'Date'[Date] ),
-1,
YEAR
)
)
Check for MAT here: https://www.daxpatterns.com/standard-time-related-calculations/
I want to calculate the sum of MTD sales per day for the same period of last year, only until a specific day - the day of the last sales transaction.
So for example, if the filter is year= 2019 and month= 2, I need the MTD sales for february 2018 until the 5th, calculated day by day:
MTDSales=
VAR MyDay = 5
RETURN
CALCULATE(
TOTALMTD(SUM(Sales); Calendar[Date]);
SAMEPERIODLASTYEAR(Calendar[Date]);
//here I need another filter to stop on the 5th!
)
Edit:
Please have a look at this link to see the sample data.
The measures I'm trying to build are:
Sales MTD CY
Sales MTD LY
Sales MTD CY*
Sales MTD LY*
Sales MTD CY**
Sales MTD LY**
Thanks for helping!
I'm assuming that you are using 5 since today is February 5th.
You can get MTDSales for the current month like this:
MTDSales =
VAR DateRange = DATESBETWEEN( Calendar[Date], EOMONTH(TODAY(), - 1) + 1, TODAY() )
RETURN CALCULATE( SUM( Sales ), DateRange )
To match that up with the previous year, just use SAMEPERIODLASTYEAR.
LastYearMTDSales =
VAR DateRange = DATESBETWEEN( Calendar[Date], EOMONTH(TODAY(), - 1) + 1, TODAY() )
RETURN CALCULATE( SUM( Sales ), SAMEPERIODLASTYEAR(DateRange) )
If you want to use a different date than TODAY, just specify that date as a variable and pass it into the DateRange variable where TODAY appears.
If you want to find the MTDSales up to the 5th day of the month (assuming you have the month in your filter context), try this
MTDSales =
Var MyDay = 5
VAR MyDate = MIN( Calendar[Date] ) + MyDay - 1
VAR DateRange = DATESBETWEEN( Calendar[Date], EOMONTH(MyDate, -1) + 1, MyDate )
RETURN CALCULATE( [Sum of Sales], DateRange )
Then for the previous year, you can reuse that measure, but shifted:
PrevYearMTDSales =
CALCULATE( [MTDSales], ALL( Calendar ), SAMEPERIODLASTYEAR( Calendar[Date] ) )
Edit: After looking at your PBIX, I realized that I had made the wrong assumption about your filter context. Since you are looking to write a measure at the date level, try this instead:
Sales MTD CY =
VAR MyDay = 5
VAR CurrentDate = MAX ( 'Calendar'[Date] )
VAR MyDate = EOMONTH ( CurrentDate, -1 ) + MIN ( MyDay, DAY ( CurrentDate ) )
RETURN
CALCULATE (
TOTALMTD ( SUM ( Sales[Sales] ), 'Calendar'[Date] ),
FILTER ( 'Calendar', 'Calendar'[Date] <= MyDate )
)
The previous year measure can still be done referencing this measure and shifting.
Replace my column names for yours and it should work
The formula remains the same:
MTDSales =
VAR MyDay = 5
RETURN
CALCULATE(
TOTALMTD([total sales], 'Calendar'[DateKey]),
SAMEPERIODLASTYEAR('Calendar'[DateKey]),
FILTER(
ALL(Sales),
Sales[DateKey] >= STARTOFMONTH('Calendar'[DateKey]) && Sales[DateKey] <= DATEADD(STARTOFMONTH(Sales[DateKey]),MyDay-1,DAY)
)
)
I have a Measure which calculates a cumulative total:
CumulativeCount:=
VAR date1 = MAX( DimDate[Date] )
VAR date2 = MAX( FactTable[EndDate] )
RETURN
CALCULATE (
SUM( FactTable[Count] ),
DimDate[Date] <= date1,
DimDate[Date] <= date2,
ALL( DimDate[Date] )
)
And another, actually used in the Pivot Table, which, when it's calculating the Grand Total, is supposed to add up the cumulative totals for each date:
CumulativeCountForPivot:=
IF (
-- If calculating for one group
COUNTROWS( VALUES( FactTable[Group] ) ) = 1,
-- Do core logic
[CumulativeCount],
-- Else add up the results from each group
SUMX(
VALUES( FactTable[Group] ),
[CumulativeCount]
)
)
I don't understand why the Grand Total in the final column is 12, not 6.
The reason is that the grand total is for GroupA and GroupB combined and there is a cumulative count of 12 on that date (6 for each group).
On 06/01/2017 there are no records for GroupA so the [CumulativeCount] measure is returning a blank, even though there are records before that date and the count would be 6. If you added a record for GroupA with a Count of 0 on 06/01/2017, then you would see 6 appear.
If you want a measure that only shows 6 on that date, then try something like this:
CountForPivot =
VAR TempTable = SUMMARIZE(FactTable,
FactTable[Group],
FactTable[EndDate],
"Cumulative",
CALCULATE(SUM(FactTable[Count]),
FILTER(ALLEXCEPT(FactTable, FactTable[Group]),
FactTable[EndDate] <= MAX(FactTable[EndDate])
)
)
)
RETURN SUMX(TempTable, [Cumulative])