django Queryset exclude() multiple data - django

i have database scheme like this.
# periode
+------+--------------+--------------+
| id | from | to |
+------+--------------+--------------+
| 1 | 2018-04-12 | 2018-05-11 |
| 2 | 2018-05-12 | 2018-06-11 |
+------+--------------+--------------+
# foo
+------+---------+
| id | name |
+------+---------+
| 1 | John |
| 2 | Doe |
| 3 | Trodi |
| 4 | son |
| 5 | Alex |
+------+---------+
#bar
+------+---------------+--------------+
| id | employee_id | periode_id |
+------+---------------+--------------+
| 1 | 1 |1 |
| 2 | 2 |1 |
| 3 | 1 |2 |
| 4 | 3 |1 |
+------+---------------+--------------+
I need to show employee that not in salary.
for now I do like this
queryset=Bar.objects.all().filter(periode_id=1)
result=Foo.objects.exclude(id=queryset)
but its fail, how do filter employee list not in salary?...

Well here you basically want the foos such that there is no period_id=1 in the Bar table.
We can let this work with:
ex = Bar.objects.all().filter(periode_id=1).values_list('employee_id', flat=True)
result=Foo.objects.exclude(id__in=ex)

Related

How do i add additional rows in M QUERY

I want to add more rows using the Query editor (Power query/ M Query) in only the Start Date and End Date column:
+----------+------------------+--------------+-----------+-------------+------------+
| Employee | Booking Type | Jobs | WorkLoad% | Start Date | End date |
+----------+------------------+--------------+-----------+-------------+------------+
| John | Chargeable | CNS | 20 | 04/02/2020 | 31/03/2020 |
| John | Chargeable | CNS | 20 | 04/03/2020 | 27/04/2020 |
| Bernard | Vacation/Holiday | SN | 100 | 30/04/2020 | 11/05/2020 |
| Bernard | Vacation/Holiday | Annual leave | 100 | 23/01/2020 | 24/02/2020 |
| Bernard | Chargeable | Tech PLC | 50 | 29/02/2020 | 30/03/2020 |
+----------+------------------+--------------+-----------+-------------+------------+
I want to find the MIN(Start Date) and MAX(End Date) and then append the range of start to end dates to this table only in the Start Date and End Date column in the Query Editor (Power Query/ M Query). Preferrable if I can create another table2 duplicating the original table and append these rows.
For example:
+----------+------------------+--------------+-----------+-------------+------------+
| Employee | Booking Type | Jobs | WorkLoad% | Start Date | End date |
+----------+------------------+--------------+-----------+-------------+------------+
| John | Chargeable | CNS | 20 | 04/02/2020 | 31/03/2020 |
| John | Chargeable | CNS | 20 | 04/03/2020 | 27/04/2020 |
| Bernard | Vacation/Holiday | SN | 100 | 30/04/2020 | 11/05/2020 |
| Bernard | Vacation/Holiday | Annual leave | 100 | 23/01/2020 | 24/02/2020 |
| Bernard | Chargeable | Tech PLC | 50 | 29/02/2020 | 30/03/2020 |
| | | | | 23/01/2020 | 23/01/2020 |
| | | | | 24/01/2020 | 24/01/2020 |
| | | | | 25/01/2020 | 25/01/2020 |
| | | | | 26/01/2020 | 26/01/2020 |
| | | | | 27/01/2020 | 27/01/2020 |
| | | | | 28/01/2020 | 28/01/2020 |
| | | | | 29/01/2020 | 29/01/2020 |
| | | | | 30/01/2020 | 30/01/2020 |
| | | | | 31/01/2020 | 31/01/2020 |
| | | | | ... | ... |
| | | | | 11/05/2020 | 11/05/2020 |
+----------+------------------+--------------+-----------+-------------+------------+
The List.Dates function is pretty useful here.
Generate the dates in your range, duplicate that to two columns and then append.
let
StartDate = List.Min(StartTable[Start Date]),
EndDate = List.Max(StartTable[End Date]),
DateList = List.Dates(StartDate, Duration.Days(EndDate - StartDate), #duration(1,0,0,0)),
DateCols = Table.FromColumns({DateList, DateList}, {"Start Date", "End Date"}),
AppendDates = Table.Combine({StartTable, DateCols})
in
AppendDates

Find a string in the entire table (all fields, all columns, all rows) in Django

I have a module (table) in my Django app with 24 fields (columns), and I want to search a string in it. I want to see a list that show me which one of the rows has this string in its fields.
Please have a look at this example:
+-----+------+------+---------+------------+------------+------------+-----+-------------+
| id | name | year | country | attribute1 | attribute2 | attribute3 | ... | attribute20 |
+-----+------+------+---------+------------+------------+------------+-----+-------------+
| 1 | Tie | 1993 | USA | Bond | Busy | Busy | ... | Free |
+-----+------+------+---------+------------+------------+------------+-----+-------------+
| 2 | Ness | 1980 | Germany | Free | Busy | Both | ... | Busy |
+-----+------+------+---------+------------+------------+------------+-----+-------------+
| 3 | Both | 1992 | Sweden | Free | Free | Free | ... | Busy |
+-----+------+------+---------+------------+------------+------------+-----+-------------+
| ... | ... | ... | ... | ... | ... | ... | ... | ... |
+-----+------+------+---------+------------+------------+------------+-----+-------------+
| 24 | Lex | 2001 | Russia | Busy | Free | Free | ... | Both |
+-----+------+------+---------+------------+------------+------------+-----+-------------+
What I am looking to get (by using filters, etc.) is something like this: (When I filter the records base on the word "Both" in the entire table and all of the records. Each row that contains "Both" is in the result below)
+----+------+------+---------+------------+------------+------------+-----+-------------+
| id | name | year | country | attribute1 | attribute2 | attribute3 | ... | attribute20 |
+----+------+------+---------+------------+------------+------------+-----+-------------+
| 1 | Ness | 1980 | Germany | Free | Busy | Both | ... | Busy |
+----+------+------+---------+------------+------------+------------+-----+-------------+
| 2 | Both | 1992 | Sweden | Free | Free | Free | ... | Busy |
+----+------+------+---------+------------+------------+------------+-----+-------------+
| 3 | Lex | 2001 | Russia | Busy | Free | Free | ... | Both |
+----+------+------+---------+------------+------------+------------+-----+-------------+
You can see that the string ("Both") appears in different rows in different columns. (one "Both" is under the column "attribute3", the other "Both" is under column "Name", and the last "Both" is under column "attribute20".
How you get this result in Django by queryset?
Thanks
Assuming you have modeled the above table as a Django model named Person
from django.db.models import Q
query_text = "your search string"
Person.objects.filter(
Q(name__contains=query_text) |
Q(year__contains=query_text) |
Q(attribute1__contains=query_text)
and so on for all your attributes
)
The above code will do a case sensitie search. if instead you want it to be case insenssitive search, use name__icontains instead of say name__contains in the above code.
As suggested by #rchurch4 in comment and based on this so answer, here's how one could search the entire table with fewer lines of code:
from functools import reduce
from operators import or_
all_fields = Person._meta.get_fields()
search_fields = [i.name for i in all_fields]
q = reduce(or_, [Q(**{'{}__contains'.format(f): search_text}) for f in search_fields], Q())
Person.objects.filter(q)

Calculate Total by specific column, then apply column total to each row

I would like to apply the total count('case_id'), while grouping by the Item.
This was my previous ask DAX Measure to calculate aggregate data, but group by Case ID. This gave me the total count('case_id') by sub_item.
Measure =
VAR datesSelection =
DATE(
YEAR(SELECTEDVALUE('Date Selection'[DateWoTime]))
,MONTH(SELECTEDVALUE('Date Selection'[DateWoTime]))
,DAY(SELECTEDVALUE('Date Selection'[DateWoTime]))
)
VAR devicesTotal =
CALCULATETABLE (
VALUES ( Outages[Sub_Item] ),
ALLSELECTED ( Outages ),
Outages[DATE] >= datesSelection,
VALUES ( Outages[Sub_Item] )
)
var counts =
CALCULATE (
COUNT( Outages[CASE_ID] ),
ALLSELECTED( Outages ),
Outages[Sub_Item] IN devicesTotal
)
return
counts
I'm getting this.
| Item | Sub_Item | TYPE | Case ID | Date | Measure |
|-------|----------|------|------------|------------------|---------|
| 701ML | abc | TFUS | 1312937981 | 7/16/19 7:18:00 | 1 |
| 702ML | abc | TFUS | 1312958225 | 7/16/19 11:13:00 | 1 |
| 702ML | abc1 | TFUS | 1312957505 | 7/16/19 11:03:00 | 1 |
| 702ML | abc2 | TFUS | 1312954287 | 7/16/19 10:24:00 | 1 |
| 702ML | abc3 | TFUS | 1312938599 | 7/16/19 7:28:00 | 1 |
| 702ML | abc4 | TFUS | 1290599620 | 5/25/18 15:43:00 | 2 |
| 702ML | abc4 | TFUS | 1312950297 | 7/16/19 9:43:00 | 2 |
| 708BI | abc | TFUS | 1312947288 | 7/16/19 9:13:00 | 1 |
| 712BI | abc | TFUS | 1312944078 | 7/16/19 8:30:00 | 1 |
| 785DL | abc | TFUS | 1312937536 | 7/16/19 7:12:00 | 1 |
| 786DL | abc | TFUS | 1312992583 | 7/16/19 14:59:00 | 1 |
| 791DI | abc | LFUS | 1289094627 | 4/28/18 20:07:00 | 2 |
| 791DI | abc | LFUS | 1312958972 | 7/16/19 11:17:00 | 2 |
| 791DI | abc1 | LFUS | 1313005237 | 7/16/19 14:00:00 | 2 |
| 791DI | abc2 | RCLR | 1290324328 | 5/22/18 15:36:00 | 2 |
| 841JU | abc | TFUS | 1312955016 | 7/16/19 10:32:00 | 1 |
| 841JU | abc1 | SBKR | 1288688911 | 4/15/18 10:09:56 | 2 |
| 841JU | abc1 | SBKR | 1312961007 | 7/16/19 11:46:24 | 2 |
| 871NI | abc2 | TFUS | 1304308511 | 3/24/19 19:13:00 | 2 |
| 871NI | abc | TFUS | 1313015455 | 7/16/19 18:39:00 | 2 |
| 917CN | abc | TFUS | 1312945831 | 7/16/19 8:58:00 | 1 |
| 918CN | abc | LFUS | 1292611263 | 6/30/18 9:41:00 | 2 |
| 918CN | abc | LFUS | 1313006283 | 7/16/19 17:03:00 | 2 |
| 922DU | abc | TFUS | 1312987081 | 7/16/19 14:20:00 | 1 |
| 922DU | abc1 | TFUS | 1313005803 | 7/16/19 17:04:00 | 1 |
| 922DU | abc2 | TFUS | 1313003541 | 7/16/19 16:42:00 | 1 |
| 931LF | abc | TFUS | 1312972165 | 7/16/19 12:46:00 | 1 |
When I would like to get this.
| Item | Sub_Item | TYPE | Case ID | Date | Measure |
|-------|----------|------|------------|-----------------|---------|
| 701ML | abc | TFUS | 1312937981 | 7/16/2019 7:18 | 1 |
| 702ML | abc | TFUS | 1312958225 | 7/16/2019 11:13 | 6 |
| 702ML | abc1 | TFUS | 1312957505 | 7/16/2019 11:03 | 6 |
| 702ML | abc2 | TFUS | 1312954287 | 7/16/2019 10:24 | 6 |
| 702ML | abc3 | TFUS | 1312938599 | 7/16/2019 7:28 | 6 |
| 702ML | abc4 | TFUS | 1290599620 | 5/25/2018 15:43 | 6 |
| 702ML | abc4 | TFUS | 1312950297 | 7/16/2019 9:43 | 6 |
| 708BI | abc | TFUS | 1312947288 | 7/16/2019 9:13 | 1 |
| 712BI | abc | TFUS | 1312944078 | 7/16/2019 8:30 | 1 |
| 785DL | abc | TFUS | 1312937536 | 7/16/2019 7:12 | 1 |
| 786DL | abc | TFUS | 1312992583 | 7/16/2019 14:59 | 1 |
| 791DI | abc | LFUS | 1289094627 | 4/28/2018 20:07 | 4 |
| 791DI | abc | LFUS | 1312958972 | 7/16/2019 11:17 | 4 |
| 791DI | abc1 | LFUS | 1313005237 | 7/16/2019 14:00 | 4 |
| 791DI | abc2 | RCLR | 1290324328 | 5/22/2018 15:36 | 4 |
| 841JU | abc | TFUS | 1312955016 | 7/16/2019 10:32 | 3 |
| 841JU | abc1 | SBKR | 1288688911 | 4/15/2018 10:09 | 3 |
| 841JU | abc1 | SBKR | 1312961007 | 7/16/2019 11:46 | 3 |
| 871NI | abc2 | TFUS | 1304308511 | 3/24/2019 19:13 | 2 |
| 871NI | abc | TFUS | 1313015455 | 7/16/2019 18:39 | 2 |
| 917CN | abc | TFUS | 1312945831 | 7/16/2019 8:58 | 1 |
| 918CN | abc | LFUS | 1292611263 | 6/30/2018 9:41 | 2 |
| 918CN | abc | LFUS | 1313006283 | 7/16/2019 17:03 | 2 |
| 922DU | abc | TFUS | 1312987081 | 7/16/2019 14:20 | 3 |
| 922DU | abc1 | TFUS | 1313005803 | 7/16/2019 17:04 | 3 |
| 922DU | abc2 | TFUS | 1313003541 | 7/16/2019 16:42 | 3 |
| 931LF | abc | TFUS | 1312972165 | 7/16/2019 12:46 | 1 |
You need to specify what level you are aggregating at in your measure. Currently, you are aggregating at the Sub_Item level.
To aggregate at the Item level, simply replace Sub_Item with Item in your measure.

Create column to classify rows based on realted tables DAX PowerBI

I have simplified my problem to solve. Lets suppose I have three tables. One containing data and specific codes that identify objects lets say Apples.
+-------------+------------+-----------+
| Data picked | Color code | Size code |
+-------------+------------+-----------+
| 1-8-2018 | 1 | 1 |
| 1-8-2018 | 1 | 3 |
| 1-8-2018 | 2 | 2 |
| 1-8-2018 | 2 | 3 |
| 1-8-2018 | 2 | 2 |
| 1-8-2018 | 3 | 3 |
| 1-8-2018 | 4 | 1 |
| 1-8-2018 | 4 | 1 |
| 1-8-2018 | 5 | 3 |
| 1-8-2018 | 6 | 1 |
| 1-8-2018 | 6 | 2 |
| 1-8-2018 | 6 | 2 |
+-------------+------------+-----------+
And i have two related helping tables to help understand the codes (their relationships are inactive in the model due to ambiguity with other tables in the real case).
+-----------+--------+
| Size code | Size |
+-----------+--------+
| 1 | Small |
| 2 | Medium |
| 3 | Large |
+-----------+--------+
and
+------------+----------------+-------+
| Color code | Color specific | Color |
+------------+----------------+-------+
| 1 | Light green | Green |
| 2 | Green | Green |
| 3 | Semi green | Green |
| 4 | Red | Red |
| 5 | Dark | Red |
| 6 | Pink | Red |
+------------+----------------+-------+
Lets say that I want to create an extra column in the original table to determine which apples are class A and class B given that medium green Apples are class A and large Red apples are class B, the other remain blank as the example below.
+-------------+------------+-----------+-------+
| Data picked | Color code | Size code | Class |
+-------------+------------+-----------+-------+
| 1-8-2018 | 1 | 1 | |
| 1-8-2018 | 1 | 3 | |
| 1-8-2018 | 2 | 2 | A |
| 1-8-2018 | 2 | 3 | |
| 1-8-2018 | 2 | 2 | A |
| 1-8-2018 | 3 | 3 | |
| 1-8-2018 | 4 | 1 | |
| 1-8-2018 | 4 | 1 | |
| 1-8-2018 | 5 | 3 | B |
| 1-8-2018 | 6 | 1 | |
| 1-8-2018 | 6 | 2 | |
| 1-8-2018 | 6 | 2 | |
+-------------+------------+-----------+-------+
What's the proper DAX to use given the relationships are initially inactive. Preferably solvable without creating any further additional columns in any table. I already tried codes like:
CALCULATE (
"A" ;
FILTER ( 'Size Table' ; 'Size Table'[Size] = "Medium");
FILTER ( 'Color Table' ; 'Color Table'[Color] = "Green")
)
And many variations on the same principle
Given that the relationships are inactive, I'd suggest using LOOKUPVALUE to match ID values on the other tables. You should be able to create a calculated column as follows:
Class =
VAR Size = LOOKUPVALUE('Size Table'[Size],
'Size Table'[Size code], 'Data Table'[Size code])
VAR Color = LOOKUPVALUE('Color Table'[Color],
'Color Table'[Color code], 'Data Table'[Color code])
RETURN SWITCH(TRUE(),
(Size = "Medium") && (Color = "Green"), "A",
(Size = "Large") && (Color = "Red"), "B", BLANK())
If your relationships are active, then you don't need the lookups:
Class = SWITCH(TRUE(),
(RELATED('Size Table'[Size]) = "Medium") &&
(RELATED('Color Table'[Color]) = "Green"),
"A",
(RELATED('Size Table'[Size]) = "Large") &&
(RELATED('Color Table'[Color]) = "Red"),
"B",
BLANK())
Or a bit more elegantly written (especially for more classes):
Class =
VAR SizeColor = RELATED('Size Table'[Size]) & " " & RELATED('Color Table'[Color])
RETURN SWITCH(TRUE(),
SizeColor = "Medium Green", "A",
SizeColor = "Large Red", "B",
BLANK())

Count same field values in Django queryset

I have a Django model with three fields: product, condition and quantity with data such as:
| Product | Condition | Quantity |
+---------+-----------+----------+
| A | new | 2 |
| A | new | 3 |
| A | new | 4 |
| A | old | 1 |
| A | old | 2 |
| B | new | 2 |
| B | new | 3 |
| B | new | 1 |
| B | old | 4 |
| B | old | 2 |
I'd like to sum the quantities of the entries where product and condition are equal:
| Product | Condition | Quantity |
+---------+-----------+----------+
| A | new | 9 |
| A | old | 3 |
| B | new | 6 |
| B | old | 6 |
This answer helps to count entries with the same field value, but I need to count two fields.
How could I implement this?
from django.db.models import Sum
Model.objects.values('product', 'condition').order_by().annotate(Sum('quantity'))