How to exclude values in django aggregation? - django

I have done this:
groups = group1.objects.filter(User=request.user, Company=company_details.pk, ledgergroups__Creation_Date__gte=selectdatefield_details.Start_Date, ledgergroups__Creation_Date__lte=selectdatefield_details.End_Date).exclude(group_Name__icontains='Capital A/c')
groups_cb = groups.annotate(
closing = Coalesce(Sum('ledgergroups__Closing_balance'), 0),
opening = Coalesce(Sum('ledgergroups__Balance_opening'), 0),
)
I want to perform aggregation in 'closing' and 'opening' by interchanging the negative values of annotation...
I mean the negative value which will come through annotation in 'closing', that value should be added with the aggregated value of 'opening'....
For example:
if the values of closing are 2500,5000,-8000
The total value will be 7500(aggregated value)
and -8000 will be added in the aggregated value of 'opening'.
Do anyone have any idea how to solve this?

You can use a Case and When within a Sum annotation:
Sum(
Case(
When(
another_model__field_value__lt=0,
then=F('another_model__field_value'),
),
default=F('another_model__other_field_value'),
output_field=IntegerField(),
)
)

Related

DAX FORMAT function cause cartesian product on Power BI visual

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, "# ### ###" ) )

Group by date, Distinct and ignore other filters - Power BI

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:

Percentage value of a segment against segment total Power BI(DAX)

Hi guys, I am new to Power BI(DAX formulas) and I am attempting to calculate the percentage contribution of the sum of "count" where "category" = X and "item_no"=1 to the total of "count" across all categories where 'item_no' = 1.
The ideal mathematical statement here will be the (30/50)*100%
I intend to represent the percentage values in a chart showing percentage contribution of each distinct item_no to its total in the format as represented in the example above.
The standard way to approach this is
calculation over partial set / same calculation over larger set
Since you haven't made it clear what context you are trying to calculate this, I will assume it's a visual along these lines:
The measure I use here is
%ItemTotal =
DIVIDE (
SUM ( Table1[count] ),
CALCULATE ( SUM ( Table1[count] ), ALLEXCEPT( Table1, Table1[item_no] ) )
)
In the numerator, you have the sum in the local filter context. For example, in that top-left cell, this means all rows that match item_no = 1 and category = "X".
In the denominator, we do the same thing except we remove all filter context except the context we say to keep (item_no) so it includes all category values.
If you're trying to calculate that 60% outside of the context of a visual, then you can explicitly define what filters you want. For example, this should work in any filter context:
X%_Item1 =
DIVIDE (
CALCULATE (
SUM ( Table1[count] ),
ALL ( Table1 ),
Table1[category] = "X",
Table1[item_no] = 1
),
CALCULATE (
SUM ( Table1[count] ),
ALL ( Table1 ),
Table1[item_no] = 1
)
)
See here and here for other ways to modify the filter context instead of ALLEXCEPT.

How to perform a conversion based on a given value using dax

I'm trying to create a conversion based on distinct column values(emails) divided by number of transactions(clients) carried out.
I've tried pointing out a formula to count email in the column.
emailConversionRate =
DIVIDE (
DISTINCTCOUNT (
ClientTransView[TransEntityID]
),
Count( Interactions[chaneltype='email'
)
)
I expect the output to be the number of entity IDs divided by the channel type used.
You could try using CALCULATE function for filtering the chaneltype and use double quotes for email as it must be a string.
emailConversionRate =
DIVIDE (
DISTINCTCOUNT ( ClientTransView[TransEntityID] ),
CALCULATE (
COUNT ( Interactions[chaneltype] ),
Interactions[chaneltype] = "email"
)
)

Sum using in dax command

I am trying to do this expression but this shows an error:
vMotor_Paid =
CALCULATE (
Paid_excel,
SUM ( Paid_excel[PAID_AMOUNT] ),
Paid_excel[Flag] = "Paid",
Premium_Excel[LOB] = "MOTOR",
Paid_excel[PAID_DATE] = { [VpeDate] }
)
Error:
A function 'SUM' has been used in a True/False expression that is used as a table filter expression. This is not allowed.
In the expression, I am trying to do SUM of paid_amount against filters of these
Paid_excel[Flag]="Paid",
Premium_Excel[LOB]="MOTOR",
Paid_excel[PAID_DATE]={[VpeDate]}
Here I create another variable of VPeDate where I fixed the date "23-12-2017"
Any help?
I don't understand why your date is wrapped in curly braces. I think you might have more luck like this:
vMotor_Paid =
VAR VpeDate = [VpeDate]
RETURN
CALCULATE (
SUM ( Paid_excel[PAID_AMOUNT] ),
Paid_excel[Flag] = "Paid",
Premium_Excel[LOB] = "MOTOR",
Paid_excel[PAID_DATE] = VpeDate
)
Now that CALCULATE filter is comparing versus a date rather than an expression that returns a date.
vMotor_Paid = CALCULATE(SUM(Paid_excel[PAID_AMOUNT]),FILTER(Paid_excel[Flag]="Paid"
,Premium_Excel[LOB]="MOTOR",Paid_excel[PAID_DATE]={[VpeDate]}))
Please check the calculate syntax