Calculate max value from Mon-Sun for every week - powerbi

I have a table where I have to find the maximum weekly quantity for the data based on the date column in the past few years. In this example, say 06/07 is Monday and 06/12 is Sunday. Then in that week, the quantity 12 is the MAX value.
Quantity
Date
Time
1
2022-06-07
04:39:57.090
2
2022-06-07
04:39:58.850
3
2022-06-08
04:37:35.900
4
2022-06-08
04:37:37.247
5
2022-06-09
04:37:56.833
6
2022-06-09
04:37:58.190
7
2022-06-10
04:37:34.757
8
2022-06-10
04:37:36.103
9
2022-06-11
04:38:18.733
10
2022-06-11
04:38:20.100
11
2022-06-12
04:38:30.377
12
2022-06-12
04:38:31.833
13
2022-06-13
04:38:30.377
14
2022-06-14
04:38:31.833
I am trying to write a DAX script for this, but failing to do it because I am filtering on MAX date instead of value. Any help on how to modify this?
CALCULATE(
MAX( 'stock_xml'[Quantity] ),
FILTER( 'stock_xml',
'stock_xml'[Date] = MAX( 'stock_xml'[Date] )
)
)
RETURN
Stock

Can you please try this below Measure script-
weekly_max =
CALCULATE(
MAX('your_table_name'[Quantity]),
FILTER(
ALL('your_table_name'),
WEEKNUM('your_table_name'[Date],1) = WEEKNUM(MAX('your_table_name'[Date]),1)
)
)
Sample output-

Related

Need a DAX expression to calculate Rate of Success

I have a table called Payment_Methods in PBI with the following columns:
timestamp, weekDay, Hour, eventType, SuccessEvents, FailedEvents, addPymntCardInstance.
weekDay is ['Sunday', 'Monday', ..., 'Friday', 'Saturday']
Hour is [0, 1, 2, 3, ..., 21, 22, 23]
I want to create a PymntSuccessRate using the DAX expression below, but the results are always the same all the way down across the column PymntSuccessRate. How can I fix this. Appreciate if you can give me some clue. Thanks.
PymntSuccessRate =
DIVIDE(
SUMX(
FILTER(
GROUPBY(
Payment_Methods,
Payment_Methods[timestamp], Payment_Methods[weekDay], Payment_Methods[Hour]
),
Payment_Methods[eventType] = "Success"
),
SUM( Payment_Methods[addPymntCardInstance] )
),
SUMX(
GROUPBY(
Payment_Methods,
Payment_Methods[timestamp], Payment_Methods[weekDay], Payment_Methods[Hour]
),
SUM( Payment_Methods[addPymntCardInstance] )
)
)
timestamp
weekDay
Hour
SuccessEvents
FailedEvents
SuccessRate
2023-01-20
Friday
20
2
8
0.20
2023-01-20
Friday
19
121
111
0.52
2023-01-17
Tuesday
6
31
8
0.79
2023-01-17
Tuesday
5
19
14
0.57
based on your sample you don't need to use SUMX and GROUPBY (visual will do the grouping), this should be enough (used as a measure, NOT a calculated column):
PymntSuccessRate =
DIVIDE (
CALCULATE (
SUM ( Payment_Methods[addPymntCardInstance] ),
Payment_Methods[eventType] = "Success"
),
SUM ( Payment_Methods[addPymntCardInstance] )
)

Calculate Number of Working Days based on a Month and Year Column - DAX

I have a column like below for which I would want to extract the number of working days (excluding just weekends- Saturday and Sunday, holidays needs not be addressed).
As of now I just want:
Required_Column = Total No of Days in that month - No of weekends in that month
Month_Year
01-2018
02-2018
03-2018
...
...
01-2019
02-2019
I am a newbie in Power query and DAX , i tried looking up various methods using DAX however, could not find any relevant lead.
Expected Output:
Month_Year Required_Column
01-2018 23 (31-8)
02-2018 20 (28-8)
03-2018 22 (31-9)
... ...
... ...
01-2019 23 (31-8)
02-2019 20 (28-8)
Appreciate your help on this.
Although a Calendar table based approach is recommended as in the comment by RADO and Strawberryshrub, it is also possible to do this with DAX calculated column.
In the example below, I'm assuming MonthYear column contains the first day of each month.
WorkingDays =
VAR Year = YEAR( [MonthYear] )
VAR Month = MONTH( [MonthYear] )
VAR DatesInMonth = GENERATESERIES( [MonthYear], DATE( Year, Month + 1, 1 ) - 1, 1 )
RETURN SUMX(
DatesInMonth,
IF( WEEKDAY( [Value] ) IN { 1, 7 }, 0, 1 )
)

Calculated Column with Current Row Values and Previous Dates in Power BI (DAX)

I'm trying to get a calculated column (not a measure) that gets the sum of a column based on the values in the current row of that table for dates that are 1 month lagged to the date on the current row. My table has dates that are the 1st day of every month only .. no other days in the month. I'm asking the question about DAX; however, I have no problem implementing in M Language in Power Query (actually would probably prefer) if there is a solution that way as well.
I have been able to get a measure to work using something like this..
CALCULATE(SUM(AMT), DATEADD(DATECOLUMN, -1, MONTH))
But I'd like to be a new column instead.
Assuming the table looks something like this..
A B C D AMT
6 BAC456 5/1/2019 TEST 25
2 EPS123 4/1/2019 TEST 45
2 EPS123 3/1/2019 TEST 65
6 BAC456 4/1/2019 TEST 43
6 BAC456 4/1/2019 TEST 88
7 GRE123 4/1/2019 TEST 90
9 BAC456 4/1/2019 TEST 43
I'd like to have another column in this table where the first row would be:
A B C D AMT NEWCOL
6 BAC456 5/1/2019 TEST 25 131
Second row would be:
A B C D AMT NEWCOL
2 EPS123 4/1/2019 TEST 45 65
etc..
In cases where the month column is the first month in the entire table NEWCOL would be 0
To get 131, I'm assuming that you are requiring a match on both columns A and B.
NewCol =
CALCULATE (
SUM ( Table1[AMT] ),
ALLEXCEPT ( Table1, Table1[A], Table1[B] ),
PREVIOUSMONTH ( Table1[C] )
)
This sums the column AMT keeping the row context of columns A and B and specifies the previous month as a filter on C. Note that this returns a blank for rows that don't have a previous month. If you'd prefer 0 then add + 0 after the last closing parenthesis.
If PREVIOUSMONTH doesn't work, then try this:
NewCol =
CALCULATE (
SUM ( Table1[AMT] ),
ALLEXCEPT ( Table1, Table1[A], Table1[B] ),
Table1[C] = EOMONTH ( EARLIER( Table1[C] ), -2 ) + 1
)
For date = 5/1/2019, EOMONTH ( date, -2 ) returns 3/31/2019. Add one day to get 4/1/2019.
To achieve this easily in a calculated column, you need something like this before writing the final DAX.
Month Num = MONTH(MyTable[C])
Month Diff = DATEDIFF(MyTable[C],MAX(MyTable[C]),MONTH)
And now you have these Month differences and Month numbers defined, you can write a DAX like this -
Amount - New Column =
Var selectedValue_B = MyTable[B]
Var SelectedValue_MonthDiff = MyTable[Month Diff]
Var out1 = CALCULATE(SUM(MyTable[AMT]), FILTER(ALL(MyTable), MyTable[Month Diff] = SelectedValue_MonthDiff+1 && MyTable[B] = selectedValue_B)) + 0
return out1
This makes my table to look something like,
I have used Var(Variables) in my formula to help you understand what is happening inside the formula.
Kindly accept the answer if it solves your problem.

Calculating cumulative values

I am trying to calculate cumulative values. I tried the formulas below, none of them worked.
Cumulative_Forecast = CALCULATE(sum(TEAMS_Forecast_LineItems[ForeCast_Value]),
filter(ALLEXCEPT(TEAMS_Forecast_LineItems,TEAMS_Forecast_LineItems[ForeCast_Year]),
TEAMS_Forecast_LineItems[MonthNumber]<=EARLIER(TEAMS_Forecast_LineItems[MonthNumber])))
Cumulative_Forecast2 = VAR RowDate = TEAMS_Forecast_LineItems[Forecast_Date]
return CALCULATE(sum(TEAMS_Forecast_LineItems[ForeCast_Value]),
FILTER(TEAMS_Forecast_LineItems, TEAMS_Forecast_LineItems[Forecast_Date]
<=RowDate && YEAR ( TEAMS_Forecast_LineItems[Forecast_Date] ) = YEAR ( RowDate )))
Cumulative_Forecast3 = TOTALYTD(sum(TEAMS_Forecast_LineItems[ForeCast_Value]),
'Calendar'[Date])
Cumulative_Forecast4 = CALCULATE(sum(TEAMS_Forecast_LineItems[ForeCast_Value]),
filter(ALL(DimDate[Date]), DimDate[Date] <= Max(DimDate[Date])) )
Here are some examples of records:
ForeCast_Value Month MonthNumber ForeCast_BU ForeCast_Year ForeCast_ID
71100 Sep 9 Business1 2018 10648
71100 Oct 10 Business1 2018 10648
81000 Sep 9 Business1 2018 10649
71200 Sep 9 Business2 2018 10700
80500 Sep 9 Business2 2017 10500
80600 Oct 10 Business2 2017 10500
81100 Sep 9 Business2 2018 10650
I have a line chart; Month on the Axis, BU on the legend and value on values.
When slicer year is all selected
Business 1= 152100 and Business 2 =232800 on September
Business 1= 71100 and Business 2 =80600 on October
When slicer year is 2017 selected
Business 2=80600 on Sep
Business 2=80500 on Oct
I want to create a new line chart for cumulative values. Desired values are:
When slicer year is all selected
Business 1= 152100 and Business 2 =232800 on September
Business 1= 223200 and Business 2 =313400 on October
When slicer year=2017 selected
Business 2=80600 on Sep
Business 2=161100 on Oct
The below DAX formulas can help you to get your expected results:
Cumilative_Forecast =
CALCULATE (
SUM ( TEAMS_Forecast_LineItems[ForeCast_Value] ),
FILTER(ALL(TEAMS_Forecast_LineItems),TEAMS_Forecast_LineItems[ForeCast_BU] = max ( TEAMS_Forecast_LineItems[ForeCast_BU] )) , FILTER(ALL(TEAMS_Forecast_LineItems), TEAMS_Forecast_LineItems[MonthNumber] = max ( TEAMS_Forecast_LineItems[MonthNumber] )),FILTER (
ALL ( TEAMS_Forecast_LineItems ),
TEAMS_Forecast_LineItems[ForeCast_Year] <= max( TEAMS_Forecast_LineItems[ForeCast_Year] ))
)
The above formula gives result of
" When slicer year is all selected
Business 1= 152100 and Business 2 =232800 on September
Business 1= 71100 and Business 2 =80600 on October "
Cumulative_Value =
CALCULATE (
SUM ( TEAMS_Forecast_LineItems[ForeCast_Value] ),
FILTER(ALL(TEAMS_Forecast_LineItems),TEAMS_Forecast_LineItems[ForeCast_BU] = max ( TEAMS_Forecast_LineItems[ForeCast_BU] )) , FILTER (
ALL ( TEAMS_Forecast_LineItems ),
TEAMS_Forecast_LineItems[MonthNumber] <= max( TEAMS_Forecast_LineItems[MonthNumber] ))
)
The above formula gives result of
"I want to create a new line chart for cumulative values. Desired values are:
When slicer year is all selected
Business 1= 152100 and Business 2 =232800 on September
Business 1= 223200 and Business 2 =313400 on October "
Please let me know does this reached your expected result or not.

Power Bi: Get Column Name with Maximum and Minimum other column value

I have a sheet containing employees leave data-
Staff Leave Taken Month
A 19 April
A 3 May
A 3 June
B 2 April
B 1 May
B 0 June
C 0 April
C 0 May
C 1 June
I want to calculate the Employee whose has taken maximum no. of leaves and name of employee whose has taken minimum no. of leaves.
Here Employee with max. leaves is A and with min. is C.
I am having trouble in getting the maximum no. of leaves.
X = MAX( SUMX ( SUMMARIZE ( Table1, Table1[STAFF], Table1[Leaves] ), [Leaves] ))
But it is showing some error.
I tried to group by staff name then also it is not working out.
You can first create a summary table with the following DAX:
Summary = SUMMARIZE(Table1, Table1[Staff], "Leaves", SUM(Table1[Leave Taken]))
Then you can use the following DAX measure to get the Max / Min name:
Max Name =
CALCULATE(
FIRSTNONBLANK('Summary'[Staff], 1),
FILTER(
Summary,
Summary[Leaves] = MAX(Summary[Leaves])
)
)
-
Min Name =
CALCULATE(
FIRSTNONBLANK('Summary'[Staff], 1),
FILTER(
Summary,
Summary[Leaves] = MIN(Summary[Leaves])
)
)