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

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/

Related

How to filter YTD to last complete month in Power BI

I am creating a report with buttons that use a slicer to show the last 3 calendar months, the default view, and YTD. The first two are all set and will continue to work fine, however i am having trouble with the YTD filter because i need it to exclude the current month (some of the key metrics for this slicer are only accurate monthly, even thought the data is updated Daily). Any idea how to accomplish this without me having to manually change it every month? An example of it working today would show me 2020 through August, since September is not complete. September would be included in the filter starting October first. I am thankful for your help/insights!
I typically build a calculated column on my date table called something like "Date in Range", that looks something like the below. You could also apply this to a date in a normal table if you are not using a date dimension.
Date in Range = IF ('MyTable'[Date] <
DATEADD(TODAY(), -1 * DAY(TODAY()), day),
1,
0)
This compares the date in the table row with TODAY(), e.g. 14 Sep 2020, minus the day of the month of today (14), effectively getting you back to the start of the current month. This will then return 1 for dates before the end of last month or 0. Filter on 1 or 0 to get your result (or use something more meaningful in place of the 1 or 0).

Power Bi DAX: Total for the month

I need to make a total for each of the months based on the working days.
So far i have working days set as 1 if the day in the month is during Monday - Friday and 0 for Saturday and Sunday. I now need to total up the 1's and make it so that it is a single value for the month.
E.g Going the weekdays is 1 and weekend is 0, January will have 22 days on each row on the table in the data mode - January 1 = 232 January 2 = 22 etc. so i can use it to divide against my target which is set to the 1st of every month.
How can i do this??
try this. i haven't tested it.
=GROUPBY( FILTER(table, table[Working_Day]=1) , table[Month], ["new col", SUMX( CURRENTGROUP(), table[Month_Order])])
filter gives working day 1's data as table,then group by performed by month, then month order has been added and returns table.

Previous Week Measure - Week 52 2019 and Week 1 2020

I currently use the below measure to retrieve a value for the previous week, however as it's now week 1 of a new year, the field is returning "blank" as it cannot find any previous weeks.
How would I adapt my measure to include something along the lines of... if week 1 use the last week of the previous year, if not use the previous week.
VAR CURRENT_WEEK = WEEKNUM(TODAY()) return
CALCULATE(AVERAGE(DATA_TABLE[VALUE]),
FILTER (DATA_TABLE, WEEKNUM(DATA_TABLE[DATE]) = CURRENT_WEEK -1))
Thanks in advance for your help
I would suggest using a start date and calculating the weeknum from that date. For example, if you choose 1 Jan 2018 as start date, then 1-7 Jan 2018 would be week 1 and first week in 2020 would be week 105. This would solve your issue.
The method you are using becomes really hard to handle if your data has multiple years. Weeknum 1 would denote the 1st week of 2020, 2019 and all the other years. This will mess up the calculation. The above method will make sure you have a unique number for a week.

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

DAX Previous Month to date total is giving entire previous month's entire total

I am using DAX in Power BI to calculate Previous Month sales total to date to create a KPI visual. i.e. if today is 7th Dec then I want to get sales total from 1st Nov to 7th Nov and compare with current month to date.
CurrentMTD = TOTALMTD(SUM(SALES_VOUCHERS[SaleValue]),DatesTable[Date])
This works fine.
However Previous Month YTD gives me total for entire month of November. I have tried the following so far
PMYTD = totalmtd(sum(SALES_VOUCHERS[SaleValue]),dateadd(DATESMTD(DatesTable[Date]),-1,month))
and
PMYTD = CALCULATE(sum(SALES_VOUCHERS[SaleValue]),
DATESBETWEEN(DatesTable[Date],
FIRSTDATE(PREVIOUSMONTH(DatesTable[Date])),
LASTDATE(DATEADD(DatesTable[Date],-1,MONTH))))
Both return the same answer which is total for the entire previous month .
If I simply hardcode the start and end date in datesbetween version above, then I do get the desired result. But that is not the solution.
I have linked the fact table (Sales_VOUCHERS) to a DatesTable and as of now there are no other visuals on the report page.
Kindly assist what I am missing out on and how I can get Previous Month year to date total
If you're aggregating at the month level (i.e. you're looking at December 2016 vs. November 2016), then the measure you have above will show you the entire month of December compared to the entire month of November (and since December is a partial month and November isn't, it causes the mismatch you see).
If you filter to the current date (e.g. 7th Dec), then both your MTD and Prior Month MTD measures will only show you through the 7th of their corresponding months.
Assuming you don't want to filter to the day level (not unreasonable), you could enhance your formula to filter out future dates. For example:
PMYTD = totalmtd(
sum(SALES_VOUCHERS[SaleValue]),
dateadd(
FILTER(
DATESMTD(DatesTable[Date]),
DatesTable[Date]<TODAY()
),
-1,
month
)
)
This says, if the date is after today, don't pass it into the TOTALMTD calculation (so it will only calculate the first 7 days of the month, for example, if today is Dec 8th - even if you're looking at full months on your report).
Side note: you can also write your previous month measure to re-use your MTD measure rather than redefining it. In this way, if you ever change the MTD calculation, the previous MTD calculation automatically updates.
PMYTD = CALCULATE(
[CurrentMTD],
DATEADD(
FILTER(
DatesTable[Date],
DatesTable[Date]<TODAY()
),
-1,
MONTH
)
)
Useful Resources:
https://www.powerpivotpro.com/2016/01/year-to-date-in-previousprior-year/ (article that covers this problem and a variety of solutions)
https://community.powerbi.com/t5/Desktop/Compare-MTD-with-previous-period/td-p/24656 (forum discussion about the same problem)
http://community.powerbi.com/t5/Desktop/Time-Intelligence-TOTALMTD-vs-DATESMTD-vs-DATEADD/td-p/10088 (forum discussion about DATESMTD vs TOTALMTD)