I have two tables as shown below :
country
id name
1 A
2 b
3 c
state
id | country_id | name | population
1 | 1 | x | 234354
2 | 1 | y | 2334
3 | 2 | h | 232323
4 | 2 | E | 8238787
Now I want query with sum population with country name like this :
a has xxxx population
b has xxxx population
c has 0 population
In django query, I have write this query :
City.objects.values('country__name').annotate(Sum('population'))
But this has not display 0 for c country :(
The query is not showing any record for c country, because table does not have any record for c country.
City.objects.values('country__name').annotate(Sum('population'))
This query will show any those records which are there in the City Model.
Related
I am new to Power BI and need some help regarding the Power BI RANKX function
So the scenario is I have Emp name and emp_id fields in my employee table and I have the office_distance and emp id column from the office table.
I want to create a column in my visual which contains ranking on the distance and name basis.
Example:
| EmpName | off_dist | Rank |
|-------- |----------|------|
| A | 10 | 1 |
| A | 20 | 2 |
| A | 30 | 3 |
| B | 20 | 3 |
| B | 10 | 1 |
| B | 15 | 2 |
Please let me know how can I achieve this
Please refer to below DAX formulas for calculated columns. And if this post helps, then please consider Accept it as the solution to help the other members find it more quickly.
Create a column to rank by the Employee Name
Rank EName = RANKX(ALL(Table), U381[Table], ,ASC, Dense)
Create another column to rank by Employee Name and by the Office Distance.
Ranking on EName and Distance =
VAR X = MAX('Table'[Rank E_Name])
var result =
RANKX(
ALL(Table),
Table[O_Distance] * X + Table[Rank E_Name], ,
ASC
)
RETURN
result
I am trying to merge two tables. table A has an id column, a date column, and an amount value for every date in a period
Table B has both id and date, but also other columns with details. However, there is only one entry any time there is a change in the details, so I do not know how to merge with normal joins. I want that for every entry in A, the details are populated as of the latest day available in B for that ID before the date in A.
Table A
| ID | date | amount |
| 1 | 01JAN| 56 |
| 1 | 02JAN| 54 |
| 1 | 03JAN| 23 |
| 1 | 04JAN| 43 |
Table B
| ID | date | details|
| 1 | 01JAN| x |
| 1 | 03JAN| y |
Wanted Output
Table A
| ID | date | amount | details |
| 1 | 01JAN| 56 | x |
| 1 | 02JAN| 54 | x |
| 1 | 03JAN| 23 | y |
| 1 | 04JAN| 43 | y |
for the jan2 entry, the latest available details as of that date is 'x', for jan3 it is y
Thank you in advance for any guidance you could provide
This will work for the question you have asked literally:
data want;
retain details_last;
merge table1 table2;
by ID date;
if not missing(details) then details_last = details;
else details = details_last;
drop details_last;
run;
But this will only work if your data meets the conditions that you have presented like the date ranges in table B should always fall within the date ranges in table A and not outside (i.e. only interpolation, no extrapolation).
I am trying to compare to sets of data that are very similar. I have done a bridge relation and used M:M relationship on PowerBI but I am still not getting the result I want.
Here is an example of the data:
Dataset 1
Name | Service | Usage
A | 1 | 10
A | 2 | 20
B | 1 | 10
B | 2 | 10
C | 1 | 20
C | 2 | 10
Dataset 2
Name | Service | Usage
A | 1 | 40
A | 2 | 20
B | 1 | 40
B | 2 | 10
C | 1 | 40
C | 2 | 10
Desired output
Name | Service | Usage 1 | Usage 2
A | 1 | 10 | 40
A | 2 | 20 | 20
B | 1 | 10 | 40
B | 2 | 10 | 10
C | 1 | 20 | 40
C | 2 | 10 | 10
Is this possible in PowerBI?
One approach (as suggested in comments), is to separate the distinct Name and Service values into separate dimension tables, in the query editor:
Names:
= Table.FromList(List.Distinct(List.Combine({#"Dataset 1"[Name], #"Dataset 2"[Name]})),Splitter.SplitByNothing(),{"Name"})
Services:
= Table.FromList(List.Distinct(List.Combine({#"Dataset 1"[Service], #"Dataset 2"[Service]})),Splitter.SplitByNothing(),{"Service"})
Create the DAX measures you want:
Usage 1 = SUM ( 'Dataset 1'[Usage] )
Usage 2 = SUM ( 'Dataset 2'[Usage] )
Now create relationships between the fact tables (Dataset 1, Dataset 2) and the dimension tables (Names, Services):
Then simply layout the visual as required:
Another approach may be to combine your dataset fact tables into one table, with an added "dataset" column:
Create your "combined" table in the query editor.
Combined Table:
= Table.Combine({Table.AddColumn(#"Dataset 1", "Dataset", each "Dataset 1", type text), Table.AddColumn(#"Dataset 2", "Dataset", each "Dataset 2", type text)})
Now use this table as your single source - either with a crosstab visual:
Or by adding separate measure for each dataset:
Usage 1 = CALCULATE ( SUM('Combined Data'[Usage]), 'Combined Data'[Dataset] = "Dataset 1" )
Usage 2 = CALCULATE ( SUM('Combined Data'[Usage]), 'Combined Data'[Dataset] = "Dataset 2" )
I have the following code to count days:
count_days:
DATEDIFF('day',LOOKUP(ATTR([visit_day]),-1),ATTR([visit_day])
and the outcome is the following:
customer_id | visit_day | count_days
customer 1 | 1/1/2016 |
customer 2 | 1/3/2016 | 2
customer 2 | 1/15/2016 | 12
customer 3 | 2/1/2016 | 16
customer 3 | 2/5/2016 | 4
I'm trying to write another function that fires the previous function only by customer, so the first day of one customer is not subtracted by the last day of the previous customer. The outcome should look like this:
customer_id | visit_day | count_days
customer 1 | 1/1/2016 |
customer 2 | 1/3/2016 |
customer 2 | 1/15/2016 | 12
customer 3 | 2/1/2016 |
customer 3 | 2/5/2016 | 4
I have tried this code so far:
IF ATTR([customer_id]) != ATTR([customer_id])
THEN
NULL
ELSE
[count_days]
END
Any thoughts about this code to make it work?
You are very close. You can alter your calculated field like so:
IF LOOKUP(ATTR([Customer Id]),-1) = ATTR([Customer Id]) THEN
DATEDIFF("day",LOOKUP(ATTR([Visit Day]),-1),ATTR([Visit Day]))
END
It will render this result:
I want to execute this query of three models/tables:
-- get all items that don't have a status for this member
SELECT i.*
FROM
mock_item i
LEFT JOIN mock_itemstatusmember ism ON i.id = ism.item_id AND ism.member_id = 2
WHERE
ism.id IS NULL
Here are how my models look:
from django.db import models
# think status as a status
class Member(models.Model):
id = models.AutoField(primary_key=True)
name = models.CharField(max_length=255)
class Status(models.Model):
id = models.AutoField(primary_key=True)
name = models.CharField(max_length=255)
no = models.SmallIntegerField()
class Item(models.Model):
id = models.AutoField(primary_key=True)
name = models.CharField(max_length=255)
status = models.ManyToManyField(Status, through='ItemStatusMember')
member = models.ManyToManyField(Member, through='ItemStatusMember')
class ItemStatusMember(models.Model):
"""
Table that keeps job statuss information for a user.
Tides Items with Members with Statuss
"""
id = models.AutoField(primary_key=True)
member = models.ForeignKey(Member)
status = models.ForeignKey(Status)
item = models.ForeignKey(Item)
And their database sample data:
mysql> select * from mock_member;
+----+--------+
| id | name |
+----+--------+
| 1 | Stefan |
| 2 | Alex |
| 3 | Diana |
+----+--------+
3 rows in set (0.00 sec)
mysql> select * from mock_status;
+----+---------+----+
| id | name | no |
+----+---------+----+
| 1 | Pending | 1 |
| 2 | Success | 2 |
+----+---------+----+
2 rows in set (0.00 sec)
mysql> select * from mock_item;
+----+----------------+
| id | name |
+----+----------------+
| 1 | My first item |
| 2 | My second item |
| 3 | My third item |
+----+----------------+
3 rows in set (0.00 sec)
mysql> select * from mock_itemstatusmember;
+----+-----------+-----------+---------+
| id | status_id | member_id | item_id |
+----+-----------+-----------+---------+
| 1 | 1 | 1 | 1 |
| 2 | 2 | 1 | 2 |
| 3 | 1 | 2 | 1 |
+----+-----------+-----------+---------+
3 rows in set (0.00 sec)
I need this, with the ORM, and NOT with the raw SQL query facility from the ORM to get all items that don't have a status for this member.
The SQL query at the beginning of the question does this, so it's important to have the
AND ism.member_id = 2
condition as a second condition to the left-join part, instead of moving into where, which I know how to do.
from django.db.models import Q
Item.objects.filter(~Q(member__id=2))