How to Get Custom Week Desc in DAX - powerbi

Basis on below data I want to add a calculated column with Week Description
I've done it in excel by typing it manually.
Also my week is starting from Thursday and ends on Wednesday hence I've Used this function to get the weekday WEEKDAY('Calendar'[date],14)
Requesting you to help me with a dax code which can be used to create a new calculated column with week information as shown below in third column.
The logic would be : If the date is current week then the value will be "This Week" else if the
date is in last week then "This Week -1" else if the date is in last to last week then "This Week - 2" and so on.

The weekday can be calculated as following:
Weekday = WEEKDAY([Date] + 3)
We do a shift of 3 days to make Thursday the start of the week
Next, we get the WeekDesc in two steps, frist we calculate the difference between now and the date in weeks and as second step we use an if statement to create the correct sting (and logic).
WeerDesc =
var weeksPast = DATEDIFF(now(), [Date] + 3,WEEK)
return if ( weeksPast = 0, "This Week", "This Week" & weeksPast)
As you can see you can use variables in your DAX, I would recommend using them to keep the overview.
Enjoy

We can do this also in this way (as a measure if you need only label to display on rows):
DayOfWeek = WEEKDAY(SELECTEDVALUE('CAL'[Date]),14)
DiffToToday = DATEDIFF(SELECTEDVALUE('CAL'[Date]),TODAY(),DAY)
Label = CONCATENATE("This Week", (DIVIDE( CAL[DiffToToday] + CAL[DayOfWeek],7) -1) *-1 )
Of course, we can do all steps in one measure.

Related

AWS Quicksight: Display week as a number, grouped by month and not year

I need to display Week as number in AWS QuickSight.I understand, we can do this to get the week numbers: In the field wells, click the format option for the date field and on the left side-panel, enter 'w' in the Custom format field.
The issue is this gives the week number in the year. How do I get week number per month?
You can use calculated fields to calculate it.
It depends how you define "week of month". In a simple case, if you define first 7 days of a month as "week 1", the second 7 days as "week 2", etc, use this formula:
dateDiff(truncDate('MM', mydate), mydate, 'WK') + 1
I do a date-diff (in weeks) between first day of month (truncDate) to input-ed date.
Result:

Last Year vs Current Selected Year

I have been creating a dashboard in which I am trying to showing current Selected year vs last year analysis. Please see the below image :
As you see in the above image, 2020 year selected from the slicer and 2020 sales is 4.30M.
Expectation : I want to show the last year difference with Arrow sign means if the current year sales is greater than last year than "Green upper arrow" and if the last year sales greater than current year then "Red down arrow".
Thing I Tried : I have created a DAX, but it not showing any value to me :
Previous Year Sales = CALCULATE(sum(Orders[Sales]),PREVIOUSYEAR(Orders[Order Date]))
Option 2 : I have also tried this one (it show me value but desired results):
same period last year = CALCULATE(Sum(Orders[Sales]),SAMEPERIODLASTYEAR(Orders[Order Date]))
Expected Output ( Current Year vs last year percentage with Arrow sign) :
Sample data link :
http://www.cse.ohio-state.edu/~hwshen/Melbourne/Data/Superstore.xlsx
How can I achieve the above ?
Thanks
The percentage should be straight foward. Keep in mind you need a DATE TABLE or use the automatic datetime created by power bi, for that you need to reference it with ".[Date]"
same period last year = CALCULATE(Sum(Orders[Sales]),SAMEPERIODLASTYEAR(Orders[Order Date].[Date]))
Sales YoY % =
VAR _ly = [same period last year]
RETURN
DIVIDE(_ly-Sum(Orders[Sales]),_ly)
Format you [Sales YoY %] measure as a percentage.
The visual part you can do it multiple ways, cards, HTML, KPIs Visuals or Table/Matrix conditional formating.

Rollover calculations in PowerBI

I am very new to PowerBI and exploring it. I came across a sample data which has a start date and end date, Group Type and a Value. Basically its something like, an exam group has a start date and end date with a score.
I want to do a rollover calculation like when I view the dashboard it needs to provide me value for this month and for future months. The values needs to be added based on the start date and end date. For example, if the start date is 01-01-2020 and end date is 12-07-2020 and the score is 20 for one record and the start date is 02-03-2020 and end date is 31-05-2020 and the score is 09 for another record, the table needs to show something like for May 29, June, July - 20.
For current months the score must be added cumulatively and for the future months it has to show the cumulative score from that month to the remaining months excluding the current month.
Below is my expected output
Source Data is
It would be helpful for me if anyone can guide me what logic I need to use for this?
Source File
You can do this by adding an extra column where you sum all totals from earlier end dates:
rollover =
var lDate = Sheet1[End Date]
var place = Sheet1[Placement]
var total = CALCULATE(sum(Sheet1[Total]),filter(Sheet1, place = Sheet1[Placement] && lDate >= Sheet1[End Date]))
return total
result:
This you can bring into your visual..

Power BI How to find the end date of a month Q

I have the start of every month in a table. What I am wanting to know is, how do you find the last day of the month if you have the starting month?
In other languages I would take the start date of a month, add a month and then subtract 1 day. How do you do this in Power BI?
In my example below, you will see the starting month in the Month field.
You can use EOMONTH function to calculate that.
=EOMONTH([Month], 0)

Retrieving parameter from relative date Slicer Power BI

I have a calculation that is being done on a weekly basis (x+y/x*40) . 40 is the number of hrs worked per week and x is the num of weeks selected.
I have a relative date slicer using which the user will select the number of weeks. The catch is that there are two kinds of relative weeks - one from the present day and one is the entire week.
If the user selects 2 weeks it will be (x+y/2*40).
I have a calculation called
Tot Weeks = DATEDIFF(MIN( DimDate[Calendar Date] ), MAX( DimDate[Calendar Date]), week )
However, this doesn't work well if I select 'Weeks(Calendar Date)' in the drop down. For the value to be right, I created
Num weeks = DISTINCTCOUNT(DimDate[CalendarWeekOfYear])
Num weeks works fine only when 'Weeks(Calendar Date)' is selected. I am trying to retrieve the parameter passed to the slicer to get my values right.(whether the user selects 'Weeks(Calendar Date)' or 'Weeks' in the slicer).Is there a way to retrieve this or
Is there a way to combine these two?
If you have a slicer like with Weeks (Calendar Date) and Weeks as the two options in the column Slicer[WeekType], then you can pass your selection into a measure like this:
Weeks =
VAR WeekType = SELECTEDVALUE(Slicer[WeekType], <default>)
RETURN IF(WeekType = "Weeks", [Tot Weeks], [Num weeks])