PowerBI: Weekday or weekend - powerbi

I have a table AW_Calendar
Date Year Month name Day Name weekend
Friday, January 1, 2016 2016 January Friday
Saturday, January 2, 2016 2016 January Saturday
and so on
I am trying to write a DAX for whether a day is weekend or not, but I am getting an error
Weekend = if(AW_Calendar[Day Name]='Saturday'||'Sunday',1,0)
I am getting an error.
'Cannot find table 'Saturday''
What could be wrong

It expects double quotes " instead of single quotes ' for strings. It uses the latter for table names.
Note that using the WEEKDAY function may be more efficient here:
Weekend = IF ( WEEKDAY ( AW_Calendar[Date], 2 ) > 5, 1, 0 )

When you use any logical operator you have to write the table name for every condition. Also ' character refers to tables. If you want to compare text data you have to use " instead of '.
This should work:
Weekend = if(AW_Calendar[Day Name]="Saturday"|| AW_Calendar[Day Name]="Sunday",1,0)

Related

I'm trying to generate the week number in PowerBI for 2023 and its giving me incorrect figures

For instance week 1 for 2023 is 01/01/2023 to 07/01/202 which is wrong.
The correct week for week 1 of 2023 is 02/01/2023 to 08/01/2023
Screenshot of the calendar
Can someone please help?
I tried creating several times but to no avail
You can specify week systems in the WEEKNUM DAX function as an optional second argument.
The default for this optional parameter is system 1, where week 1 is the week that contains January 1st. System 2 sets week 1 as the week containing the first Thursday of the new year, which is ISO 8601 compliant.
Try this:
Week = WEEKNUM ( 'Date'[Date] , 2 )
If you are calculating this in Power Query, the calculation is much more complex, for some reason. See this link for a solution: https://datacornering.com/how-to-calculate-iso-week-number-in-power-query/

How to get week number per month on Power Bi?

I have a a column called Date originated that hold the a date like "Tuesday, March 21, 2022".
I want to get that week number per month but the following gives me incorrect numbers.
The following DAX: Week of Month = 1 + WEEKNUM('ECR'[Date Originated]) - WEEKNUM(STARTOFMONTH('ECR'[Date Originated]))
gives me 1 for that date.
Some entries are correct like for Wednesday, April 6, 2022 I do get 1
The problem is not the DAX expression, but the date column, which you didn't share with us.
STARTOFMONTH is not the right choice of function here, since that function does not by default return the first date of a given month; rather, it returns the first date of the month for the supplied dates (within the current context).
Here you are supplying only one date, and so, for example:
=STARTOFMONTH(21/03/2022)
will simply return 21/03/2022.
You should be using
=
1 + WEEKNUM( 'ECR'[Date Originated] )
- WEEKNUM( EOMONTH( 'ECR'[Date Originated], -1 ) + 1 )

DATEADD to calculate PYTD (Jan 1 till today's date) in DAX Power BI

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

PowerBI - Convert Date to the WeekNumber of the Month

Date WeekNum Month Year
5/2/2018 Week 1 May 2018
6/1/2018 Week 1 June 2018
How would you get the WeekNum from the Date?
The WeekNumber needs to be week number of the particular month and not the Year.
One approach would be to take the day of the month and divide by seven:
WeekNum = ROUNDUP(DIVIDE(DAY(TableName[Date]), 7), 0)
You can use the next formula
1 + WEEKNUM(usage_users[row_date]) - WEEKNUM(STARTOFMONTH(usage_users[row_date]))
This gives you the number of the week.
basically you need to use STARTOFMONTH and WEEKNUM together:
Here is a good video explaining it also:
https://www.youtube.com/watch?v=Oq5WOmo94_Q

Stata: How to modify some values in a string variable but keep original values?

I am working with a very large dataset (1 million obs.).
I have a string date that looks like this
key seq startdate (string)
AD07 1 August 2011
AD07 2 June 2011
AD07 3 February 2004
AD07 4 November 2004
AD07 5 2001
AD07 6 January 1998
AD5c23 1 January 2014
AD5c235 2 February 2014
AD5c235 3 2014
These are self-reported employment dates.
Some did not report the month at which they started.
But I would like to replace for AD07 the date “2001” to “January 2001”. Hence I cannot simply replace it because I would like to keep the original years but add the month in the string variable.
I started with:
levelsof start if start<="2016", local(levels)
which gives me all the years without the month from 1900 to 2016.
Now I would like to add "January" for the years without the month and keep original years.
How should I do that without using replace for every year? foreach loop?
You have a serious data quality problem if people are claiming to have started work in 1900 and every year since then! Even considering early employment starts and delayed retirement, that implies people older than the oldest established age.
Also, imputing "January" will impart bias as almost all job durations will be longer than they would have been. Real January starts will be correct, but no others: "June" or "July" or random months would make more obvious statistical sense.
That said, there is no loop needed here. You're asking for one line, say
replace startdate = "January " + startdate if length(trim(date)) == 4
or
replace startdate = "January " + startdate if real(startdate) < .
-- assuming a follow-up in converting to numeric dates. The logic there is that all year-only dates trim down to 4 characters, or (better) that feeding month names to real() will yield missings.
That said in turn, creating a new variable is better practice than over-writing one. Also, consider throwing away the month detail. Is it needed?
EDIT
You may have another problem if there are people with two or more jobs in the same year without month specifications. You don't want to impute all months in question as "January". You can check for such observations by
gen byte incomplete = real(startdate) < .
gen year = substr(trim(startdate), -4, 4)
bysort key year incomplete : gen byte multiplebad = incomplete & _N > 1