SUM VALUES FROM 2 DISTINCT COLUMNS - powerbi

Hello, people.
As you guys can see on the table above, I´have more than one "seller" per "order_num" and it’s ok. But I need to SUM the "sku_qtd" without repeating the items from the order. Right now, SUM is returning 6 instead of 3.
Pls help me, I have been looking for an answer for hours.

If you cannot modify the data model to properly represent the relationship between order_num and seller (many-to-one or many-to-many), this DAX formula should do it:
sum_qtd = SUMX(DISTINCT('table'[order_num]),FIRSTNONBLANK('table'[sku_qtd],0))

Related

Tableau SUMIF statement

I have a dataset of movies with the amount of views per day, I want to sum total views per movie and I am doing the following:
SUM(IF [Title] = [Title]
THEN [Views]
END)
But is not giving me the right numbers, can anyone please help me?
You don't need to make a calculated field every time you want to see values in Tableau - that's part of the beauty of it.
Assuming that the Views field is a numeric measure by default, and the Movie field is a text dimension by default, then just drag Movie to one shelf (such as Rows) and Views to another (such as Columns).

Doctrine 2 How to do a Sum from a Join but Group by 2 columns

I am using Symfony and Doctrine Querybuilder to return a list of Orders with a count of all Products in each Order. That means I have a 1:n Relationship between Orders and OrderProducts the OrderProducts table stores the OrderID, and the quantity of each product. If I do a groupBy o.orderId then I get the correct Sum, but then I do not get the data for all the orderProducts belonging to the orderId so if there are 2 different Products in the orderproducts table then I get qty:2 but only the data for 1 product, so I need to group by oi.orderProductId but obviously as soon I add the second groupBy oi.orderProductId then the TotalCount is showing me the sum for each individual record. I am fully aware that this is the correct behaviour, but I just can not get my head around it if there is a way to achieve what I am trying to do. I assume I have to somehow use a subselect, but I am not sure if this is even possible to achieve. My 2 tables and my attemped so far
tbl order
orderId
dateCreated
tblorderProducts
orderProductid
orderId
qty
return $this->createQueryBuilder('o')
->select(array(
'o AS orderData',
'oi',
'sum(oi.qty)AS noOfItems'))
->join('o.orderItems', 'oi')
->groupBy('o.orderId')
->groupBy('oi.orderProductId')
->getQuery()
->execute();
I hope somebody can give me a hint how I could achieve it. Thank you very much in advance.
The groupBy() method always replaces the current group by clause. Use addGroupBy() method instead.

Assign a rank to a movie based on number of comments - Django

I have movies, each movie has comments added to it. I want to create a function that will rank my Movies based on a number of comments using dense ranking. This is what I got so far - the problem is, every movie always has rank 1.
commented_movies = Movie.objects.annotate(comment_count=Count('comments', distinct=True)).annotate(
rank=Window(expression=DenseRank()
, order_by=F('comment_count').desc()
, partition_by=[F('id')]))
I guess the problem might be in partition_by, but I have no idea how to solve it. id is a primary key of the Movie.
Okay, that was quick. I've just figured out partition_by is not necessary - removed it and it works:
commented_movies = Movie.objects.annotate(comment_count=Count('comments', distinct=True)).annotate(
rank=Window(expression=DenseRank()
, order_by=F('comment_count').desc()
, partition_by=[F('id')]))

Django orm group by multiple columns

How to perform a multiple column group by in Django?
I have only seen examples on one column group by.
Below is the query i am trying to convert to Django ORM.
SELECT order_id,city,locality,login_time,sum(morning_hours),sum(afternoon_hours),sum(evening_hours),sum(total_hours)
FROM orders
GROUPBY order_id,city,locality,login_time`
from django.db.models import Sum
Your_Model.objects.values(
"order_id", "city", "locality", "login_time"
).order_by().annotate(
Sum("morning_hours"),
Sum("afternoon_hours"),
Sum("evening_hours"),
Sum("total_hours"),
)
I hope the above code snippet helps (While answering, I had no idea whether morning hours, afternoon hours, etc are derived columns or existing fields in the table, because you have specified nothing of the sort in your question. Hence, I made my assumption and have answered your question). For grouping by multiple columns, there already exists a question on SO. See this link.

django aggregate for multiple days

I have a model which has two attributes: date and length and others which are not relevant. And I need to display list of sums of length for each day in template.
The solution I've used so far is looping day by day and creating list of sums using aggregations like:
for day in month:
sums.append(MyModel.objects.filter(date=date).aggregate(Sum('length')))
But it seems very ineffective to me because of the number of db lookups. Isn't there a better way to do this? Like caching everything and then filter it without touching the db?
.values() can be used to group by date, so you will only get unique dates together with the sum of length fields via .annotate():
>>> from django.db.models import Sum
>>> MyModel.objects.values('date').annotate(total_length=Sum('length'))
From docs:
When .values() clause is used to constrain the columns that are returned in the result set, the method for evaluating annotations is slightly different. Instead of returning an annotated result for each result in the original QuerySet, the original results are grouped according to the unique combinations of the fields specified in the .values() clause.
Hope this helps.