PowerBI: Ranking of songs based on number of votes - powerbi

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)

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

How to get two player records from same player table in one row in a table visualisation?

I have a database table of sports matches which has two fields for player IDs: p1_id and p2_id. These fields both have foreign keys to a player table where there is an id and a name field.
I want to build a table visualisation in PowerBI that has both player names in a single row. I can't do this easily because I can only use one of the foreign key relationships at any one time. I've tried using USERELATIONSHIP but it looks like this can only be used within a CALCULATE function.
I'm sure there's a way to do this but it's beyond me! Thanks in advance.
Try with lookupvalue.
p1Name = LOOKUPVALUE(players[Name],players[pID], SELECTEDVALUE('Table'[p1]))
p2Name = LOOKUPVALUE(players[Name],players[pID], SELECTEDVALUE('Table'[p2]))
You can also check this video [Lookup multiple values in DAX]:
https://www.youtube.com/watch?v=SRnIH0pnF3o&ab_channel=SQLBI

using inner join criteria language

Have two tables : Rating and Books. Rating table has foreign key to the Books table.
These tables are mapped this way :
Books :
HasMany(x => x.RatingList).Cascade.All().Inverse();
and the Rating table :
References(x => x.Books).Column("BookId").Cascade.All();
Have this :
var bks = session.CreateCriteria("Books", "b");
using this restriction for selecting books
bks.Add(Restrictions.Eq("CategoryId",id));
here is the problem, how to join Rating table ??
bks.CreateAlias("b.Rating", "c");
bks.List();
return PartialView("/Views/Home/_Books.cshtml", bks);
The final result i need is to select all Books but also Rating for them. In the Rating table has many ratings for one book. Book rating should be given as average of ratings.
Some help ?
A criteria lets to retrieve a list of a specific class, in your case List<Book>. So, you are asking hibernate to retrieve a book list, not a list of books and ratings.
Of course, you can access ratings for every book into the resulting list. If it doesn't work, maybe a LazyInitialitationException happens. In this case you will have to apply OSIVF, extends session lifetime, or whatever.
Criteria lets you join entities to filter query results. If you create an alias for ratings, it is because you want to filter results with an ratings attributes, but it won't include ratings into the resulting list.

SharePoint calculated columns

Can I create a calculated column that totals the record count in another list based on a field value?
Example:
List1 contains departments, each with a deptID
List2 contains employees, each assigned to a deptID
Can List1 contain a total employee count (for that deptID)?
No, this field does not exist.
You can create custom view or workflow (or maybe eventreciever) on add item in the list2, which will change corresponding field of conformity item in the list1.
But it's best just to create the custom view.
No, calculated field can refer only to fields in the same item.
I suggest you to use EventReceiver to accomplish your request. Register the receiver to the employee list with ItemAdded, ItemUpdated and ItemDeleting events. In the receiver's methods update corresponding department item and its field (number of employees).

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.