Power Bi DAX: Total for the month - powerbi

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.

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/

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

DAX Looping Variable

I'm building a Power BI dashboard that includes actual cost vs budget. Summarizing the cost is not a problem. The budget amount is entered in a table as a weekly amount, for example $1,000. We use a custom accounting calendar that dictates the number of weeks per month. The second month of each quarter is 5 weeks while all other months are 4 weeks. Here's what it looks like:
January is 4 weeks
February is 5 weeks
March is 4 weeks
April is 4 weeks
May is 5 weeks
June is 4 weeks
July is 4 weeks
August is 5 weeks
September is 4 weeks
October is 4 weeks
November is 5 weeks
December is 4 weeks
I need a looping variable in DAX that will simply count the number of weeks based on the months chosen with the month slicer. Then I can multiply the number of weeks by the weekly budget amount.
Here's a practical example:
If I choose Jan, Feb, Mar, Apr then the number of weeks should be 17 weeks. And the budget amount should be $17,000.
I figured this out in Crystal Reports. Here's the formula I wrote:
local datetimevar startdate:= {?BegMonth};
local numbervar numofWeeks;
While startdate <= {?EndMonth}
Do(
if month(startdate) in [2,5,8,11] then
numofWeeks := numofWeeks + 5
else
numofWeeks := numofWeeks + 4;
startdate := DATEADD("m",1,startdate);
);
numofWeeks
I've tried several approaches with DAX but I got nothing to work.
DAX doesn't exactly do looping. It does aggregations on filtered columns.
If you don't have a Month column, then create a calculated column.
TableName[Month] = MONTH ( TableName[Date] )
Once you have that, adding up weeks can be done with an iterator function.
Weeks =
SUMX (
VALUES ( TableName[Month] ),
IF ( TableName[Month] IN { 2, 5, 8, 11 }, 5, 4 )
)
The VALUES function is a list of unique values in the filter context, so if you selected Jan - Apr, then it would be {1,2,3,4}. Then SUMX iterates ("loops") over those four values and add 5 or 4 depending on the condition specified in the IF function.

Grand Total not working for sales per day last year function

I am trying to do a sales by day comparison where I compare the sales this year with the sales of the same day last year as a date of the week.
So I would be comparing Monday March 25 2019 with Monday march 24 2018 etc
Here is the formula I’m using for last year’s sale
Amount per Day LY = CALCULATE([Amount TY], FILTER(all(Dates), Dates[Date] = MAX(Dates[Date])-364))
However, my total isn’t working right for my sales last year. It will just be the total for 1 day (and that day seems to change as my date range increases)
Because you are having a one to one, you can do this with LookupValue:
SalesLY = LOOKUPVALUE(Sheet1[Sales];Sheet1[Date];DATEADD(Sheet1[Date];-364;DAY))
However if you have more rows for the same date, this will not hold, in this case you need to sum all the dates together,
SalesLY = CALCULATE(SUM(Sheet1[Sales]);FILTER(Sheet1; DATEADD(Sheet1[Date];364;DAY)= EARLIER(Sheet1[Date])))
Using DateAdd fixed my problem
Amt per Day LY = CALCULATE([Amount TY], DATEADD(Dates[Date], -364,DAY))

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