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.
Related
I need your help.
I have a table (“Table”) like this.
In the table below I have SUM “sales” by the LATEST 2 days with sale (not the latest 2 DATES! i.e. example: if the latest sales update is on a Tuesday, it sums the sale for Monday and Friday (no sale in weekend)) for each products.
in other words:
The calculation is made with the following DAX calculated column:
Sale last 2 days=
VAR ProductDates =
CALCULATETABLE (
VALUES ( Table[Date]),
ALLEXCEPT ( Table, Table[Product_ID])
)
VAR LastTwoDates = TOPN ( 2; ProductDates;[Date] )
RETURN
CALCULATE (
SUM ([Sale]);
ALLEXCEPT ( Table, Table[Product_ID] );
Table[Date] IN LastTwoDates)
Now, I need to take it a step further:
What I want to do is to make a new calculations which SUM the sale for each product for the latest 2 days, but ONLY for the Distributors, where the "Distributor indicator"=1. And the latest 2 sales days in question, are the sales days where there has been sale to these distributors only.
(example: if the latest sales day is a tuesday and there were no sale from these distributors yesterday, the the latest two days will be previous friday and thursday (i.e. the latest 2 days where sales is not null).
I know I can use the calculation, I have already made, but I can’t figure out where to put the logic in, in order to get the right result:
Example:
I know I can use the calculation, I have already made, but I can’t figure out where to put the logic in, in order to get the right result:
Can some of you please help!
Thanks. It is greatly appreciated.
Br,
Jakob
Assuming there is a date dimension table named "Calendar", this worked for me.
=IF(
COUNTROWS(
INTERSECT(
VALUES('Calendar'[Date]),
CALCULATETABLE(VALUES(Table[Date]), ALL(Table))
)
) > 0,
CALCULATE(
SUM(Table[Sale]),
CALCULATETABLE(
TOPN(2, SUMMARIZE(Table, Table[Date]), Table[Date], DESC),
FILTER(ALL(Table), Table[Date] <= MAX('Calendar'[Date]))
),
ALL('Calendar')
)
)
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.
I am newbie in Power Bi. I am calculating months to date of a measure.
I have written following DAX formula for that,
MTD in Sales = CALCULATE([Total Sales], DATESMTD(Dates[Date]) )
it shows me correct total sales value of this month.But when i make day-wise, it shows me some unrealistic value.
I have attached a screenshots of my result..Please have a look.
I don't understand what wrong are going on? Can you please find out the problem plz?
DATESMTD(Dates[Date]) is equivalent to:
CALCULATETABLE(
FILTER(
ALL(Dates[Date]),
AND(
Dates[Date] <= MAX(Dates[Date]),
AND (
YEAR(Dates[Date]) = YEAR(MAX(Dates[Date])),
MONTH(Dates[Date]) = MONTH(MAX(Dates[Date]))
)
)
)
)
This only considers the maximum value of the dates in the external filter context, so for Tuesday (today), it will contain every day of the month up to today, for Monday (yesterday) it will contain every day of the month up to yesterday and so on. (Assuming that no Sales are linked to future dates).
If you want to further filter this to only include sales that occurred on the given day of the week, I'd suggest altering MTD in Sales to:
[MTD in Sales] := CALCULATE([Total Sales], DATESMTD(Dates[Date]), Dates[DayOfWeekName])
This will additionally filter the included dates to only those with DayOfWeekName values present in the external filter context.
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.
I have a table of defect data that I would like to create a MEASURE that gives me a count of defects for each month. I know I need to use a date table but my attempts thus far haven't worked out.
What I am looking for when this works is a simple count by month:
January 125
February 225
March 220
April 120
Defect Table
Date Table
Here is the Measure I was trying to build without any luck...
Monthly Defects =
// TOTALYTD(COUNT(Defects[Defect]), 'Date'[Date])
VAR defectDate = FIRSTDATE(Defects[Created Date])
VAR defectYear = YEAR(defectDate)
VAR defectMonth = MONTH(defectDate)
RETURN
CALCULATE (
COUNT(Defects[Defect]),
FILTER (
Defects,
Defects[Created Date] <= defectDate &&
(YEAR (Defects[Created Date]) = defectYear) && (MONTH(Defects[Created Date]) = defectMonth)
)
)
Here is what I am looking to do in the end.
If you simply want a count per month, then your measure should be
Monthly Defects=COUNTROWS(Defects)
This is assuming that you have a relationship between your defect table and your date table.