How to Compare Averages of Categories - powerbi

I have 3 tables:
table1:
Category
--------
1
2
3
4
table2:
Branch | Category
------------------
A1 | 1
A2 | 1
A3 | 2
A4 | 3
A5 | 4
A6 | 4
A7 | 4
table3: Values
Date | Branch | Value
---------------------------
1/1/2019 | A1 | 35
1/1/2019 | A2 | 40
1/1/2019 | A4 | 62
1/1/2019 | A5 | 10
1/1/2019 | A6 | 20
1/1/2019 | A7 | 35
Table Connections are:
Category -->--- Branch
and
Branch -->-- Values
I am trying to compare the value of every Branch with the average of the Category that belongs to. If the value of the Branch is below Category's average then it is good else is bad.
If I create a matrix with Branch, value and Category Average
it looks like this :
Branch | Value | Category Average
-------------------------------------
A1 | 35 | 35
A2 | 40 | 40
A4 | 62 | 62
A5 | 10 | 10
A6 | 20 | 20
A7 | 35 | 35
BranchAVG = AVERAGE(Table3[Value])
CategoryAVG = AVERAGEX(VALUES(Categories[Category]);[BranchAVG])
But it should look like this:
Branch | Value | Category Average | Good/Bad
------------------------------------------------
A1 | 35 | 37.5 | Good
A2 | 40 | 37.5 | Bad
A4 | 62 | 62.0 | Good
A5 | 10 | 21.7 | Good
A6 | 20 | 21.7 | Good
A7 | 35 | 21.7 | Bad
Is there any possible way to do that?
Thanks in advance
Elias

You can do this by adding a Category column to your Value table:
Category = RELATED(Branch[Category])
And after this add the AgeragePerCategory column:
AgeravePerCategory = CALCULATE(AVERAGE('Values'[Value]);FILTER('Values'; 'Values'[Category] = EARLIER('Values'[Category])))

Thanks for the answer.
To work it out I had to add an additional filter to Date, because it averages all values regardless the date.
So the AveragePerCategory it looks like:
AveragePerCategory = CALCULATE(AVERAGE('Values'[Value]);FILTER('Values'; 'Values'[Category] = EARLIER('Values'[Category]) && 'Values'[Date] = EARLIER('Values'[Date])))

Related

After appending, I get null values in primary table headers

I have a table that I want to use as headers for another table that just has data. I used append as new in PBI, used the headers table as primary and data table as secondary. All the columns from the primary table have null values and the data table is appended next to headers column.
Eg:
Table 1 ( Headers)
+-----+-----+-----+-----+
| ABC | DEF | IGH | KLM |
+-----+-----+-----+-----+
Table 2 ( Data )
+----+----+----+----+
| 1 | 2 | 3 | 4 |
| 6 | 7 | 8 | 9 |
| 11 | 12 | 13 | 14 |
| 16 | 17 | 18 | 19 |
| 21 | 22 | 23 | 24 |
| 26 | 27 | 28 | 29 |
| 31 | 32 | 33 | 34 |
+----+----+----+----+
Table I am getting after append:
+------+------+------+------+------+------+------+------+
| ABC | DEF | IGH | KLM | null | null | null | null |
+------+------+------+------+------+------+------+------+
| null | null | null | null | 1 | 2 | 3 | 4 |
| null | null | null | null | 6 | 7 | 8 | 9 |
| null | null | null | null | 11 | 12 | 13 | 14 |
| null | null | null | null | 16 | 17 | 18 | 19 |
| null | null | null | null | 21 | 22 | 23 | 24 |
| null | null | null | null | 26 | 27 | 28 | 29 |
| null | null | null | null | 31 | 32 | 33 | 34 |
+------+------+------+------+------+------+------+------+
Table I need:
+-----+-----+-----+-----+
| ABC | DEF | IGH | KLM |
+-----+-----+-----+-----+
| 1 | 2 | 3 | 4 |
| 6 | 7 | 8 | 9 |
| 11 | 12 | 13 | 14 |
| 16 | 17 | 18 | 19 |
| 21 | 22 | 23 | 24 |
| 26 | 27 | 28 | 29 |
| 31 | 32 | 33 | 34 |
+-----+-----+-----+-----+
I used Append as new in PBI
Used the headers table ( Table 1) as primary and appended Table 2 to it.
This shows at the top function:
= Table.Combine({Table 1, Table 2})
This in the advanced editor:
let
Source = Table.Combine({Sheet1, InterviewQn})
in
Source
Expected result:
+-----+-----+-----+-----+
| ABC | DEF | IGH | KLM |
+-----+-----+-----+-----+
| 1 | 2 | 3 | 4 |
| 6 | 7 | 8 | 9 |
| 11 | 12 | 13 | 14 |
| 16 | 17 | 18 | 19 |
| 21 | 22 | 23 | 24 |
| 26 | 27 | 28 | 29 |
| 31 | 32 | 33 | 34 |
+-----+-----+-----+-----+
OR
+-----+-----+-----+-----+
| ABC | DEF | IGH | KLM |
| 1 | 2 | 3 | 4 |
| 6 | 7 | 8 | 9 |
| 11 | 12 | 13 | 14 |
| 16 | 17 | 18 | 19 |
| 21 | 22 | 23 | 24 |
| 26 | 27 | 28 | 29 |
| 31 | 32 | 33 | 34 |
+-----+-----+-----+-----+
If you're only trying to rename the columns of Table 2, using the column names of Table 1, then it's simply:
= Table.RenameColumns(#"Table 2", List.Zip({Table.ColumnNames(#"Table 2"), Table.ColumnNames(#"Table 1")}))
See https://pwrbi.com/so_55529969/ for worked example PBIX file

Power BI DAX to filter common items A & B share

Sample data:
| Vendor | Size Group | Model | Quantity | Cost | TAT | Posting Date |
|--------|------------|-------|----------|-------|-----|-------------------|
| A | S | A150 | 150 | 450 | 67 | July 7, 2018 |
| A | M | A200 | 250 | 1500 | 75 | June 22, 2018 |
| A | M | A150 | 25 | 8500 | 85 | July 9, 2018 |
| C | L | A200 | 350 | 1250 | 125 | March 5, 2018 |
| C | XL | A500 | 150 | 6500 | 45 | February 20, 2018 |
| A | M | A900 | 385 | 475 | 40 | January 29, 2018 |
| A | M | A150 | 650 | 45 | 45 | August 31, 2018 |
| D | M | A150 | 65 | 7500 | 15 | April 10, 2018 |
| D | M | A300 | 140 | 3420 | 10 | April 3, 2018 |
| E | S | A150 | 20 | 10525 | 85 | January 3, 2018 |
| B | S | A150 | 30 | 10500 | 40 | June 3, 2018 |
| B | S | A150 | 450 | 450 | 64 | April 3, 2018 |
| E | XS | A900 | 45 | 75 | 60 | January 3, 2018 |
| F | M | A900 | 95 | 655 | 175 | January 3, 2018 |
| D | XL | A300 | 15 | 21500 | 25 | January 3, 2018 |
| D | S | A500 | 450 | 65 | 25 | May 3, 2018 |
| A | M | A350 | 250 | 450 | 22 | January 3, 2018 |
| B | S | A150 | 45 | 8500 | 28 | January 3, 2018 |
| A | S | A300 | 550 | 650 | 128 | January 3, 2018 |
| C | M | A150 | 1500 | 855 | 190 | January 3, 2018 |
| B | M | A150 | 65 | 1750 | 41 | January 3, 2018 |
| A | L | A500 | 75 | 1700 | 24 | January 3, 2018 |
| B | S | A900 | 55 | 9800 | 37 | May 29, 2018 |
| B | M | A500 | 150 | 850 | 83 | April 18, 2018 |
In the provided sample, the common Size Groups A & B both share are S & M. So, I was hoping to display those Size Groups as the legend and Average Cost as the value in a clustered column chart.
Can anyone please advise how I can go about this?
Thank you!!!

Sort by two or more variables

I'm trying to sort by ID and then by Date.
What I have:
| ID | Date |
| ----------------------|
| 112 | 2013-01-01 |
| 112 | 2013-01-15 |
| 113 | 2012-01-01 |
| 112 | 2014-02-13 |
| 112 | 2013-01-02 |
| 113 | 2011-01-11 |
What I need:
| ID | Date |
| ----------------------|
| 112 | 2013-01-01 |
| 112 | 2013-01-02 |
| 112 | 2013-01-15 |
| 112 | 2014-02-13 |
| 113 | 2011-01-11 |
| 113 | 2012-01-01 |
My problem is that I only know how to sort by ID or Date.
More generally:
clear
input id foo
1 56
1 34
2 13
1 67
1 22
2 89
2 61
2 76
end
sort id (foo)
list, sepby(id)
+----------+
| id foo |
|----------|
1. | 1 22 |
2. | 1 34 |
3. | 1 56 |
4. | 1 67 |
|----------|
5. | 2 13 |
6. | 2 61 |
7. | 2 76 |
8. | 2 89 |
+----------+
In a more advanced programming context you can use the same syntax with bysort.

Conditionally create new observations

I have data in the following format (there are a lot more variables):
year ID Dummy
1495 65 1
1496 65 1
1501 65 1
1502 65 1
1520 65 0
1522 65 0
What I am trying to achieve is conditionally create new observations that fills in the data between two points in time conditional on a dummy. If the dummy is equal to 1, the data is supposed to be filled in. If the variable is equal to 0 then it shall not be filled in.
For example:
year ID Dummy
1495 65 1
1496 65 1
1497 65 1
1498 65 1
.
.
1501 65 1
1502 65 1
1503 65 1
1504 65 1
.
.
.
1520 65 0
1522 65 0
Here's one way to do this:
clear
input year id dummy
1495 65 1
1496 65 1
1501 65 1
1502 65 1
1520 65 0
1522 65 0
end
generate tag = year[_n] != year[_n+1] & dummy == 1
generate delta = year[_n] - year[_n+1] if tag
replace delta = . if abs(delta) == 1
expand abs(delta) if tag & delta != .
sort year
bysort year: egen seq = seq() if delta != .
replace seq = seq - 1
replace seq = 0 if seq == .
replace year = year + seq if year != .
drop tag delta seq
The above code snippet will produce:
list
+-------------------+
| year id dummy |
|-------------------|
1. | 1495 65 1 |
2. | 1496 65 1 |
3. | 1497 65 1 |
4. | 1498 65 1 |
5. | 1499 65 1 |
|-------------------|
6. | 1500 65 1 |
7. | 1501 65 1 |
8. | 1502 65 1 |
9. | 1503 65 1 |
10. | 1504 65 1 |
|-------------------|
11. | 1505 65 1 |
12. | 1506 65 1 |
13. | 1507 65 1 |
14. | 1508 65 1 |
15. | 1509 65 1 |
|-------------------|
16. | 1510 65 1 |
17. | 1511 65 1 |
18. | 1512 65 1 |
19. | 1513 65 1 |
20. | 1514 65 1 |
|-------------------|
21. | 1515 65 1 |
22. | 1516 65 1 |
23. | 1517 65 1 |
24. | 1518 65 1 |
25. | 1519 65 1 |
|-------------------|
26. | 1520 65 0 |
27. | 1522 65 0 |
+-------------------+

Editing a text file in python?

I have this in a text file :
Rubble HM3 80 HM2 90 HM4 92
Bunny HM2 92 HM5 70 HM1 98
Duck HM1 86 HM3 100 HM2 93 HM4 94
Chipmunk HM4 96 HM1 86
Simpson HM3 70 HM1 90 Test1 90
and i want to write a code that changes it to this :
Name | HM1 | HM2 | HM3 | HM4 | Avg. |
________________________________________________
Bunny | 98 | 92 | 0 | 0 | 47.50 |
Chipmunk | 86 | 0 | 0 | 96 | 45.50 |
Duck | 86 | 93 | 100 | 94 | 93.25 |
Rubble | 0 | 90 | 80 | 92 | 65.50 |
Simpson | 90 | 0 | 70 | 0 | 40.00 |
so far :
my_file=open("C:/python27/tools/student_grades.txt", "r+")
my_file_pointer=my_file.read()
for lines in my_file_pointer:
x=my_file_pointer.replace("HM2","|")
print x
Go Easy first time programmer . :)
and if i use the replace function how can i print it all at once and then sort it under every subject "HM1" ?