DAX Last Year for Count - powerbi

I’m trying to calculate last year data incorporated in the following measure.
Previous Month = VAR CurrMonth = MONTH ( TODAY () ) RETURN IF ( CurrMonth = 1, 12, CurrMonth - 1 )
I’ve tried using PREVIOUSYEAR if the current month is 1 (Jan) but it’s grabbing all previous years.

Related

Same day of Last Week and Last Month PowerBI

I need date functions as below.
Last month of a specific date selected and the same day last week.
Example
4th day of last week = today 2020-12-02 return 2020-11-25
same day last month = today 2020-12-02 return 2020-11-04
Can you give an example?
For the same week day last week it's easy. Since the DATE is kept as an integer representing the days elapsed since the 12/30/1899, it's enough to subtract 7 from the current date.
Same Week Day Last Week =
VAR CurrentDay = MAX('Date'[Date])
RETURN CurrentDay - 7
The same week day last month measure is lacking a precise definition.
It's not possible to map weeks over months and for this reason week based calendars exist.
Here is a DAX measure that tries to keep most of the days inside the month
Same Week Day last month =
VAR CurrentDay =
MAX ( 'Date'[Date] )
VAR WeekDaysDelta =
WEEKDAY ( EOMONTH ( CurrentDay, -1 ) ) - WEEKDAY ( EOMONTH ( CurrentDay, -2 ) )
VAR DaysAdjust =
IF (
WeekDaysDelta > 3,
WeekDaysDelta - 7,
IF ( WeekDaysDelta < -3, WeekDaysDelta + 7, WeekDaysDelta )
)
VAR SameWeekDay =
EOMONTH ( CurrentDay, -2 ) + DAY ( CurrentDay ) + DaysAdjust
RETURN
SameWeekDay
There is an ISO week date standard that defines week based calendars.
Here is a link to an article about working with weeks in DAX
Week-related calculations

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

Output value from Pervious Month (December) in January

Hopefully someone can help me.
I use the measure below to output a value from the previous month, however it appears to have broken this month (January).
In March it outputs Februarys value, and in April it outputs Marchs value. However as it is January it doen't appear to be outputting Decembers value.
Can anyone advise how I would adapt this measure to retrieve Decembers value?
Previous Month = var current_month= MONTH(TODAY()) return CALCULATE( AVERAGE('DATA_TABLE'[VALUE]), FILTER(DATA_TABLE,MONTH(DATA_TABLE[DATE])=current_month-1))
Thanks for your support.
Dax's Month commmand returns a number. In your case, you're asking for month 0, as january is month number 1. Since there is no month numbered as 0, it returns no data (blank).
You just need to add a condition for when the month is 0, to instead return data for the previous year's last month.
This is a possible solution:
VAR current_month = MONTH(TODAY())
VAR current_year = YEAR(TODAY())
VAR previous_month = current_month - 1
VAR previous_year = current_year - 1
RETURN
CALCULATE(
AVERAGE('Table'[Values]);
FILTER('Table';
IF(previous_month = 0;
MONTH('Table'[Date]) = 12 && YEAR('Table'[Date]) = previous_year;
MONTH('Table'[DATE]) = current_month - 1)
)
)
This way you accomodate for the 0 month value and filter according to it.
If it is not 0, the measure behaves as before. Otherwise it filters for the last month of the previous year.

Calculate Number of Working Days based on a Month and Year Column - DAX

I have a column like below for which I would want to extract the number of working days (excluding just weekends- Saturday and Sunday, holidays needs not be addressed).
As of now I just want:
Required_Column = Total No of Days in that month - No of weekends in that month
Month_Year
01-2018
02-2018
03-2018
...
...
01-2019
02-2019
I am a newbie in Power query and DAX , i tried looking up various methods using DAX however, could not find any relevant lead.
Expected Output:
Month_Year Required_Column
01-2018 23 (31-8)
02-2018 20 (28-8)
03-2018 22 (31-9)
... ...
... ...
01-2019 23 (31-8)
02-2019 20 (28-8)
Appreciate your help on this.
Although a Calendar table based approach is recommended as in the comment by RADO and Strawberryshrub, it is also possible to do this with DAX calculated column.
In the example below, I'm assuming MonthYear column contains the first day of each month.
WorkingDays =
VAR Year = YEAR( [MonthYear] )
VAR Month = MONTH( [MonthYear] )
VAR DatesInMonth = GENERATESERIES( [MonthYear], DATE( Year, Month + 1, 1 ) - 1, 1 )
RETURN SUMX(
DatesInMonth,
IF( WEEKDAY( [Value] ) IN { 1, 7 }, 0, 1 )
)

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