Django query annotate with applied distinct on another field - django

assume I have a db table with following rows:
some_key - price
1 | 100
1 | 100
2 | 150
2 | 150
3 | 100
3 | 100
I want to list users with their total expending according to orders table. but for whatever (stupid) reason each row may have been duplicated. so I should add distinct on "some_key" column and obviously this code bellow won't work.
how could I annotate Sum of prices with distinct on "some_key" column
query = User.objects.filter(<...>).annotate(price_sum=Sum("orders_set__price", distinct=True))
any suggestion?

Related

Summarize a table based on a column in another table

I have two tables in Power BI model
Table A
value1
value2
value3
....
value 1000
Table B
value 1 | 10
value 2 | 10
value 1 | 50
value 3 | 10
value 1 | -10
value 2 | 70
Can I make a new column (or measure) in Table A to Sum UP connected values ???
Expected RESULT:
value 1 | 50 --- (10+50-10)
value 2 | 80 --- (10+70)
value 3 | 10 --- (10)
Just something like SUM.IF in Excel, which can I drag to all rows ? Thanks in advance.
I tried to CALCULATE, but I can't do this for all different rows in Table A
You don't need Table A for this. SUMMARIZE() will create a column of distinct values to group by. Use the following Calculated Table. Note that this is NOT a MEASURE!
Result =
SUMMARIZE(
'Table B',
'Table B'[value ID],
"Sum", SUM('Table B'[number])
)
Yes, Possible ! Just Add a new column on your table, and write this DAX Code after relationship is created!

Power BI dynamic index table with filters

I try to get a dynamic index table in Power BI to later be used to sort a stacked area chart. Importantly, I need it to take current filters into account.
The idea is to get something like:
Product | Index
Product 1 | 1
Product 3 | 2
Product 4 | 3
Product 2 | 4
I can count product with filters a measure:
sum2 = COUNTX(table_name, [Product])
But unfortunately I can't sort Product column by a measure.
All examples I found have the names of the categories entered manually which is not what I'm looking for. Moreover, the filtering part is important to me.

PowerBI DAX - Filter Total based on selected columns

I have the table that looks like this:
Column Header: User | Test Score 1 | Test Score 2 | Test Score 3 | Total Points | Status
Row: Person1 | 50 | 70 | 75 | 195 | Failed
Row: Person2 | 70 | 75 | 85 | 210 | Passed
The "Total Points" column is simply the SUM of the three test scores and the "Status" is calculated based on the "Total Points" (if "Total Points" < 200, "Failed", "Passed").
What I'm having difficulty with is that sometimes a test needs to be eliminated from the equation. I would like the end user to be able to uncheck a box in the filters area or on a slicer to remove a test from the equation for the "Total Points", which would then affect the "Status". Since the test is a column header and not a value, I can't seem to find a way to make this work.
The data table is already in a report layout. That makes your intention difficult to process. A better layout would be a flat table in this format:
Name, Test number, Score
You can use Power Query to unpivot the data to get from your layout to the flat table. With a flat table, you can then build a measure for the total and add a slicer to the report where the test number can be selected.
Build a matrix visual with the Name in the rows and the test number in the columns, the measure for the total in the values. Then use the slicer to remove tests from the matrix at your discretion.

SQL Scalar Function values into power bi table

I have two SQL functions in Power BI
Each returns one number each (say fn_1 =3 fn_2 = 4). What I am trying to do is create a table that has 2 column 1 row with fn_1 as a colum and fn_2 as the 2nd column
|---------------------|------------------|
| fn_1 | fn_2 |
|---------------------|------------------|
| 3 | 4 |
|---------------------|------------------|
Tried duplicate, combine, aggregate, merge. They're all returning something different.
Thanks
EDIT: The only reason wanting to do that is so I can combine them into 1 row as 3/4 (which I can do fine with regular table) have both values (3 and 4 as 3/4) display on top of a report as 3/4 using a card.
For two values, I'd try some basic table constructions:
#table({"fn_1", "fn_2"}, {{fn_1, fn_2}})
or
Table.FromRecords({[fn_1 = fn_1, fn2 = fn_2]})
or
Table.FromColumns({{fn_1}, {fn_2}}, {"fn_1", "fn_2"})

Split data into categories in the same row in Power BI

I have a table that contains multiple columns with their named having either the suffix _EXPECTED or _ACTUAL. For example, I'm looking at my sold items from my SoldItems Table and I have the following columns: APPLES_EXPECTED, BANANAS_EXPECTED, KIWIS_EXPECTED, APPLES_ACTUAL, BANANAS_ACTUAL, KIWIS_ACTUAL (The Identifier of the table is the date, so we have results per date). I want to show that data in a table form, something like this (for a selected date in filters:
+------------+----------+--------+
| Sold items | Expected | Actual |
+------------+----------+--------+
| Apples | 10 | 15 |
| Bananas | 8 | 5 |
| Kiwis | 2 | 1 |
+------------+----------+--------+
How can I manage something like this in Power BI ? I tried playing with the matrix/table visualization, however, I can't figure out a way to merge all the expected and actual columns together.
It looks like the easiest option for you would be to mould the data a bit differently using Power query. You can UNPIVOT your data so that all the expected and actual values become rows instead of columns. For example take the following sample:
Date Apples_Expected Apples_Actual
1/1/2019 1 2
Once you unpivot this it will become:
Date Fruit Count
1/1/2019 Apples_Expected 1
1/1/2019 Apples_Actual 2
Once you unpivot, it should be fairly straightforward to get the view you are looking for. The following link should walk you through the steps to unpivot:
https://support.office.com/en-us/article/unpivot-columns-power-query-0f7bad4b-9ea1-49c1-9d95-f588221c7098
Hope this helps.