Calculate the average row_count by each process in DAX/powerbi? - powerbi

I have a table in powerbi that has columns:
process | row_count |
P1 1
P1 2
P1 3
P2 4
P2 5
P3 2
P3 1
and I want to add a column that will have the average row_count of each process.
For example,
process | row_count | avg_row_count |
P1 1 2
P1 2 2
P1 3 2
P2 4 3
P2 5 3
P3 2 1
P3 1 1
Does anyone know how to do this using Dax /powerbi?

DAX measure for New calculated Column in a DataTable displaying the average of [row_count] column, in relation to distinct values in a [process] column.
avg_row_count = CALCULATE(
AVERAGE('Table1'[row_count]),
ALLEXCEPT('Table1','Table1'[process])
)

Related

create a measure count of values and blank in a column with dax

I have these columns and I want to create a measure to calculate the number of types
types
ID Type
1 A
2 A
3 B
4 C
5 D
6 A
7
8
Results :
A 3
B 1
C 1
D 1
Blank 2
Try this Calculated Table:
Results =
SUMMARIZE(
'Table',
'Table'[Type],
"Count", COUNT('Table'[ID])
)

SUM of DISTINCTCOUNT

I need to sum of different suppliers by produt_id, below is an example of table:
product_id
supplier
1
A
1
B
2
A
2
C
2
C
3
D
4
A
4
B
4
B
4
E
4
E
5
C
5
F
5
F
In the table I have other elements, but these columns are the relevant ones for the count.
The answer I'm looking for is 10, I try to explain in the table below:
product_id
DistinctCount(supplier)
1
2
2
2
3
1
4
3
5
2
Total = 2 + 2 + 1 + 3 + 2 = 10
Thanks in advance!
I tried several different ways and I couldn't. I hope I can solve my problem.
These 2 measures will solve your problem
Count Supplier = DISTINCTCOUNT('Table'[supplier])
and
Sum Count Supplyer =
SUMX(
DISTINCT( 'Table'[product_id] ),
[Count Supplier]
)
The Total of Count Supplier is the number of distinct suppliers in the whole table, ignoring the grouping.

dax calculation for costume measure

i have a 2 tables that looks like this
Key |Num Of Treatments| Cost |
1 2 1000
1 2 1500
1 2 2000
2 3 700
3 3 800
4 4 900
key | limit |
1 1
2 1
3 2
4 3
the calculation that i want to do on dax is : (Num Of Treatments-Limit)*cost/Num Of Treatments
Assuming that the key column is unique for the second table (Table2 in dax).
Calculation =
VAR _limit =
LOOKUPVALUE ( Table2[limit], Table2[key], [key] )
RETURN
DIVIDE ( ( [Num Of Treatments] - _limit ) * [cost], [Num Of Treatments] )
This can be easily be achieved after creating one to many relationship between two tables with column Key.
Dax formula :
New Measure = ((SUM(Asset[No Of Treatments])-SUM(Tickets[Limit]))*SUM(Asset[Cost]))/SUM(Asset[No Of Treatments])

pandas dataframe category codes from two columns

I got a pandas dataframe where two columns correspond to names of people. The columns are related and the same name means same person. I want to assign the category code such that it is valid for the whole "name" space.
For example my data frame is
df = pd.DataFrame({"P1":["a","b","c","a"], "P2":["b","c","d","c"]})
>>> df
P1 P2
0 a b
1 b c
2 c d
3 a c
I want it to be replaced by the corresponding category codes, such that
>>> df
P1 P2
0 1 2
1 2 3
2 3 4
3 1 3
The categories are in fact derived from the concatenated array ["a","b","c","d"] and applied on individual columns seperatly. How can I achive this ?.
Use:
print (df.stack().rank(method='dense').astype(int).unstack())
P1 P2
0 1 2
1 2 3
2 3 4
3 1 3
EDIT:
For more general solution I used another answer, because problem with duplicates in index:
df = pd.DataFrame({"P1":["a","b","c","a"],
"P2":["b","c","d","c"],
"A":[3,4,5,6]}, index=[2,2,3,3])
print (df)
A P1 P2
2 3 a b
2 4 b c
3 5 c d
3 6 a c
cols = ['P1','P2']
df[cols] = (pd.factorize(df[cols].values.ravel())[0]+1).reshape(-1, len(cols))
print (df)
A P1 P2
2 3 1 2
2 4 2 3
3 5 3 4
3 6 1 3
You can do
In [465]: pd.DataFrame((pd.factorize(df.values.ravel())[0]+1).reshape(df.shape),
columns=df.columns)
Out[465]:
P1 P2
0 1 2
1 2 3
2 3 4
3 1 3

Power BI, show category average rows in same table

I have this raw data from source:
Category Product Price
C1 P1 1
C1 P2 1
C1 P3 4
C2 P4 2
C2 P5 10
C2 P6 12
I want to visualise a Power BI table that shows the Category average within the same structure:
Category Product Price
C1 P1 1
C1 P2 1
C1 Avg_C1 3
C1 P3 4
C2 P4 2
C2 Avg_C2 8
C2 P5 10
C2 P6 12
Many thanks if you show me a solution.
Just to reformat the question...
I have this raw data from source:
**Category Product Price**
C1 P1 1
C1 P2 1
C1 P3 4
C2 P4 2
C2 P5 10
C2 P6 12
I want to visualise a Power BI table that shows the Category average within the same structure:
**Category Product Price**
C1 P1 1
C1 P2 1
C1 Avg_C1 3
C1 P3 4
C2 P4 2
C2 Avg_C2 8
C2 P5 10
C2 P6 12
You can create a Matrix visual in which you place the Category and Product columns on Rows and for Values you use a Measure that would be like this:
PriceWithAverage =
VAR CurrentCategory =
MAX ( ProductTable[Category] )
RETURN
IF (
ISFILTERED ( ProductTable[Product] ),
MAX ( ProductTable[Price] ),
CALCULATE (
AVERAGE ( ProductTable[Price] ),
FILTER ( ProductTable, ProductTable[Category] = CurrentCategory )
)
)
Let us know if that works for you
Best
David