PowerBI calculate overflow backlog in measure - powerbi

My data is as follows:
Factory ID
Week
CAPACITY
Request
1
21
1000
500
1
22
1000
1200
1
23
1500
1600
1
24
1500
1100
2
21
1000
500
2
22
2000
1900
2
23
2000
1000
2
24
2000
2500
3
21
1000
200
3
22
1000
900
3
23
1000
1300
3
24
1000
800
I want to calculate backlog in a measure or any other way to have backlog be dynamic based on the factories I select. Backlog is calculated as follows:
Backlog = Capacity - (Request + Previous week backlog); where we have backlog when requests + pre. week backlog exceeds capacity or else it is 0. I cannot move capacity from future weeks, so the backlog would always accumulate going forward
Eg. If I select Factory 1, my backlog should look as follows:
Factory Selected: 1
Week
Backlog
21
0
22
-200
23
-300
24
-100
Factory Selected: 1,2
Week
Backlog
21
0
22
-100
23
0
24
-400
Factory Selected: 1,3
Week
Backlog
21
0
22
-100
23
-500
24
0
I have been trying to find a solution since the last 2 days. Let me know if you need any additional details. Any help will be greatly appreciated.

In DAX there are no loops and no recursion, this means that we must write some very ugly loop unrolling dax code. This is a minimal implementation of a working measure, but to make it working in the general case the BacklogWeekNN variables must be added until reaching the maximum possible week depth in the model.
Backlog =
VAR MinWeek = CALCULATE(MIN( T[Week] ), REMOVEFILTERS( T ) )
VAR MaxWeek = MAX( T[Week] )
VAR TAggregated =
ADDCOLUMNS(
CALCULATETABLE( VALUES( T[Week] ), T[Week] <= MaxWeek, REMOVEFILTERS( T ) ),
"#Capacity", CALCULATE( SUM( T[CAPACITY] ), ALLEXCEPT( T, T[Week], T[Factory ID] ) ),
"#Request", CALCULATE( SUM( T[Request] ), ALLEXCEPT( T, T[Week], T[Factory ID] ) )
)
VAR BacklogWeek00 = SUMX( FILTER( TAggregated, T[Week] = MinWeek ), [#Capacity] - [#Request] ) + 0
VAR BacklogWeek01 = SUMX( FILTER( TAggregated, T[Week] = MinWeek + 1), [#Capacity] - [#Request] ) + IF(BacklogWeek00 > 0, 0, BacklogWeek00)
VAR BacklogWeek02 = SUMX( FILTER( TAggregated, T[Week] = MinWeek + 2), [#Capacity] - [#Request] ) + IF(BacklogWeek01 > 0, 0, BacklogWeek01)
VAR BacklogWeek03 = SUMX( FILTER( TAggregated, T[Week] = MinWeek + 3), [#Capacity] - [#Request] ) + IF(BacklogWeek02 > 0, 0, BacklogWeek02)
VAR Result = IF(BacklogWeek03 > 0, 0, BacklogWeek03)
RETURN Result
This way we can obtain the desired resulting matrix using a slicer to select the factories (I'm afraid there are a few errors in the expected result samples int the question)
Edit: I used 'FILTER( TAggregated, T[Week] = MinWeek )' instead of the equivalent CALCULATE/CALCULATTABLE DAX code to avoid context transition happening and because the weeks table has very few rows to be iterated (I can imagine a maximum of a few tens or at maximum hundreds of rows if keeping a few years history)

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] )
)

Powerbi Top N and others for Pie Chart

I have the following data:
Country Sales
1 20
1 30
1 10
2 25
2 80
3 200
3 4
4 20
5 30
I want to have top 2 country sales summed up and the rest
Country Sales
2 105
3 204
Others 110
I want to display the summed up data in a pie chart.
Rank Sales =
RANKX(ALL(Table[Country]),CALCULATE(sum(Table[Sales])))
What should I do ahead? The if condition is giving an error:
Rank Display =
IF('DAX Measures'[Rank Sales]>2,"Other",'Table'[country])
Please create a new table, and paste this code:
CategorizationByRank =
VAR Summary_01 =
ADDCOLUMNS (
VALUES ( 'Table'[Country] ),
"Total", CALCULATE ( SUM ( 'Table'[Sales] ) )
)
VAR Summary_02 =
ADDCOLUMNS ( Summary_01, "Ranking", RANKX ( Summary_01, [Total] ) )
VAR TablesRankLess_And_Equal_To_2 =
FILTER ( Summary_02, [Ranking] <= 2 )
VAR TablesRankGreater_Than_2 =
ROW (
"Country", "Others",
"Total", SUMX ( FILTER ( Summary_02, [Ranking] > 2 ), [Total] ),
"Ranking", 3
)
VAR UnionAllRecords =
UNION ( TablesRankGreater_Than_2, TablesRankLess_And_Equal_To_2 )
RETURN
UnionAllRecords
And It produces this result:
Then you can put the fields directly into a pie chart, see the picture:

Power BI Dax to Calculate the average in 3 periods

I have two below tables in Dim_Date the Period doesn't correspond to exact months and first I calculated a measure to Divide the count of records per region by NumofWeeks.
The Num of the week is always the same in the same period.
Table1
Region
Subregion
DataID
Amount
North Central
Missouri
12042022
123000
North Central
Minnesota
12052022
170000
North Central
North Dakota
10042022
234000
Northeast
New York
08042022
500000
Northeast
New Jersey
12052022
578000
Southwest
Nevada
12032022
679000
Southwest
Arizona
10032022
654000
DimDate
DataID
Period
NumofWeeks
12052022
2022_05
5
10042022
2022_04
4
12042022
2022_04
4
12052022
2022_05
5
08042022
2022_04
4
12032022
2022_03
4
10032022
2022_03
4
Divide per region week =
VAR TotalCountPerRegion = COUNT(Table1[Region])
VAR tblNumOfWeeksInPeriod=
SUMMARIZE(
DimDate
,DimDate[Period]
,DimDate[NumofWeeks]
)
VAR SuMOfWeeksInPeriod = SUMX(tblNumOfWeeksInPeriod,DimDate[NumofWeeks])
RETURN
MROUND(
DIVIDE(
TotalCountPerRegion
,SuMOfWeeksInPeriod
)
,1
)
With the formula, I got the following table
Period
Divide per region week
2021_04
17
2021_05
15
2021_06
9
2021_07
16
2021_08
20
2021_09
21
2021_10
17
2021_11
19
2021_12
20
2022_01
27
I want a new formula to calculate the average of the period with 2 following periods
Example
Average 2021_04 = 2021_04 + 2021_05 + 2021_06 = (17+15+9)/3
Average 2021_05 = 2021_05 + 2021_06 + 2021_07 = (15+9+16)/3
and so on.
thank you in advance.
I accepted this solution thank you very much ...
[M 3-Period Rolling Avg] =
// Set the number of periods to calculate
// the average over.
var PeriodCount = 3
// First, get the last visible period.
var LastPeriodSeqno = MAX( Dim_Date[PeriodSeqno] )
// Then, get the periods over which to calc.
var PeriodsToAverageOver =
CALCULATETABLE(
DISTINCT( Dim_Date[PeriodSeqno] ),
// Here's the place where you use the fact that
// all the periods are consecutively numbered.
// In fact, the counting does not have to start
// at 1 but it has to increment by 1.
Dim_Date[PeriodSeqno] <= LastPeriodSeqno,
Dim_Date[PeriodSeqno] > LastPeriodSeqno - PeriodCount,
REMOVEFILTERS( Dim_Date )
)
// We need to make sure that there are indeed
// PeriodCount periods in the set. Otherwise,
// the average will not be correct. This could happen
// if we were too close to the beginning of the calendar.
var ShouldCalculate =
COUNTROWS( PeriodsToAverageOver ) = PeriodCount
var Result =
if( ShouldCalculate,
CALCULATE(
AVERAGEX(
PeriodsToAverageOver,
[M]
),
REMOVEFILTERS( Dim_Date )
)
)
return
Result
My calendar is different for example the periods are like a financial calendar the period
2021_04 - Starts on 05/04/2021 and finishes on 02/05/2021
2021_05 - Starts on 03/05/2021 and finishes on 30/05/2021
2021_06 - Starts on 31/05/2021 and finishes on 04/07/2021
I want to calculate
Divide per region week =
VAR TotalCountPerRegion = COUNT(Table1[Region])
VAR tblNumOfWeeksInPeriod=
SUMMARIZE(
DimDate
,DimDate[Period]
,DimDate[NumofWeeks]
)
VAR SuMOfWeeksInPeriod = SUMX(tblNumOfWeeksInPeriod,DimDate[NumofWeeks])
RETURN
MROUND(
DIVIDE(
TotalCountPerRegion
,SuMOfWeeksInPeriod
)
,1
)
Rolling 3 periods if we start in 2021_04 I have
2021_06 = Average(2021_06+2021_05+2021_04)
2021_07 = Average(2021_07+2021_06+2021_05)
2021_08 = Average(2021_08+2021_07+2021_06)
and so on
I need to incorporate the formula above because the average is the sum of the formula for 3 periods.
Thanks in advance

how to get running sum/cumulative sum of a measure in power bi using DAX (direct query)

i have simple dataset like below where the delta is a measure (not a column) which is the diff of demand and supply.I wanted to calculated the running sum of measure "delta" as shown below.running sum needs to be at material-location-week level.
dax i tried:
Cum =
var mat = MAX('table'[Material])
var pla = MAX('table'[Plant])
var pw =MAX('table'[PWk])
return
CALCULATE(
table[delta],
FILTER(
ALL(table),
'table'[Material]= mat && 'table'[Plant] <= pla && 'table'[PWk]<=pw
)
))
<>
material location week demand supply delta(demand-supply) running_sum??
123 1000 wk1 100 40 60 60
123 1000 wk2 40 30 10 70
123 2000 wk1 30 20 10 10
123 2000 wk2 40 15 25 35
please help. I am stuck with this and dont know`enter code here` where i am going wrong.
Perhaps:
running_sum =
VAR Mat =
MAX ( 'Table'[material] )
VAR Loc =
MAX ( 'Table'[location] )
VAR Wk =
MAX ( 'Table'[week] )
RETURN
CALCULATE (
[delta],
FILTER (
ALL ( 'Table' ),
'Table'[material] = Mat
&& 'Table'[location] = Loc
&& 0 + SUBSTITUTE ( 'Table'[week], "wk", "" )
<= 0 + SUBSTITUTE ( Wk, "wk", "" )
)
)

Calculate Weighted Average and Weighted Standard Deviation in DAX

This is something I have never attempted before
I want to calculate the weighted standard deviation and the weighted average for the dataset containing records for actual values measured against set values
The calculation is to be done using a DAX query in PowerBI
Set Value 1
Actual Value 1
Set Value 2
Actual Value 2
10
8
101
102
10
11
101
104
10
12
101
97
10
7
101
99
10
13
101
97
10
13
101
100
10
9
101
98
10
10
101
100
10
8
101
102
10
14
101
98
10
8
101
98
10
13
101
96
10
13
101
103
10
14
101
102
10
7
202
205
20
18
202
198
20
18
202
197
20
19
202
203
20
19
202
202
20
19
202
201
20
22
202
202
20
18
202
200
20
17
202
195
20
23
202
198
Edit 1:
Please use the data above.
Also, please note that although, set points are what we intend to use as weights but its the count of a particular set point. for eg: if the setpoint1 10 is repeating 15 times and set point1 20 is repeating 9 times then wieght to be used as 15 & 9 respectively
Weighted Average and Standard deviation can be implemented in DAX according to their mathematical definition.
Assuming we have a table with the columns Weight and Value the formula for the Weighted Average is
WAvg =
VAR Num = SUMX( Samples, Samples[Weight] * Samples[Value] )
VAR Den = SUM( Samples[Weight] )
RETURN DIVIDE( Num, Den )
and the formula for the Weighted Standard Deviation is
WStdDev =
VAR WAvg = [WAvg]
VAR Num = SUMX( Samples, Samples[Weight] * (Samples[Value] - Wavg)^2 )
VAR Den = SUM( Samples[Weight] )
VAR WVar = DIVIDE( Num, Den )
RETURN SQRT(WVar)
Edit:
if I understand your new request, the Weight is the number of rows with the same Set Value, that is to be used for each of the Actual Value. Then, since there are two pairs of columns, I assume that the requirement is to have a set of measures per each couple of columns.
The formula requires to add a count of the number of rows per each group of Set Value, to be used as weight. I imported the sample table as table "V"
Weighted average for Set Value 1 and Actual Value 1
WAvg1 =
VAR Num =
SUMX(
ALL( V ),
CALCULATE( COUNTROWS( V ), ALLEXCEPT( V, V[Set Value 1] ) ) * V[Actual Value 1]
)
VAR Den =
SUMX(
ALL( V ),
CALCULATE( COUNTROWS( V ), ALLEXCEPT( V, V[Set Value 1] ) )
)
RETURN
DIVIDE( Num, Den )
Weighted average for Set Value 2 and Actual Value 2
WAvg2 =
VAR Num =
SUMX(
ALL( V ),
CALCULATE( COUNTROWS( V ), ALLEXCEPT( V, V[Set Value 2] ) ) * V[Actual Value 2]
)
VAR Den =
SUMX(
ALL( V ),
CALCULATE( COUNTROWS( V ), ALLEXCEPT( V, V[Set Value 2] ) )
)
RETURN
DIVIDE( Num, Den )
Weighted standard deviation for Set Value 1 and Actual Value 1
WStdDev1 =
VAR Num =
SUMX(
ALL( V ),
VAR WAvg = [WAvg1]
RETURN
CALCULATE( COUNTROWS( V ), ALLEXCEPT( V, V[Set Value 1] ) ) * ( V[Actual Value 1] - WAvg ) ^ 2
)
VAR Den =
SUMX(
ALL( V ),
CALCULATE( COUNTROWS( V ), ALLEXCEPT( V, V[Set Value 1] ) )
)
VAR WVariance =
DIVIDE( Num, Den )
RETURN
SQRT( WVariance )
Weighted standard deviation for Set Value 2 and Actual Value 2
WStdDev2 =
VAR Num =
SUMX(
ALL( V ),
VAR WAvg = [WAvg2]
RETURN
CALCULATE( COUNTROWS( V ), ALLEXCEPT( V, V[Set Value 2] ) ) * ( V[Actual Value 2] - WAvg ) ^ 2
)
VAR Den =
SUMX(
ALL( V ),
CALCULATE( COUNTROWS( V ), ALLEXCEPT( V, V[Set Value 2] ) )
)
VAR WVariance =
DIVIDE( Num, Den )
RETURN
SQRT( WVariance )
Applying these formulas to the sample table we get these results