Month over Month Amount in Power BI - powerbi

Here's my DAX Function that gives me my total amount (say sales) for this month:
Current Month =
CALCULATE(
SUM (Budget[Amount]),
FILTER(
ALL ('Calendar'),
'Calendar'[MonthDate]= DATE(YEAR(TODAY()), MONTH (TODAY()),1)
)
)
How would I be able to edit this to give me the amount for last month, so that I can compare Month-over-month?

You can try this
Previous Month = CALCULATE (SUM (Budget[Amount]),
FILTER (ALL ('Calendar'),
'Calendar'[MonthDate]= DATE (YEAR ( TODAY ()), MONTH (TODAY () ) , -1 )))

To compare the current month sales with last month, you can store the current month in a variable like below:
Last_Month_Sales = var Current_Month= MONTH(TODAY()) return CALCULATE(SUM(Budget[Amount]),FILTER(ALL ('Calendar'),
'Calendar'[MonthDate]=Current_Month-1))

Related

Calculate the total annual sum for the previous year in Power BI

How can I calculate the total annual sum for the last year? Specifically, I need to calculate last year's total sales.
Here's what I do:
total_sales_last_year =
CALCULATE(
SUM(fact_sale_order[sales_amount]),
DATEADD(dim_date[formatted_date], -1, YEAR),
REMOVEFILTERS(dim_date[month_name])
)
However, if for example, I filter for January, in sales_last_year I won't get the sales of the whole year, but I will only have those of January 2021. The result I'm looking for is the total of the previous year.
Solution:
total_sales =
VAR last_year = YEAR(MAX(dim_date[formatted_date]))-1
VAR sales_last_year =
CALCULATE(
SUM(fact_sale_order[sales_amount]),
FILTER(ALLEXCEPT(dim_date, dim_date[year]), YEAR(dim_date[formatted_date]) = last_year )
)
VAR sales_current_year =
CALCULATE(
SUM(fact_sale_order[sales_amount]),
DATESYTD(dim_date[formatted_date])
)
RETURN sales_last_year + sales_current_year

Compare todays data against end of month of the previous month DAX

Lets just say that I want to compare todays data (3/10/2021) against the data of the last day of the previous month (2/26/2021), I am using February 26 because is the last business day of the previous month, Is there a Way to do this?
We can get the last day of February that is not a weekend (DAX doesn't have holidays, because each country can have its own free days).
calcForLastDayPrevMonth_noWeekend =
var __firstOfMonth = DATE( YEAR(TODAY()), MONTH(TODAY()), 1)
var __lastDayPrevMonth = calculate( MAX(DateTable[Date]), FILTER(ALL(DateTable), DateTable[Date] < __firstOfMonth && WEEKDAY(DateTable[Date]) not in {1,7} ))
return
calculate( sum(Table[SomeVal]), Table[date] = __lastDayPrevMonth )

Previous Working Day Measure to Incorporate Holidays

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
)
)
)

Total month to date for last year until a specific day of the month

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)
)
)

how to select last day (max day) of each month using dax

I have the following table:
I need to make a measure to return the values of "last day", but I cant use EOMONTH, because I have the current month, and the current month doesnt end yet, so, the last day of current month is today.
You can use EOMONTH with a bit of extra logic:
LastDay =
VAR CurrDate = MAX(Table1[Date])
RETURN CALCULATE(MAX(Table1[Date]),
FILTER(ALL(Table1),
Table1[Date] > EOMONTH(CurrDate, -1) &&
Table1[Date] <= EOMONTH(CurrDate, 0)))
This takes the max of the dates you have that occur between the end of the previous month and the end of the current month.
Once you have this measure, you can use it to calculate the sum of Value:
Last Date Sum = CALCULATE(SUM(Table01[Value]),
FILTER(Table01, Table01[Date] = [LastDay]))