PowerBI measure dynamic average per multiple categories - powerbi

The following measure shows the average per Werks and MATNR but static, for all values in the table.
CALCULATE(
AVERAGEX(
SUMMARIZE( LieferscheineUnique, LieferscheineUnique[WERKS], LieferscheineUnique[MATNR] ),
CALCULATE( AVERAGE(LieferscheineUnique[fci_zu_PA] ) )
),
ALLEXCEPT( LieferscheineUnique, LieferscheineUnique[WERKS], LieferscheineUnique[MATNR] )
)
I have the following table
DATUM
WERKS
ID
MATNR
fci_zu_PA
Average_fci_per_Werks&MATNR
08.04.2021
H006
1
10009
41,7
35,84
12.04.2021
H006
2
10009
43,3
35,84
14.04.2021
H006
3
10009
43,5
35,84
08.04.2021
H100
4
10009
43,3
38,20
22.04.2021
H100
5
10009
43,3
38,20
22.04.2021
H100
6
10010
24,5
35,01
Now I want the average per WERKS and MATNR displayed in each row according to the date filter.
The desired output would look like this:
DATUM
WERKS
ID
MATNR
fci_zu_PA
Average_fci_per_Werks&MATNR
08.04.2021
H006
1
10009
41,7
42,83
12.04.2021
H006
2
10009
43,3
42,83
14.04.2021
H006
3
10009
43,5
42,83
08.04.2021
H100
4
10009
43,7
43,50
22.04.2021
H100
5
10009
43,3
43,50
22.04.2021
H100
6
10010
24,5
24,50
It would be great if someone knows how to achieve this.

Related

Calculating the sum of values in a different table for rows that are between two dates

I have two tables.
A campagin table:
Campaign ID
Start Date
End Date
Daily Target
1
21/12/2020
15/02/2021
5
2
18/10/2020
18/01/2021
3
3
01/07/2020
03/01/2021
8
4
09/01/2021
15/05/2021
1
5
05/08/2020
09/01/2021
2
And a simple Date table:
Date
01/01/2021
02/01/2021
03/01/2021
04/01/2021
05/01/2021
06/01/2021
07/01/2021
08/01/2021
09/01/2021
10/01/2021
11/01/2021
12/01/2021
13/01/2021
What I would like to do is add a calculated column to the Date table that will calculate the sum of all the Daily Targets for campaigns that are between Start Date and End Date. So for 1st January 2021 I want to take the sum of the Daily Targets for Campaign 1, 2, 3 & 5. E.g:
Date
Total Daily Target
01/01/2021
18
02/01/2021
18
03/01/2021
18
04/01/2021
10
05/01/2021
10
06/01/2021
10
07/01/2021
10
08/01/2021
10
09/01/2021
9
10/01/2021
9
11/01/2021
9
12/01/2021
9
13/01/2021
9
I'm quite new to DAX and have tried multiple different variations of SUM(), SUMX() & FILTER() within CALCULATE(), all to no avail. I also don't know what the relationship between the two tables should be seeing as there are two dates in the Campaign table? Any help at all would be greatly appreciated.
Try this below Measure-
Measure =
var current_row_date = MIN('date'[Date])
RETURN
CALCULATE(
SUM(campaign[Daily Target]),
campaign[Start Date] <= current_row_date
&& campaign[End Date] >= current_row_date
)
output-

DAX Running Total with Buckets

I'm newish to Power BI/DAX, and I'm having trouble getting a running total to work the way I need. Assume the following table for data:
User month sales
UserA 1/1/2019 1
UserB 1/1/2019 3
UserC 1/1/2019 2
UserA 2/1/2019 1
UserB 2/1/2019 3
UserC 2/1/2019 2
UserA 3/1/2019 1
UserB 3/1/2019 3
UserC 3/1/2019 2
I've been looking around and I've found the following formula gives me a good running total the way I need:
AllSales =
calculate(
sum('table'[Sales]),
filter(
all ('table'),
'table'[date] <= max ('table'[date])
)
)
--
Total 6 12 18 18
The problem comes when I want to see this in matrix form with the users breaking out into buckets. When I do this, the number of sales is the same for each user:
UserA 6 12 18 18
UserB 6 12 18 18
UserC 6 12 18 18
Total 6 12 18 18
My desired outcome would look like this:
UserA 1 2 3 3
UserB 3 6 9 9
UserC 2 4 6 6
Total 6 12 18 18
I believe I understand why the ALL function is causing this, but I don't know how to tweak it or which function to switch to in order to resolve this issue. Any help would be very much appreciated. Thanks!
Instead of applying ALL to the entire table, apply it only to the column you need:
AllSales =
CALCULATE (
SUM ( 'table'[Sales] ),
FILTER ( ALL ( 'table'[date] ), 'table'[date] <= MAX ( 'table'[date] ) )
)

Add a dynamic constant line based on column in powerbi

I am trying to plot a line chart in powerBI with a reference line based on another columns value. I have data that represents the journeys of different cars on different sections of road. I am plotting those journeys that travel over the same section of road. e.g. RoadId 10001.
Distance JourneyNum Speed ThresholdSpeed RoadId
1 10 50 60 10001
2 10 51 60 10001
3 10 52 60 10001
1 11 45 60 10001
2 11 46 60 10001
3 11 47 60 10001
7 12 20 30 10009
8 12 21 30 10009
9 12 22 30 10009
10 12 23 30 10009
So currently I have:
Distance on x-axis (Axis),
Speed on y-axis (Values),
JourneyNum as the Legend (Legend),
filter to roadId 10001
I want to also add the thresholdSpeed as a reference line or just as another line would do. Any help?
I don't think it's possible (yet) to pass a measure to a constant line, so you'll need a different approach.
One possibility is to reshape your data so that ThresholdSpeed appears as part of your Legend. You can do this in DAX like so:
Table2 =
VAR NewRows = SELECTCOLUMNS(Table1,
"Distance", Table1[Distance],
"JourneyNum", "Threshold",
"Speed", Table1[ThresholdSpeed],
"ThresholdSpeed", Table1[ThresholdSpeed],
"RoadId", Table1[RoadId])
RETURN UNION(Table1, DISTINCT(NewRows))
Which results in a table like this:
Distance JourneyNum Speed ThresholdSpeed RoadId
1 10 50 60 10001
2 10 51 60 10001
3 10 52 60 10001
1 11 45 60 10001
2 11 46 60 10001
3 11 47 60 10001
1 Threshold 60 60 10001
2 Threshold 60 60 10001
3 Threshold 60 60 10001
7 12 20 30 10009
8 12 21 30 10009
9 12 22 30 10009
10 12 23 30 10009
7 Threshold 30 30 10009
8 Threshold 30 30 10009
9 Threshold 30 30 10009
10 Threshold 30 30 10009
Then you make a line chart on this table instead:
Note: It's probably preferable to do this transformation in the query editor though so you don't have redundant tables.

Finding the max(latest) date out of a column of dates then grouping them by employee

Importing the data frame
df = pd.read_csv("C:\\Users")
Printing the list of employees usernames
print (df['AssignedTo'])
Returns:
Out[4]:
0 vaughad
1 channln
2 stalasi
3 mitras
4 martil
5 erict
6 erict
7 channln
8 saia
9 channln
10 roedema
11 vaughad
Printing The Dates
Returns:
Out[6]:
0 2015-11-05
1 2016-05-27
2 2016-04-26
3 2016-02-18
4 2016-02-18
5 2015-11-02
6 2016-01-14
7 2015-12-15
8 2015-12-31
9 2015-10-16
10 2016-01-07
11 2015-11-20
Now I need to collect the latest date per employee?
I have tried:
MaxDate = max(df.FilledEnd)
But this just returns one date for all employees.
So we see multiple employees in the data set with different dates, in a new column named "LatestDate" I need the latest date that corresponds to the employee, so for "vaughad" in a new column it would return "2015-11-20" for all of "vaughad" records and in the same column for username "channln" it would return "2016-5-27" for all of "channln" latest dates.
You need to group your data first, using DataFrame.groupby(), after which you can produce aggregate values, like the maximum date in the FilledEnd series:
df.groupby('AssignedTo')['FilledEnd'].max()
This produces a series, with AssignedTo as the index, and the latest date for each of those employees as the values:
>>> df.groupby('AssignedTo')['FilledEnd'].max()
AssignedTo
channln 2016-05-27
erict 2016-01-14
martil 2016-02-18
mitras 2016-02-18
roedema 2016-01-07
saia 2015-12-31
stalasi 2016-04-26
vaughad 2015-11-20
Name: FilledEnd, dtype: object
If you wanted to add those max dates values back to the dataframe, use groupby(...).transform() with numpy.max instead, so you get a series with the same indices:
df['MaxDate'] = df.groupby('AssignedTo')['FilledEnd'].transform(np.max)
This adds in a MaxDate column:
AssignedTo FilledEnd MaxDate
0 vaughad 2015-11-05 2015-11-20
1 channln 2016-05-27 2016-05-27
2 stalasi 2016-04-26 2016-04-26
3 mitras 2016-02-18 2016-02-18
4 martil 2016-02-18 2016-02-18
5 erict 2015-11-02 2016-01-14
6 erict 2016-01-14 2016-01-14
7 channln 2015-12-15 2016-05-27
8 saia 2015-12-31 2015-12-31
9 channln 2015-10-16 2016-05-27
10 roedema 2016-01-07 2016-01-07
11 vaughad 2015-11-20 2015-11-20

How to find matching patterns from two different files

I have these two files, and would like to find the matching patterns
for the second file based on the entries from the first file, then print out the
matched results.
Here is the first file,
switch1 Ethernet2 4 4 5
switch1 Ethernet4 4 4 5
switch1 Ethernet7 4 4 5
switch1 Ethernet9 4 4 5
switch1 Ethernet10 4 4 5
switch1 Ethernet13 1 4 5
switch1 Ethernet14 1 4 5
switch1 Ethernet15 1 4 5
switch2 Ethernet5 4 4 5
switch2 Ethernet6 1 4 5
switch2 Ethernet7 4 5
switch2 Ethernet8 1 4 5
switch1 Ethernet1 2002 2697 2523
switch1 Ethernet17 2002 2576 2515
switch1 Ethernet11 2002 2621 2617
switch2 Ethernet1 2001 2512 2577
And here is the second file,
switch1 Ethernet1 1 4 5 40 2002 2523 2590 2621 2656 2661 2665 2684 2697 2999
switch1 Ethernet2 1 4 5 40 2002 2504 2505 2508 2514 2516 2517 2531 2533 2535 2544 2545 2547 2549 2555 2566 2571 2575 2589 2590 2597 2604 2611 2626 2629 2649 2666 2672 2684 2691 2695
switch1 Ethernet3 40
switch1 Ethernet4 1 4 5 40 2002 2504 2516 2517 2535 2547 2549 2571 2579 2590 2597 2604 2605 2620 2624 2629 2649 2684 2695
switch1 Ethernet5 1 4 5 40 55 56 2000 2002 2010 2037 2128 2401 2409 2504 2526 2531 2540 2541 2548 2562 2575 2578 2579 2588 2590 2597 2604 2606 2608 2612 2615 2616 2621 2638 2640 2645 2650 2666 2667 2669 2670 2674 2678 2684 2690 2696
switch1 Ethernet6 40
switch1 Ethernet7 1 4 5 40 2037 2128 2174 2401 2409 2463 2526 2535 2540 2541 2544 2562 2578 2579 2590 2616 2621 2625 2631 2645 2659 2667 2670 2674 2678 2682 2684 2690 2696
switch1 Ethernet8 1 4 5 40 2037 2128 2396 2401 2409 2420 2531 2619 2640 2653 2658 2669 2677 2683 2684
switch1 Ethernet9 1 4 5 40 2128 2169 2396 2401 2409 2420 2504 2515 2531 2553 2578 2597 2619 2621 2640 2658 2669 2677 2683 2684 2694
switch1 Ethernet10 1 4 5 40 2079 2128 2169 2378 2396 2453 2509 2578 2591 2597 2621 2634 2641 2657
switch1 Ethernet11 1 4 5 40 2002 2128 2169 2396 2453 2509 2512 2520 2526 2549 2552 2564 2571 2575 2589 2591 2597 2611 2617 2621 2634 2641 2657 2671 2676 2686 2694
switch1 Ethernet12 1 4 5 40 2079 2378 2396 2453 2515 2531 2553 2597 2619 2621 2640 2657 2669 2677 2684 2694
switch1 Ethernet13 1 4 5 40 2174 2396 2453 2463 2508 2524 2531 2536 2546 2567 2597 2629 2640 2657 2669 2674 2684
switch1 Ethernet14 1 4 5 40 2524 2536 2544 2567 2575 2582 2628 2640 2659 2674 2681 2689
switch1 Ethernet15 1 4 5 40 2515 2553 2575 2582 2621 2628 2640 2681 2689 2694
switch1 Ethernet16 1 4 5 40 2513 2539 2544 2553 2561 2573 2575 2582 2619 2640 2670 2681
switch1 Ethernet17 1 4 5 40 2002 2508 2513 2515 2531 2538 2539 2544 2547 2553 2561 2570 2573 2575 2576 2582 2586 2601 2608 2619 2621 2640 2658 2670 2681
the results should display,
switch1 Ethernet13 1 4 5
switch1 Ethernet14 1 4 5
switch1 Ethernet15 1 4 5
switch1 Ethernet8 1 4 5
switch1 Ethernet16 1 4 5
switch1 Ethernet1 2002 2697 2523
switch1 Ethernet17 2002 2576 2515
The challenging part for me is that I don't know
how to compare a pattern from one line of the first file to all the lines of the second file. And in this case, it has 5 patterns to match, when the second file doesn't have
consistent fields to work with. Some lines are shorter than the others, and the matching fields may not be the same with other lines.
Any idea or suggestion would be greatly appreciated.
Thanks much in advance.
Something like this should work, but there's probably a better way
#!/bin/bash
patternfile='file1'
exec 4<$patternfile
otherfile='file2'
while read -u4 pattern; do
grep ".*`echo ""$pattern"" | sed 's/\s\+/ \\\+.* /g'`.*" $otherfile
done
P.S. This doesn't output all of the lines you said you wanted:
Ethernet8 is switch2 in one and switch1 in the other. fixing that in regex would be really ugly, but you could use awk to get only the records you want.
Ethernet16 isn't even in the first file, I think that line is a mistake.
Ethernet1 and Ethernet17 have the records out of order, which my solution can't deal with.