Select Columns from multiple tables - Django - django

I am trying to select all the columns from one table and only a single column from the second table using the Django ORM.The two tables are related by a foreign key but the column in the second table which i want, is not related, so using select_related doesn't work.
I used
FirstTable.objects.filter().values("All first table columns separated by a comma","second tablename__column name")
and this worked fine. Since, the columns are very large in number, i do not want to list all of them in "values". So, i am looking for an easy way to do this.
Thanks for your help
EDIT:
Thanks for the help! I was able to get it working by using annotate like the following.
FirstTable.objects.filter().annotate(variable_name=F("SecondtableFieldName"))

You Can use the extra method in this.
FirstTable.objects.filter().extra("write down here what every yo want")

Related

Clean up a dimension table

My dimension tables contains more rows than my fact table, I would like my dim table fields to show only the values in the fact table when used as a filter in the filter panel.What cleaning/modeling steps are the best to achieve this.
I also know how to write sql if that is an option for an answer.
Rather than use a table for your dimension, use a view that has an inner join to the fact table

Is it possible to provide USERPRINCIPALNAME in a Calculated Column?

I have a requirement in my project where I have to concatenate the e-mail ID of the report user in a column.
The Column where this has to be done is a web-link that redirects to a different application.
So for eg: the data in the columns is "**www.abc.html?/powerbi**".
My result should be "**www.abc.html?/powerbi,emailid#company.com**"
I want to get the email id of the user who is accessing he report.
I am not able to use USERPRINCIPALNAME() in calculated columns. Is there any alternate way to achieve this ?
Thanks in Advance.
Best Regards,
Pratik
Columns are created at refresh time, and it would not make sense to include things like USERPRINCIPALNAME in a column.
Measures are calculated at query time, and can contain these things. You could try to incorporate this in a measure instead.
You can concatenate the value of userprincipal() into a text field with a DAX query in a measure. An example of this is below:
WebUrl = www.abc.html?/powerbi,"&Userprincipal()&"/somefolder/"&FirstNonBlank(Table[ConcatText],Table[ConcatText]=Table[ConcatText])
If you need it with specific text from another field, you can force the text from the line by adding formula like FirstNonBlank() as below:
WebUrl2 = www.abc.html?/powerbi,"&Userprincipal()&FirstNonBlank(Table[TableName],TableName[Text]=TableName[Text])

Is there SQL function to update table by replacing one column with data (DIFFERENT TYPE) from another column (in a related table)?

I run into a problem in SQL, I don't know how to update table A by replacing its column "Edu" with another column "Level" of table B.
Two tables have a relationship in column "Edu"
Different data types bt "Edu" (number) & "Level" (Short Text)
Can anyone explain why the below code does not work? And kindly suggest a solution. Thanks!
UPDATE
A
SET
A.Edu= CAST(B.Level AS Varchar(Max))
FROM
A INNER JOIN B ON A.Edu=B.Edu;
Maybe an idea.
I think your query doesnt work because you try to update a key using a the same time this key, A.Edu.
My solution :
create in the table A a column EduBis with same value as Edu.
Then update with A.EduBis=B.Edu in you clause in the join.

Relationships break when adding column using query

I have a dataset with multiple tables and relationships between those tables which were auto detected when I connected to my PostgreSQL server.
But when I add a column using query, those relationships are no longer effective in the report view and all my graphs show 'blank' labels.
One thing I noticed is that in the Data view, the uuid which are used to make the relationships (my foreign keys in PSQL) appear with brackets and those brackets desappear after I add columns in query.
Before:
After:
I don't know if this helps.
I have tried adding columns with custom queries or simply duplicating an existing column.
I don't have any issue when adding columns using DAX.
Thanks,
Change the type of the column by yourself as one of the first steps in the query editor. You can either go with the brackets or without. Make sure its the same for every other key-/foreign key.
If you lost your relationship you can edit it by yourself. After your ID´s are proparly formatted go to Model (the last on the left pane) and drag and drop your relationship form one table to the other. This way it should work again.

Grouping by multiple columns and aggregating all values

I am rather new to Power Bi and I have a question i can't find the answer to.
I want to import a table that have some label columns, with repeated items, and more than 15 data columns.
My desire result would be to group the label columns, so no repeated items, and aggregate the values of the remaining columns.
Is there a way to do that in PQ editor or DAX ?
I appreciate any help or direction you can give me!
A sample of the table (it's much bigger, with multiple values in the first three columns)
Table Sample
Thanks a lot
Edit: From that sample, the output y I want is the following
Output Sample
The thing is, there are many different values in the first columns, and i need to agreggate all the other values, keeping they column name (cause this info is already linked to other files).
Maybe the only way is to group by and add the columns, renaming them one by one?
I want to do this in a couple of files, so if you know of another way please let me know!
In your query designer import your table. Then go to Home > Group By and group like you want it, the same goes for the aggregations and thats it.
If you just want to remove row duplicates, just group all columns which you dont want to aggregate and the rest can be aggregated like you want it.