Calculating cumulative sum - powerbi

I want to display bar graph that has the cumulative sum of Panels for a week. Then after the week it refreshes starts again for the next week. We are using Date completed column to figure out the week.
For eg.
week 1:
1st nov - 10 panels are produced
2nd nov - 10 panels are produced (the bar graph would have 20 panels for 2nd nov)
3rd nov - 10 panels are produced (the bar graph would have 30 panels for 3rd nov)
I was able to calculate the cumulative sum using the below formula
Cumulative_Panels =
SUMX (
FILTER (
Query1,
WEEKNUM ( Query1[TDATE] )
= WEEKNUM ( EARLIER ( Query1[TDATE]) )
&& Query1[TDATE] <= EARLIER ( Query1[TDATE] )
),
Query1[Units]
)
This is what i get
Everything is good but the only problem is that the week starts from sunday. As you can see, i want 11/05/2017 to be part of week1 and the new week should start from monday 11/06/2017

WEEKNUM has a parameter where you can set which day the week begins.
If you want the week to start from Monday, you can change your WEEKNUM function to:
WEEKNUM(Query1[TDATE], 2) = WEEKNUM(EARLIER(Query1[TDATE]), 2)
where 2 is the parameter I'm talking about.
Details reference here.
WEEKNUM(<date>, <return_type>)
return_type: A number that determines the return value: use 1 when the week begins on Sunday; use 2 when the week begins on Monday. The default is 1.

Related

Why do i get the avrage of the subcategory and not the avrage of category per year?

When i drag in a tabel
and just have
-YEAR
-Kategory
-Summa
I will get the medium value of 895,50 (815 + 976 / 2) for february
I don't want it to separeate the Subcategory
I want it to show
1791
when i just mark february
and if i use February and March
It sould say
2022 Inköp 2435,5
Becouse that the Medium of February and Marsh on the category
It's difficult to understand what you are asking (likely the reason for your downvotes). I think you are after a monthly average measure. Since your sample data is extremely limited I assume that each month will have exactly one date. Ideally this type of calculation should be handled by using a calendar table as a basis for month values, but for your sample data, try this:
Monthly average =
AVERAGEX (
VALUES ( 'Table'[Datum] ) ,
CALCULATE ( SUM ( 'Table'[Summa] ) )
)

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 )

Calculate Number of Working Days based on a Month

Using Power Bi desktop,
I need to create a query where the result will be the current month's working days.
For example July 2021 has 22 working days and so on.
My goal for achieving this will be to be to average the number of lines processed divided into the number of working days for the current month.
Will this be possible?
You can always get the job done only by creating just a single measure and nothing else like following
_count =
COUNTX (
FILTER (
VALUES('fact'[_factDate]),
WEEKDAY ( 'fact'[_factDate], 1 ) <> 1
&& WEEKDAY ( 'fact'[_factDate], 1 ) <> 7
),
'fact'[_factDate]
)
The minimum dependency of the calculations are having a calendar table like this
Calendar_Date
Calendar_Year
Calendar_Month
1/1/2000
2000
1
first create a calculated column in you calendar table (Dates for my case) with this below code-
Column = if(
FORMAT(Dates[date],"dddd") = "Saturday"
|| FORMAT(Dates[date],"dddd") = "Sunday",
0,
1
)
now create a measure as below-
weekday = sum(Dates[Column])
now, create visual with month and new measure.

Power Bi DAX: Relative Date Filtering

I have a quick question, is there a way to filter data from set days between 2 months?
E.g filter from 16-15 of Jan-Feb.
Is this possible?
For example i have used a measure to calculate days between dates
Last Assessment 16-15
= CALCULATE(SUM('Table1'[Duration1]),'Table1'[Start],
DATESBETWEEN('Calendar'[Date], [Assessment Date], [Assessment one month]))
Assessment Date = if(DAY(TODAY())<16,DATE(YEAR(TODAY()),MONTH(TODAY())-1,15),
DATE(YEAR(TODAY()),MONTH(TODAY()),15))
Assessment one month = EDATE([Assessment Date],-1)+1
Assessment 6 = EDATE([Assessment Date],-6)+1
Assessment 12 = EDATE([Assessment Date],-12)+1
The last assessment does show from the 16th of 2 months ago to last months 15th e.g Dec 16th - Jan 15th.
But i need to show from last 6 months and the last 1 year.
How can i work this out so i can show the Last 6 months and 1 year.
So far i have had to use a date filter to manually select the dates which i want to stop and have it be automatic.
If it is just the last 6 months or the last year you could make a custom column in the query editor (this would be the easiest way then). Like a filter flag:
'Includes the current month
Last 6 Months Flag = Date.IsInPreviousNMonths([YourDate], 6) or Date.IsInCurrentMonth([YourDate])
'Without the current month
Last 6 Months Flag = Date.IsInPreviousNMonths([YourDate], 6)
The same for last year:
Last Year Flag = Date.IsInPreviousYear([YourDate])
Drag and drop these custom columns as filter on your report and you are done.
consider following measure for Latest assessment (basically what you had before with EDATE for safe Dec/Jan handling and with variables for better performance)
Latest Assessment Date =
VAR __today = TODAY()
RETURN
IF (
DAY ( __today ) < 16,
DATE ( YEAR ( __today ), MONTH(EDATE(__today, - 1)), 15 ),
DATE ( YEAR ( __today ), MONTH ( __today ), 15 )
)
then you have this measure for handling 1/6/12 scenarios, you just replace 12 with whatever number of months you need
Measure 12 =
VAR __nrOfMonthsBack = 12
VAR __lastAssesment = [Latest Assessment Date]
RETURN
CALCULATE (
SUM ( Table1[Duration] ),
ALL('Calendar'), --this will reset any date filters coming from slicers
DATESBETWEEN (
'Calendar'[Date],
EDATE ( __lastAssesment, - __nrOfMonthsBack ) + 1,
__lastAssesment
)
)
EDIT: added ALL('Calendar') to disable filters coming from slicers

What is the opposite of EOMONTH() in Power BI (DAX)

What is the opposite of EOMONTH()? I am trying to find the beginning date of 12 months ago. Would it be EOMONTH(Current Date,12)-1?
Any direction would be great. TIA
To find the beginning of a month, go to the end of the month before and add a date to it (not sure if I understand the question)
Start of month one year ago = EOMONTH(TODAY(),-13)+1
In general, the opposit function is STARTOFMONTH:
=STARTOFMONTH('Date'[Current Date])
If you want to get the first day of the month 12 months ago:
= STARTOFMONTH ( DATEADD ( 'Date'[Current Date], -12, MONTH ) )
This measure will return the first date of the same month a year ago (i.e., for 2018-09-22, it will return 2017-09-01). Current month is included, so you will get a total of 13 months. If you need only 12 months total:
= STARTOFMONTH ( DATEADD ( 'Date'[Current Date], -11, MONTH ) )
If you are trying to compute something over a rolling 12 months period, this article should help:
12-month Rolling Average in DAX
In DAX, you could also use
= DATE(YEAR(Dates[Date])-1, MONTH(Dates[Date]), 1)
This backs up one year, keeps the month, and returns the first day of that month.
There is no straight opposite function for EOMONTH() in DAX. But you can still use EOMONTH() to get the first date of a month.
Last date of a month
= EOMONTH(Leaves[LeaveStart]
First date of a month
= EOMONTH(Leaves[LeaveStart],-1)+1
You can also just use 1 in place of date to get first day of a month, like:
= DATE(YEAR(Leaves[LeaveStart]), MONTH(Leaves[LeaveStart]), 1)
Please note that STARTOFMONTH() is opposite function for ENDOFMONTH() not for EOMONTH().