Get documents between two dates - arangodb

In my collection I have some documents and one of their values is the created_on key, that is filled by timestamp at the creation of the document.
I want to retrieve the documents created between two dates, but I can't get it in a simple way, i have the next:
FOR d IN mycollection
FILTER '2021-12-01' <= DATE_FORMAT(d.created_on, "%yyyy-%mm-%dd") <= '2021-12-05'
SORT d.created_on ASC
RETURN DATE_FORMAT(d.created_on, "%yyyy-%mm-%dd")
but the above query returns all the records, not only the documents that are in the specified time period.
Any suggestions? Thanks in advance!

The problem is your filter expression '2021-12-01' <= DATE_FORMAT(d.created_on, "%yyyy-%mm-%dd") <= '2021-12-05'
This is basically the same as
LET x = '2021-12-01' <= DATE_FORMAT(d.created_on, "%yyyy-%mm-%dd")
FILTER x <= '2021-12-05'
x is a bool and as such always compares less than a string.
You should rewrite your query as follows:
FOR d IN mycollection
LET date = DATE_FORMAT(d.created_on, "%yyyy-%mm-%dd")
FILTER '2021-12-01' <= date AND date <= '2021-12-05'
SORT d.created_on ASC
RETURN DATE_FORMAT(d.created_on, "%yyyy-%mm-%dd")
This should filter correctly, but you won't be able to utilize any indexes on created_an for the FILTER, only for the SORT. So instead it would be better to write the query as follows:
LET from = DATE_TIMESTAMP('2021-12-01')
LET to = DATE_TIMESTAMP('2021-12-05')
FOR d IN mycollection
FILTER from <= d.created_on AND d.created_on <= to
SORT d.created_on ASC
RETURN DATE_FORMAT(d.created_on, "%yyyy-%mm-%dd")

Related

Formula to return value from a range will not work in a DAX measure, but works in a DAX Column

This is driving me nuts. I have never encountered this before.
I have a table (Excel Based called 'Metric Ranges' That contains a series of Metrics and their scoring range values (Lower Limit and Upper Limit).
Metric Values Table
I need to create a measure that will pass in the the Metric's ID, and Value into this table and return the score based on the ranges. The formula below works fine in a table column and returns the Score, but for some reason as a Measure, it will not return the value. When I test and return all the VAR's I get values returned, so I know it's not the values there, it is the actual Calculation and that is not working. The Score returned is a blank value.
Net Savings Year to date =
VAR MetricValue = [Savings % Annualize Goal]
VAR MetricPrgm = "4003"
VAR MetricId = 1
VAR Score =
CALCULATE(
VALUES( 'Metric Ranges'[Score] ),
FILTER(
'Metric Ranges',
'Metric Ranges'[MetricId] = MetricId
&& 'Metric Ranges'[Program] = MetricPrgm
&& MetricValue > 'Metric Ranges'[Lower Range]
&& MetricValue <= 'Metric Ranges'[Upper Range]
)
)
RETURN
MetricId
When a calculated column is evaluated, a row context exist and to have the corresponding filter context a CALCULATE is required, to trigger a context transition.
When a measure is evaluated instead, there is the filter context but no row context.
This means that when evaluating the FILTER( 'Metric Ranges' ...) in the measure, only the Metric Ranges portion in the filter context is used. Instead, in the calculated column the whole table is used. To make this code work we must change it to work with a filter context. This might be a possible solution
Net Savings Year to date =
VAR MetricValue = [Savings % Annualize Goal]
VAR MetricPrgm = "4003"
VAR MetricId = 1
VAR Score =
CALCULATE(
SUM( 'Metric Ranges'[Score] ),
FILTER(
ALL( 'Metric Ranges' ),
'Metric Ranges'[MetricId] = MetricId
&& 'Metric Ranges'[Program] = MetricPrgm
&& MetricValue > 'Metric Ranges'[Lower Range]
&& MetricValue <= 'Metric Ranges'[Upper Range]
)
)
RETURN
MetricId

How to add an if statement in a DAX formula measure?

I am new to DAX and have created two measures to get the total pay per employee then lookup that total pay into a table and return a value in my Power Pivot.
Total Pay Measure:
NMRPaySum:=SUMX(Pay,[Regular Pay]+[Overtime Pay]+[Other Pay])
Range Lookup Measure:
SSSContributionEE :=
CALCULATE (
VALUES ( SSSContribution[EE] ),
FILTER (
SSSContribution,
[NMRPaySum] >= SSSContribution[Lower Bound] &&
[NMRPaySum] <= SSSContribution[Upper Bound]
)
)
However, I need the range lookup to only calculate if the employee type is satisfied.
The logic for it is below:
If Employee[Type]="Regular" Then
Calculate SSSConbtributionEE
Else
0
End If
I have tried this DAX formula, but doesn't seem to be working:
=
IF (
OR ( Salary[Type] = "Regular", Salary[Type] = "Agency" ),
CALCULATE (
VALUES ( SSSContribution[EE] ),
FILTER (
SSSContribution,
[NMRPaySum] >= SSSContribution[Lower Bound] &&
[NMRPaySum] <= SSSContribution[Upper Bound]
)
),
0
)
NMRPay Table:
SSS Contribution Table:
Employee Information Table:
Use your measure as is, for all the data. You then build a Pivot table, where you can use a filter or slicers on Employee Type to exclude unwanted values. Add the measure to the Values area and it will only calculate for data that is in the rows and columns of the pivot table.
For the OR condition, you should be able to add that as another filter:
= CALCULATE (
VALUES ( SSSContribution[EE] ),
FILTER (
SSSContribution,
[NMRPaySum] >= SSSContribution[Lower Bound] &&
[NMRPaySum] <= SSSContribution[Upper Bound]
),
FILTER (Salary, Salary[Type] = "Regular" || SalaryType = "Agency")
)
This may or may not work depending on your exact data model / relationship structure, but it might point you in the right direction. It's possible you need to use RELATED / RELATEDTABLE, but I'm not sure without being able to play with it myself.

DAX query to see if there's another row in a table with same id and same date - 1 year

I've got a table with orders. It contains the following relevant columns:
OrderId (Key)
CustomerId
Date (Hierarchy)
I want to create a new column in the same table: OrderedSameMonthLastYear
The value should be true if there's at least one other order from the same customer the same month one year ago.
I've tried a couple different queries but I don't really know enough DAX to accomplish this.
Thanks!
You can use the EARLIER() function to access the previous row context (which is all the rows in the table in this case) and do the comparison between columns, and then use COUNTROWS() to count the number of filtered rows.
OrderedSameMonthLastYear =
IF(
COUNTROWS(
FILTER(
Orders,
Orders[CustomerId] = EARLIER(Orders[CustomerId]) &&
Orders[Date].[Year] = EARLIER(Orders[Date].[Year]) - 1 &&
Orders[Date].[Month] = EARLIER(Orders[Date].[Month])
)
) > 0,
TRUE,
FALSE
)
The result will be as below:

Doctrine2 subquery

I'm trying to write a subquery in doctrine2, to sort a table ordered by date column of another table.
(let's say I'm querying table A, which has an id column, and B has an a_id and a date, in the subquery b.a_id = a.id)
I'm using query builder, and the addSelect method, but since I can't use LIMIT in my query I get this error:
SQLSTATE[21000]: Cardinality violation: 1242 Subquery returns more
than 1 row
This error is true, but how could I limit this query to 1 row, if LIMIT is not allowed in doctrine2 and when I try to do it by querybuilder (I mean the subquery) and I'm using setMaxResults, and then getDQl it is still not working.
->addSelect('(SELECT b.date FROM B b WHERE b.conversation = a.id ORDER BY b.date DESC)')
Is there any solution for my problem?
Thanks
Make the query return exactly one row. Try SELECT MAX(b.date) FROM B b WHERE b.conversation = a.id)

How do I select a RowKey range with Azure Table Storage?

I would like to query my azure tablestorage using PrimaryKey plus I would like to check my RowKey is within a range. For example the range 02001 to 02999
Can someone tell me how I can do this? I understand how to query the PK with a simple:
where fooEntiy.PartitionKey == partition
but I don't know how I can query fooEntity.RowKey.
Also if I do this by specifying a range then will it still retrieve all the entries for that partition and then check to see if they match the range?
Thank you for your advice,
Mariko
Your query could look something like this:
where fooEntity.PartitionKey == partionKey
&& fooEntity.RowKey.CompareTo(lowerBoundRowKey) >= 0
&& fooEntity.RowKey.CompareTo(upperBoundRowKey) <= 0
This should return all of the items between the lowerBoundRowKey and the upperBoundRowKey including those values (if you don't want it to be inclusive, just use > and < rather than >= and <=).
You will not need to do any other filtering than this.
It looks like you're already padding your numbers that you're storing in the RowKey with leading zeros which is a good thing as this range will be a lexical range, not a numeric range.
e.g. running this query with lowerBoundKey = 10 and upperBoundKey = 100 will not return an item with a RowKey of 20.
If you pad it with zeros however lowerBoundKey = 00010 and upperBoundKey = 00100 will return an item with a RowKey of 00020.
This will bring entities using the specified range of RowKey values with specified PartitionKey:
" PartitionKey eq 'your partitonKey value' and (RowKey gt '02001' and RowKey lt '02999') "
Find more information here and here.
Hope this helps.

Resources