I have the following tables:
Revenue
Branch
Date
I have a table viz with branch name, % over last year for revenue
Here, my calculations are correct and numbers are correct as per requirement.
DAX I am using:
% over Last Year = IFERROR(
([Revenue 2019 YTD] / [Total Revenue 2018 for YTD]) -1,
BLANK())
Problem:
For 3 branches, these branches were acquired mid-year on 2018 and only has data from July of 2018.
When I am calculating data for % over last year, the numbers are incorrect for these branches as they only had partial data on 2018 and complete data ( Jan to current month) in 2019.
I need help on how I can calculate the % over last year also while considering the min date of 2018 for some branches that were acquired mid-year in 2018.
The solution to my question:
This Year YTD branch Growth with Partial Data =
var _thisyear = YEAR([Today])
var _currentweek = WEEKNUM([Today])
// last year min date
var _minweek =
CALCULATE(
SUMMARIZE(
Revenue,
"Min Date lY", CALCULATE(MIN(Revenue[weeknum])))
, FILTER(WeekCalendar, WeekCalendar[CalendarYear] = _thisyear - 1
))
return
SUMx(
SUMMARIZE(Revenue,
Revenue[Weekkey],
"Revenue YTD",
CALCULATE(
SUM(Revenue[Revenue]),
FILTER(Revenue, Revenue[weeknum] <= _currentweek),
FILTER(Revenue, Revenue[Year] = _thisyear),
FILTER(Revenue, Revenue[weeknum] >= _minweek), GROUPBY(branch, branch[Branchname])
)
),
[Revenue YTD]
)
If anyone has any suggestions to my DAX, Please let me know as well.
Related
We are using a date table based on a fiscal calendar. I need to determine if a particular year has 53 weeks or 52 weeks.
The page has a slicer with a date hierarchy that is used to select a year, quarter, month, or week. I would like the Measure to return the maximum number of weeks in a particular year regardless of what is selected.
The measure below works fine, it returns 53 which is the maximum number of weeks in any year regardless of the selection in the date hierarchy slicer.
PriorYearNumberOfWeeks = calculate(max(vwPBIDate[Week of Year]),REMOVEFILTERS(vwPBIDate))
However, when I try to add a filter condition to select the proper year it no longer works it returns the maximum week number of the period selected in the hierarchy. If I select January (a 5 week month) it returns 5 instead of 53 then number weeks in fiscal 2021.
PriorYearNumberOfWeeks = calculate(max(vwPBIDate[Week of Year]),REMOVEFILTERS(vwPBIDate),filter(vwPBIDate,vwPBIDate[Fiscal Year Number]=2021))
You're missing an ALL:
=
CALCULATE(
MAX( vwPBIDate[Week of Year] ),
REMOVEFILTERS( vwPBIDate ),
FILTER(
ALL( vwPBIDate ),
vwPBIDate[Fiscal Year Number] = 2021
)
)
which is equivalent to:
=
CALCULATE(
MAX( vwPBIDate[Week of Year] ),
REMOVEFILTERS( vwPBIDate ),
vwPBIDate[Fiscal Year Number] = 2021
)
I have a daily sales data for 3-4 years. I want to create the YTD and Prior Year To Date sales measure that will be updated daily. That is it should always be from beginning of the year (selected) to TODAY or the last day of the data (1 day lag from today and max date).
I used Sameperiodlastyear but it is problematic at the beginning of the month as it compares say Jan 1, 2022-June 8 2022 with Jan 1, 2021 with June 30, 2021.
Any suggestion how I can create a modified prior year to date measure to address this nuance?
This is a standard solution for the case. First, you get all dates, with a DATESYTD() function, for the current year or last visible year up today or last visible day, then you offset it.
SAMEPERIODLASTYEAR(DATESYTD(‘Date’[Date]))
It is equal to
DATEADD(DATESYTD(‘Date’[Date]),-1,YEAR))
Try this if you want to get exact days set:
VAR FirstDayThisYear =
SAMEPERIODLASTYEAR(STARTOFYEAR(‘Date’[Date])
VAR LastDayThisYear =
SAMEPERIODLASTYEAR(
LASTDATE(‘Date’[Date])
)
VAR SetOfDates=
DATESBETWEEN(
‘Date’[Date]
,FirstDayThisYear
,LastDayThisYear
)
RETURN
SetOfDates
I have a calculated column "LastSaleDate" that finds the last date with sales with the following dax expression:
LastSaleDate=LASTDATE(filter(ALL(Dim_Time[Date]),[Sales Amount] >0))
What I want to achieve next is to create another calculated column "LastSaleDateStartingFromYesterday". So for example if the current date is Monday and there are no sales on Sunday or Saturday it should return Friday as the last date with sales (See example below). It is possible that Saturday and Sunday have Sales so I cannot exlude/filter them out.
How to write such a dax expression ?
All tips, solutions and help are highly appreciated
Best regards, Rubrix
I found that your calculation for last sales date is not properly applying the filter for [Sales Amount] > 0. You can see this if you add an additional row for 2022-05-17 where [Sales Amount] = 0.
Here are the DAX formulas that worked for me:
LastSaleDate2 = CALCULATE(
MAX('Table'[Date]),
FILTER('Table','Table'[Sales Amount]>0)
)
LastSaleDateStartingFromYesterday2 = CALCULATE(
MAX('Table'[Date]),
FILTER('Table','Table'[Sales Amount]>0 && VALUE('Table'[Date]) < VALUE(TODAY()-1))
)
These can be converted to True/False by adding "= 'Table'[Date]" to the end of the formula.
You can add a calculated column:
PreviousSalesDate =
var sDate = Sales[Date]
var lDate = CALCULATE(max(Sales[Date]), FILTER(Sales, sDate > Sales[Date] && Sales[Sales Amount] > 0))
return lDate
We work with variables in the code. sDate is teh current date of the row. Next we get the max of all rows where the date is smaller than the sDate (all rows before) and the sales is bigger than zero.
By the data you have given we only get the 13th of may.
I am working in POWER BI and trying to calculate a DAX expression for the rolling total of the previous month. I have a filter where I select a certain month, I would like to calculate the rolling total for the previous month.
Below is the calculation that works perfectly to calculate the rolling total for the selected date range.
How can I calculate the previous months rolling total?
Rolling_Total_Current_Month = CALCULATE(
SUM(SalesInvoice[Sales])
,FILTER(ALLSELECTED(SalesInvoice), (SalesInvoice[Date]) <= MAX(SalesInvoice[Date])))
Here is a sample of my data, I have sales per day, for multiple categories, (in fact i have a couple more columns of details but this is simplified)
Date Day Amount Category
1/1/2016 1 100 A
1/1/2016 1 120 B
1/1/2016 1 90 C
1/2/2016 2 500 A
1/2/2016 2 321 B
1/2/2016 2 143 C
So far I have come up with an equation to solve the rolling total, but when I try to slice is and view the rolling total of a single category it does not work for the previous month. I just keeps the original rolling total for the previous month.
Here is the equation for the rolling total previous month that works. But does not recalculate a rolling total for the previous month once sliced based on category.
PREVIOUS_MONTH_ROLLING_TOTAL =
CALCULATE(
[Current Sales]
,FILTER(
ALL( Orders )
,Orders[MonthNumber] = MAX( Orders[MonthNumber] ) - 1
&& Orders[Day] <= MAX( Orders[Day] )
)
)
I have solved how to get the previous months rolling total.
You must do three things. First create a Month Number column in your data sheet (this is used as an integer to subtract 1 month from). You must also create a days column as well.
Then create a measure for Current Sales or whatever your value is.
Create a measure for the current month sales
Current Sales = SUM(Orders[Amount])
Then this equation.
PREVIOUS_MONTH_ROLLING_TOTAL =
CALCULATE(
[Current Sales]
,FILTER(
ALL( Orders )
,Orders[MonthNumber] = MAX( Orders[MonthNumber] ) - 1
&& Orders[Day] <= MAX( Orders[Day] )
)
)
The Idea of this equation is to be able to display the previous months rolling total on a chart with the X axis as "DAY" (so 1-31) Then you can view the current month, previous month, same period last year all on the same chart or table.
Try something along these lines:
Rolling_Total_Previous_Month =
VAR CurrentMonth = MAX(SalesInvoice[Date])
VAR PreviousMonth = EOMONTH(CurrentMonth,-1)
RETURN CALCULATE(SUM(SalesInvoice[Sales]), SalesInvoice[Date] <= PreviousMonth)
To start of, I have a data like this in a table called as Orders-
Date Amount
12/12/2017 100
12/12/2017 200
12/12/2017 300
1/1/2018 400
1/1/2018 500
I first create a calculated column called as Year & Month by using the formula:-
Year = YEAR(Orders[Date])
Month = FORMAT(Orders[Date],"mmmm")
Then I create a column called as Month Number which will be helpful for sorting when more than one year is involved in the table and as well as to Identify the Previous Months.
MonthNumber = DATEDIFF(Min(Orders[Date]),Orders[Date],MONTH)
Current Month Sales can be a measure or a Calculated column. Qn the Question, you had your answer for current month sales via a calculated column and if you want to go for a measure then something like this would work on a summary table.
Current Month Sales = SUm(Orders[Amount])
I would also create a column called as Key:-
Key = Orders[MonthNumber] & Orders[Category]
Now, for the Previous Month Sales, I would create a measure that looks for selected MonthNumber that we created.
Previous Month Sales =
Var SelectedCategory = SELECTEDVALUE(Orders[Category])
Var SelectedMonthNumberr = SELECTEDVALUE(Orders[MonthNumber]) - 1
Var ReqKey = SelectedMonthNumberr & SelectedCategory
Return
IF(ISBLANK(SelectedCategory) <> True(),
CALCULATE(SUM(Orders[Amount]),FILTER(ALL(Orders), Orders[Key] = ReqKey)),
CALCULATE(SUM(Orders[Amount]),FILTER(ALL(Orders), Orders[MonthNumber] = SelectedMonthNumberr)))
or to your Measure
PREVIOUS_MONTH_ROLLING_TOTAL =
CALCULATE(
[Current Sales]
,FILTER(
ALL( Orders )
,Orders[MonthNumber] = MAX( Orders[MonthNumber] ) - 1
&& Orders[Day] <= MAX( Orders[Day] )
)
)
You can just add another filtering item as && Orders[Category] = SELECTEDVALUE(Orders[Category]) but won't work when you don't have any categories selected or on a table or visual which doesn't have categories. So, you would need to define an if else logic here as I have quoted on my measure formula.
Do let me know, if this helps or not.
Need to calculate YTD Average and LYTD Average in DAX , Our Fiscal year starts from Apr.
So if my current day is 5th of the June 2017 then lytd_AVG would be from Apr to June would be
(MTD Aprn 17) + (MTD May 17) + (MTD June 17)/3
and LYTD_AVG would be
(MTD Apr 16) + (MTD May 16) + (MTD June 16)/3
Assuming my current date is 06-03-2017, the YTD avg would be 60
Assuming your data is given monthly (since you have no sample data), for YTD you should be able to use something that looks like this:
YTD Avg = CALCULATE(AVERAGE(Table[Value]), DATESYTD(Table[Date], "03-31"))
The last argument specifies the year-end date (Reference).
You can use the SAMEPERIOODLASTYEAR function for LYTD:
LYTD Avg = CALCULATE(AVERAGE(Table[Value]),
SAMEPERIODLASTYEAR(DATESYTD(Table[Date], "03-31")))
Edit: Since your data is daily, we need to do a bit more work.
First, add month and year calculated columns to your table:
Month = MONTH(Table[Date])
FiscalYear = YEAR(MINX(DATESYTD(Table[Date], "03-31"), [Date]))
Then you will group by months in your measures:
YTD = AVERAGEX
SUMMARIZE(Table1,
Table1[Month],
"MTDAmount", SUM(Table1[Amount])),
[MTDAmount])
LYTD = AVERAGEX(
SUMMARIZE(
FILTER(ALL(Table1),
Table1[Month] IN VALUES(Table1[Month]) &&
Table1[FiscalYear] = MAX(Table1[FiscalYear]) - 1),
Table1[Month],
"MTDAmount", SUM(Table1[Amount])),
[MTDAmount])
You should then be able to build tables like this: