How do I change the individual value to be summed during annotation in Django? - django

I am front end developer new to django. There is a certain column(server_reach) in our postgres DB which has values of (1,2). But I need to write a query which tells me if at least one of the filtered rows has a row with reachable values( 1= not reachable, 2 = reachable).
I was initially told that the values of the column would be (0,1) based on which I wrote this:
ServerAgent.objects.values('server').filter(
app_uuid_url=app.uuid_url,
trash=False
).annotate(serverreach=Sum('server_reach'))
The logic is simple that I fetch all the filtered rows and annotate them with the sum of the server_reaches. If this is more than zero then at least one entry is non-zero.
But the issue is that the actual DB has values (1,2). And this logic will not work anymore. I want to subtract the server_reach of each row by '1' before summing. I have tried F expressions as below
ServerAgent.objects.values('server').filter(
app_uuid_url=app.uuid_url,
trash=False
).annotate(serverreach=Sum(F('server_reach')-1))
But it throws the following error. Please help me getting this to work.
AttributeError: 'ExpressionNode' object has no attribute 'split'

Use Avg instead of Sum. If average value is greater than 1 then at least one row contains value of 2.

Related

Power BI function that checks if multiple specified values (numbers) exist in a column

Is there a function in Power BI that can check whether a list of specified values (numbers) exists in a column?
For example, in the image below, I have a column with some values and another one with 0 and 1s. You can see that some values are marked with 1 and some with 0. In order to do this, I used IF function, but this is just too cumbersome.
I am looking for a formula that can check if the values from a list like {XXXX, XXXX, XXXX, etc} exist in a column and that can easily be edited when I need to add other values.
Thank you and have a good day!
Best,
Denis
You can do that by adding a custom column for example. If we assume your table is named Table and first column is named Value, then add a custom column like this:
Where the list contains all the values of interest. This will give you a boolean column Flag:
If you want an integer column with 0 and 1 values, then change the column to something like this:
= if (List.Contains({"5006", "4905"}, [Value])) then 1 else 0

Failing if condition in Power BI

While I am trying to create a new measure, with condition, I am facing the below error.
I am trying to create a dynamic line based on dates on my line graph. For my case, if Sheet[Date] matches to Test[Date] it should return me a value of 100.
But the error says that a single value for column 'Date' in table Sheet1 cannot be determined.
I also tried to convert the date to week with Weeknum function. But the error still persists.
Does it mean that I can not compare single values in IF condition
If there is a way to compare the dates, kindly guide me.
Sheet[Date] and Test[Date] are columns, not single values.
You need to specify which single values from those columns to compare. For example, you could compare the maximal values of those columns (within the local filter context):
MAX ( Sheet[Date] ) = MAX ( Sheet[Date] )

Comparing numbers in Calculated Column formula, inside a simple OR Condition then IF Condition is not returning Correct Value in SharePoint

Simply trying to compare numbers in formula for calculated Column Total Days in Route. The numbers to be compared are returned by subtraction like TODAY()-[CurrentRouteDateTemp] where the column CurrentRouteDateTemp is of type Date
See column descr
Why is it still just returning the value from the main calculation i.e. TODAY()-[CurrentRouteDateTemp] Why the rest of the condition not working????
See list view
Try to add value function in the formula, like this:
or(value(TODAY()-[CurrentRouteDateTemp])=43725,value(TODAY()-[CurrentRouteDateTemp])<0)

Power BI remove duplicates based on max value

I have 2 column; ID CODE, value
Remove duplicates function will remove the examples with the higher value and leave the lower one. Is there any way to remove the lower ones? The result I expected was like this.
I've tried Buffer Table function before but it doesn't work. Seems like Buffer Table just works with date-related data (newest-latest).
You could use SUMMARIZE which can be used similar to a SQL query that takes a MIN value for a column, grouped by some other column.
In the example below, MIN([value]) is taken, given a new column name "MinValue", which is grouped by IDCode. This should return the min value for each IDCode.
NewCalculatedTable =
SUMMARIZE(yourTablename, yourTablename[IDCode], "MinValue", MIN(yourTablename[value]) )
Alternatively, if you want the higher values just replace the MIN function with MAX.

Combining formulas

Please help me with combining these two formulas:
REGEXP_EXTRACT([resolution],'(\d{2}:\d{2})')
IF CONTAINS([resolution],"closed") and NOT CONTAINS([resolution],"resolution") THEN 1 ELSE 0 END
Example:
I have lines
1.resolution(01:01) Completed repair equipments
2.resolution(01:02) Preventive maintenance
3.The problem is not current
4.resolution(01:03) Completed repair equipments. The problem is not current
I have 2 calculated field.
REGEXP_EXTRACT([resolution],'(\d{2}:\d{2})') - find numbers 01:01,01:02,01:03
IF CONTAINS([resolution],"closed") and NOT CONTAINS([resolution],"resolution") THEN 1 ELSE 0 END - excludes records:3.The problem is not current
I want to make a formula which combined two formulas and displays numbers:01:01,01:02,01:03
I assume you are trying to use these in a calculated field?
You should be able to pull the initial one into your second calculated field. So if you create your first calculated field(analysis->create calculated field) and lets call it "Calc 1". So this will have your initial formula:
REGEXP_EXTRACT([resolution],'(\d{2}:\d{2})')
Then create a second calculated field and call it "Calc 2". In this formula just update your original to call the first calculated field which contains your formula:
IF CONTAINS([Calc 1],"closed") and NOT CONTAINS([Calc 1],"resolution") THEN 1 ELSE 0 END