I want to calculate number of group members (no_members), sum of the points of the group members(point) and average point per person(avg_point) for each group with annotation:
groups = StudyGroup.objects.filter(group_filter).select_related('parent').annotate(
no_members=Count('student', distinct=True),
point=Sum('student__point__point', filter=point_filter),
avg_point=ExpressionWrapper(F('point') / F('no_members'), output_field=FloatField()))
but when I check query (groups.query) in avg_point instead of use point/no_members query is SUM(study_league_point.point) / COUNT(DISTINCT users_student.user_id) (point and no_members calculate again). query is:
SELECT
`study_league_studygroup`.`id`,
`study_league_studygroup`.`name`,
`study_league_studygroup`.`parent_id`,
COUNT(DISTINCT `users_student`.`user_id`) AS `no_members`,
SUM(`study_league_point`.`point`) AS `point`,
(
SUM(`study_league_point`.`point`) / COUNT(DISTINCT `users_student`.`user_id`)
) AS `avg_point`,
`layers_layer`.`id`,
`layers_layer`.`name`,
`layers_layer`.`type_id`,
`layers_layer`.`parent_id`,
`layers_layer`.`created`,
`layers_layer`.`default`,
`layers_layer`.`lft`,
`layers_layer`.`rght`,
`layers_layer`.`tree_id`,
`layers_layer`.`level`
FROM
`study_league_studygroup`
LEFT OUTER JOIN `users_student` ON (
`study_league_studygroup`.`id` = `users_student`.`study_group_id`
)
LEFT OUTER JOIN `study_league_point` ON (
`users_student`.`user_id` = `study_league_point`.`student_id`
)
INNER JOIN `layers_layer` ON (
`study_league_studygroup`.`parent_id` = `layers_layer`.`id`
)
GROUP BY
`study_league_studygroup`.`id`
but I want use (point / no_members) AS avg_point instead of (SUM(study_league_point.point) / COUNT(DISTINCT users_student.user_id)) AS avg_point
Related
I have the following SSAS Tabular model defined:
On the Product table, I have the following measures defined:
DeliveryQty2018:= CALCULATE ( SUM ( PurchaseDelivery[PurchaseOrderQuantity] ), ( PurchaseEstimatedWarehouseArrivalDate[PurchaseEstimatedWarehouseArrivalYear] = 2018 ) )
DeliveryQty2019:= CALCULATE ( SUM ( PurchaseDelivery[PurchaseOrderQuantity] ), ( PurchaseEstimatedWarehouseArrivalDate[PurchaseEstimatedWarehouseArrivalYear] = 2019 ) )
Sum DeliveryQty 2018-2020: = FORMAT([DeliveryQty2018] + [DeliveryQty2019] + [DeliveryQty2020],"# ### ###")
I'm creating a table visual on my Power BI report, that consists of the following fields:
This combinations gives me a cartesian product of: Product X ProductCategory:
What's interesting, when I remove the FORMAT() wrapper for the Sum DeliveryQty 18-20, cartesian product is removed and I achieve the single record I was loooking for. However, if I remove the ProductCategory field and leave the Sum DeliveryQty 18-20 measure with the FORMAT() function in place I also get the single record..
Can anyone explain to me what's going here in both scenarios?
FORMAT turns blanks (nulls) into empty strings "" rather than proper blanks, so you probably want to check for that first before formatting.
Sum DeliveryQty 2018-2020: =
VAR Qty = [DeliveryQty2018] + [DeliveryQty2019] + [DeliveryQty2020]
RETURN
IF ( ISBLANK ( Qty ), BLANK(), FORMAT ( Qty, "# ### ###" ) )
I have the dataset where the values in the col 'value' are repeated per month and id, e.g. for 1/1/2020 and id 1, the value is 0.5, for 2/1/2020, the value is 2, etc. The dataset has other columns which are to be used as filters for other calculations.
What will be the measure to get:
so that even when I use filters from the table, e.g. filter1, the value remains grouped by date ONLY?
I've tried with sumx and max; sum and value but nothing gives a result and calculation still reacts on other filters.
When i spoke abount ALL i had in mind this kind of solution:
WithoutExternalFilter =
CALCULATE (
VAR __dist =
ADDCOLUMNS (
SUMMARIZE ( Te, Te[Date], Te[ID] ),
"val", CALCULATE ( MAX ( Te[value] ) )
)
RETURN
SUMX ( __dist, [val] ),
ALL ( Te[filter1] )
)
WHEN we put some filter, value for "2020-08-01" is still 1.1:
good afternoon.
I made a pareto chart in the top 10 states, but when I apply a filter to the data slice the measure of "cumulative goals" falls apart.
correct:
Wrong:
For the measures used:
Goals =
CALCULATE (
SUM ( 'All domains - main data'[Value] );
FILTER (
'All domains - main data';
'All domains - main data'[Attribute] <> "Goal Completions"
)
)
Classification =
RANKX ( ALL ( 'All domains - main data'[Region] ); [Objectives];; DESC )
Cumulative goals =
CALCULATE (
[Goals];
TOPN (
[Classification];
ALL ( 'All domains - main data'[Region] );
[Goals]; DESC
)
)
Total =
CALCULATE (
[Goals];
ALLEXCEPT ( 'All domains - main data'; 'All domains - main data' )
)
% Pareto = [Cumulative targets] / [Total]
I'm not positive this will work, but try this for Cumulative goals.
Cumulative goals =
SUMX (
TOPN (
[Classification];
ALL ( 'All domains - main data'[Region] );
[Goals]; DESC
);
[Goals]
)
Using TOPN as a filter table inside CALCULATE is a bit odd.
I have the following formula which creates the table in the screenshot below on the left (names of actual tables are different - also it combines 2 separate tables in one) -
Top 11 Jun =
IF (
[Type Rank Jun] <= 11,
[Total Jun],
IF (
HASONEVALUE ( Partners[partner_group] ),
IF (
VALUES ( Partners[partner_group] ) = "Others",
SUMX (
FILTER ( ALL ( Partners[partner_group] ), [Type Rank Jun] > 11 ),
[Total Jun]
)
)
)
)
Now i'm stuck on how to combine the "Null" and "Others" under "Others" and put "Others" at the bottom.i can combine the "Null" & "Others" at each table level, i'm just not sure how.
The DAX solution:
To get the Other and blank (at least that is how I read your null) together, you can create a new column on the table (is easiest).
newProducts = IF(fruits[product] = BLANK(); "Other";fruits[product])
A better solution is to replace your blanks (or NULL) in the Query language:
Go to: Edit Query:
Select your table and the product column and press on the bar the "Replace values"
Do the replace and save and close the editor.
Last step
It is not relevant in which order you have the rows in the table because you can control this in the visual self.
Below example:
As you can see, I filtered other out, this is not needed when you want to count them in your top N.
If you want to show all four, we need to make a new Table:
Tabel =
var Top3 = TOPN(3;FILTER(fruits;fruits[product] <> "Other") ;fruits[July Avail])
var prioTop3 = ADDCOLUMNS(Top3;"Order"; CALCULATE(COUNTROWS(fruits);FILTER(Top3; fruits[July Avail] <= EARLIER(fruits[July Avail]))))
var Other = ADDCOLUMNS(GROUPBY(FILTER(fruits;fruits[product] = "Other");fruits[product];"June Avail"; SUMX(CURRENTGROUP();fruits[June Avail]); "July Avail"; SUMX(CURRENTGROUP();fruits[July Avail]));"Order";0)
return UNION(prioTop3; Other)
Result:
How do I do this in doctrine2 QB or DQL.
SELECT * FROM
(
select * from my_table order by timestamp desc
) as my_table_tmp
group by catid
order by nid desc
I think your query is the same as:
SELECT *
FROM my_table
GROUP BY catid
HAVING timestamp = MAX(timestamp)
ORDER BY nid DESC
;
If it is correct, then you should be able to do:
$qb->select('e')
->from('My\Entities\Table', 'e')
->groupBy('e.catid')
->having('e.timestamp = MAX(e.timestamp)')
->orderBy('nid', 'DESC')
;
Or, directly using DQL:
SELECT e
FROM My\Entities\Table e
GROUP BY e.catid
HAVING e.timestamp = MAX(e.timestamp)
ORDER BY e.nid DESC
;
Hope this helps and works! ;)