Tableau SUMIF statement - if-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).

Related

SUM VALUES FROM 2 DISTINCT COLUMNS

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))

PowerBI: Ranking of songs based on number of votes

I do have a table songs with several fields; among them: songs[title]), with songs titles, obviously, and songs[votes], with the number of votes every song has received.
I'd like to create a measure using RANKX() in order to add it to a new table (a PowerBI widget, I mean, not a table from my data model) with those both fields (songs[title]) and songs[votes]) so it tells me the rank position of every song based on the number of votes it has.
I have tried:
songs_ranking = RANKX(ALL(songs), MAX(songs[votes]))
However, all songs end up ranked #1, as if ALL() were not able to remove row context for each song:
Any hint? Thanks in advance.
_rank:=RANKX(ALL('fact'),CALCULATE(MAX('fact'[Vote])),,ASC,Dense)

Calculating frequency of occurrence as a percentage of total, in Django queryset

In a Django app, I have a queryset for a data model called Comment. This contains text comments left by users.
Imagine 'n' users commented. What's the fastest way to calculate what % of comments were left by which user?
Right now I'm thinking it's going to be:
Comment.objects.filter(commenter=<username>).count()/Comment.objects.count()
What do you suggest? My objective is to flag people who're commenting too much, in order to screen their accounts for possible spamming. I'd be running this query voluminously, hence the focus on performance.
You should avoid making one query for each user in your database. Instead you can just query the number of comments for each user (or even the top n commenters) with something like:
from django.db.models import Count
total_comments = Comment.objects.count()
# Fetch top 10 commenters, annotated with number of comments, ordered by highest first
User.objects.annotate(num_comments=Count('comment')).order_by('-num_comments')[:10]
for user in users:
percentage = user.num_comments / total_comments
This example assumes you have a User model that your Comment has a foreign key to.
The percentage of total comments doesn't actually matter if you are comparing relative numbers of comments.

Django - show sums of a table of records

I have a table which has a list of invoices and their details:
class Invoice(models.Model):
invoiceNum = models.CharField(etc...)
invoiceDate = models.DateField(etc...)
customerID = models.ForeignKey(etc...)
isPaid = models.CharField(etc...)
The Invoice records do not hold the actual invoice total. Instead, an invoice's total is made up of several Invoice_Line_Item records, held in another table:
class Invoice_Line_Item(models.Model):
invNum = models.ForeignKey(Invoice)
itemName = models.CharField(etc...)
itemPrice = models.DecimalField(etc...)
I have a webapp that shows all the invoices in a big HTML table, along with all the details of that invoice on the table's tr row. Details such as, Invoice Date, Invoice Number, Customer ID, all come from that Invoice table. There are hundreds of invoices to display in this HTML table.
What I would like to do is also show each invoice's total value - which is the sum of all the line items. However, I can't think of a simple way to acomplish this since the invoice details and the line items that make up the invoice's total are in two different tables.
One way I thought is to pass the entire Invoice_Line_Item querySet into the HTML template, then for each invoice displayed in a table tr, I could iterate over the entire Invoice_Line_Item querySet, adding up all the line items that match the current invoice. This, however, seems hugely inefficient.
Any better ideas on how to do this?
Thanks!
One word: Aggregation
Invoice_Line_Item.objects.filter(invNum=invoice).aggregate(Sum('itemPrice'))
https://docs.djangoproject.com/en/dev/topics/db/aggregation/
Another way is to store the total in Invoice and update it whenever you change a related Invoice_Line_Item
One more word: annotate.
from django.models import Sum
Invoice.objects.filter( .. ).annotate( InvTotal=Sum( 'invoice_line_number__itemPrice' ) )
InvTolal becomes a new attribute of Invoice object, you can use it in template the same way as invoiceNum or invoiceDate.
With this approach you do not have to pass any additional data structures to your template, only a QuerySet of Invoices.
Please note:
Argument of Sum is a string, which is a concatenation of the name of related model converted to lowercase, than double '_', and than the name of a field in related model.

Google Charts - Multiple Category Filters - Restricting options?

I have 3 suppliers, 40 brands and 120 products, all displayed in three category filters. When a user selects one supplier from the Supplier filter, I want the Brand filter to hide all brands that are not provided by that supplier. Likewise, when a brand is selected, I want to hide the irrelevant products from the Products filter. I don't really care about what's being shown in the table at this point.
Is there any way to implement something like this?
Heres an example for you -
http://jsfiddle.net/danabnormal/cjn2tbbm
You can do this by creating a Dashboard. At around line 61 you can see that the selection made in the 'Sex' drop down is bound to the 'Name' dropdown, thus limiting what Names can be selected.
dashboard.bind(filterSex, filterName);
dashboard.bind(filterSex, pieChart);