Power BI average rating per product - average

I have a Ratings table with some product ratings:
productKey rating
product-1 4
product-1 5
product-2 3
I want to calculate the average rating per product:
Incorrect average (default): (4+5+3)/3 = 12/3 = 4
Average per product : ( (4+5)/2 + 3 ) / 2 = (4.5 + 3) / 2 = 3.75
I managed to do this with an intermediate table
1) Create table which averages ratings per product:
RatingsPerProd = SUMMARIZE(Ratings,Ratings[productKey],"averageRating",AVERAGE(Ratings[rating]))
which creates the following table:
productKey averageRating
product-1 4.5
product-2 3
2) Then I simply do an AVERAGE of averageRating
However I'd like to do this with 1 single measure on the original Ratings table, but whatever I try to do with the SUMMARIZE formula I get the following error:
The expression refers to multiple columns... cannot be converted to a
scalar value
How can I achieve average per product in 1 single measure on the original Ratings table?

You can try the following formula:
AveragePerProduct =
AVERAGEX (
VALUES ( Ratings[productKey] ),
CALCULATE ( AVERAGE ( Ratings[rating] ) )
)
Result:

Related

How to divide an individual unit value of a column by another column's aggregate value?

I have been trying to take individual scores from my database and divide them by the collective sum of a secondary score of students that share the same name without aggregating them and maintaining individual cells.
I have a database like this
name score 1 score 2
reed 30 10
reed 50 20
brick 60 30
brick 60 12
and i want this output for a new column Score % 2
name score 1 score 2 score % 2
reed 30 10 10/(30+50)=0.125
reed 50 20 20/(30+50)=0.25
brick 60 30 30/(60+60)=0.25
brick 60 12 10/(60+60)=0.1
so i figured my query would be something like: Score 1/SUM(Score 2) OVER (PARTITION BY Name)
but this doesn't really work probably due to the fact the PARTITION BY is trying to sum by name but the first part of the query refers to individual, unit level data.
Is what I want even possible? Thanks you!
You can just join in the SUM like so.
Select score1,
score2,
sums.score1,
Score2/sums.score1 AS [score % 2]
FROM table INNER JOIN (
SELECT name,
SUM(Score1) score1
FROM table
GROUP BY name
) AS sums ON table.name = sums.name

Create deciles to group and label records where the sum of a value is the same for each decile

I have something similar to the following table, which is a randomly ordered list of thousands of transactions with a Customer_ID and an order_cost for each transaction.
Customer_ID
order_cost
1
$503
53
$7
4
$80
13
$76
6
$270
78
$2
8
$45
910
$89
10
$3
1130
$43
etc...
etc...
I want to group the transactions by Customer_ID, aggregate the cost of all the orders into a spending column, and then create a new "decile" row that would assign a number 1-10 to each customer so that when the "spending" for all customers in a decile is added up, each decile contains 10% of all the spending.
The resulting table would look something like the table below where each ascending decile will contain fewer customers, but the total sum of "spending" for all the records in each decile group will be the same for deciles 1-10. (The actual numbers in this sample column don't actually add up, it's just the concept)
Customer_ID
spending
Decile
45
$500
1
3
$700
1
349
$800
1
23
$1,000
1
64
$2,000
1
718
$2,100
1
3452
$2,300
1
1276
$2,600
2
10
$3,000
2
34
$4,000
2
etc...
etc...
etc...
So far I have grouped by Customer_ID, aggregated the order_cost to a spending column, ordered each customer in ascending order based on the spending column, and then partitioned all the customers into 5000 groups. From there I manually found the values for each .when statement that would result in deciles 1-10 each containing the right amount of customers so each decile has 10% of the sum of the entire spending column. It's pretty time-consuming to use trial and error to find the right bucket configuration that results in each decile having 10% of the spending column.
I'm trying to find a way to automate this process so I don't have to find the right bucketing ratio for each decile by trial and error.
This is my code so far:
Import pyspark.sql.functions as F
deciles = (table
.groupBy('Customer_ID')
.agg(F.sum('order_cost').alias('spending')).alias('a')
.withColumn('rank', F.ntile (5000).over(W.Window.partitionBy()
.orderBy(F.asc('spending'))))
.withColumn('rank', F.when(F.col('rank')<=4628, F.lit(1))
.when(F.col('rank')<=4850, F.lit(2))
.when(F.col('rank')<=4925, F.lit(3))
.when(F.col('rank')<=4965, F.lit(4))
.when(F.col('rank')<=4980, F.lit(5))
.when(F.col('rank')<=4987, F.lit(6))
.when(F.col('rank')<=4993, F.lit(7))
.when(F.col('rank')<=4997, F.lit(8))
.when(F.col('rank')<=4999, F.lit(9))
.when(F.col('rank')<=5000, F.lit(10))
.otherwise (F.lit(0)))
)
end_table = (table.alias('a').join(deciles.alias('b'), ['Customer_ID'], 'left')
.selectExpr('a.*', 'b.rank')
)

(psql) How to sum up on a column based on conditions?

I have a following postgresql table.
user
amount
type
danny
2
deposit
danny
3
withdraw
kathy
4
deposit
kathy
5
deposit
kathy
6
withdraw
Now, I am trying to get every user's remaining wallet balance. The sum up calculation works like this: for deposits they are positive in the sum function and for withdraw they are negative values. e.g. For danny, the remaining balance after 2 deposit and 3 withdraw is 2 - 3 = -1. For kathy, the remaining balance is 4 + 5 - 6 = 3.
What is the easiest way to calculate this in one Postgresql query?
Thanks.
Convert the type from text to the numeric factor 1 or -1 as appropriate. Then just do sum(amount * factor):
with test (usr, amount, type) as
( values ( 'danny', 2, 'deposit')
, ( 'danny', 3, 'withdraw')
, ( 'kathy', 4, 'deposit')
, ( 'kathy', 5, 'deposit')
, ( 'kathy', 6, 'withdraw')
)
-- your query starts here
select usr "User"
, sum (amount * factor) "Balance"
from ( select usr
, amount
, case when type = 'deposit' then 01
when type = 'withdraw' then -1
else null
end factor
from test
) sq
group by usr
order by usr;
NOTE: It is poor practice to use user as an identifier (i.e. column name, etc) since it is both a Postgres and SQL standard reserved word.

What will be the SQL to get the results by applying group by on specific matching string of column value?

What will be the SQL to get the results by applying the group by on a specific matching string of column value?
Table: results ( this is not the physical table but the result of specific queries )
rname count1 count2
Avg-1 2 2
Avg-1 1 1
Avg-2 2 2
Avg-1 1 1
Zen-3 2 2
Zen/D 2 1
QA/C 3 1
QA/D 2 1
The expected output is:
rname count1 count2
Avg 6 6
Zen 4 3
QA 5 2
In expected output count1 is sum of all count1 of rname which match the string 'Avg', 'Zen' and 'QA' respectively. Same for count2.
What will be the SQL can you please give me some directions?
demo:db<>fiddle
SELECT
(regexp_split_to_array(rname,'[-/]'))[1],
SUM(count1) AS count1,
SUM(count2) AS count2
FROM
mytable
GROUP BY 1
regexp_split_to_array(rname,'[-/]') splits the rname value at the - or the / character. Taking the first part ([1]) gives you Avg, Zen or QA
Group hy this result (using the column index 1)
SUM up the values

Crystal Reports 11 - Formula. 2 Groups

What is happening is that I have a formula that is in the Group Header 2. What it is doing is SUM all of Group 2 (Invoice ID)
formula = SUM({Commissions_ttx.CommissionAmount},{Commissions_ttx.arpARInvoiceID})
Group 1 is EmpID
Group 2 is InvoiceID
It is skipping over the Group of 1 instead is getting all of the InvoiceID. There can be more than 1 employee on this. So the totals are doubling. Any ideas?
in the sum function you need to do something like
SUM({Commissions_ttx.arpARInvoiceID},{your group name})
if you have multiple sum values you need to get the sum of each group separately into 2 variables the the sum of both values e.g.
variable1 = SUM({field1},{Group1})
variable2 = SUM({field2},{Group2})
variable3 = tonumber(variable1 + variable2)