How to filter in crossfilter by current date and yesterday's date? - dc.js

I want to make a query in crossfilter to filter all data from today's date.
Example: I have this query:
var countPerDim2 = Dim2.filterExact("1.1.2018").group().reduceSum();
So crossfilter will filter data from 1.1.2018.
But I want crossfilter to automatically get the the current date.
My reason is that I want to draw two charts to compare them.
Like :
Chart 1 by Date 1.1.2018 and Chart 2 by Date 31.12.17
How can I get the filter by yesterday's date? Is there a function like datenow() for the current day and maybe datenow(-1) for yesterday's date?
Thank you and happy new year!

Today and yesterday
To answer your immediate question, JavaScript's new Date() will return the current time and date as a JavaScript Date object. Then you just need to convert the date object to a string to match your date format.
You can use d3.time.format to produce those strings. Looks like you would want something like
d3.time.format('%-d.%-m.%Y')
(or perhaps with m and d reversed - unclear from your example whether month or day comes first)
Yesterday is something like
var date = new Date();
date.setDate(date.getDate()-1);
See the JavaScript Date documentation for more details.
Comparing two days
However, I think you'll run into a more fundamental problem, which is that crossfilter doesn't have the concept of multiple filters, so it's hard to compare one date against another in side-by-side charts.
Off the top of my head, the best thing I can think of is to "freeze" a group using a fake group:
function freeze_group(group) {
var _all = group.all().slice();
return {
all: function() {
return _all;
}
};
}
You'd apply one date filter, then call
chart.group(freeze_group(group));
on the chart you want to have that date. Now it won't change when the filter changes, and you can apply the other date filter for other charts.

Related

DAX How to Adjust Dates Based on Selected Value of Slicer

Here is my setup:
I have a date slicer that I created by doing this:
d_date = Calendar(min('Table_1'[CTD]),max('Table_1'[CTD])
Here is what my dax looks like for my variables currently:
var FirstVisibleVDate = Min('d_date'[Date])
var LastVisibleDate = Max('d_date'[Date])
My problem(s):
Problem 1: I am doing a running average, I want the running average to adjust itself based on the dates selected in the slicer. I don't think this is possible, is it?
So basically, something like this:
var FirstVisibleVDate = Min(value set in the date slicer)
var LastVisibleDate = Max(value set in the date slicer)
Date range=
Var MinDate = CALCULATE(MIN(Table1[transaction date]),ALLSELECTED(Table1[transaction date]))
Var MaxDate = CALCULATE(MAX(Table1[transaction date]),ALLSELECTED(Table1[transaction date]))
Source:
https://community.powerbi.com/t5/Desktop/Capture-Time-Slicer-Minimum-Value/m-p/820372
If you are going to make a selection over the slicer then that will limit the rows that are visible in the Matrix visual, you will only see the result for the dates selected, so you will have to create a duplicate disconnected Date table that will not filter the rows of the visual.
I have made a couple of forum post on this topic as well as some videos:
Posts:
https://forum.enterprisedna.co/t/dax-calculation-for-choose-n-specific-date/16408
https://forum.enterprisedna.co/t/learning-resources-about-disconnected-date-tables/15416/2
https://forum.enterprisedna.co/t/cumulative-transactions/11803/19
Videos:
https://www.youtube.com/watch?v=WFJThyfQt-A&ab_channel=AntrikshSharma
https://www.youtube.com/watch?v=fLWfdlAaHlQ&ab_channel=AntrikshSharmaAntrikshSharma

Why date validation in Google sheets rejects today's date before 8:00am?

I've created a Google sheet to keep a list of work tasks with a column to track the date on which items are created, and built a script to automatically populate the cells in that column with the day's date when a new line is inserted.
The cell (e.g. G9) that is target of the script uses the following validation formula to make sure that when users change the date, they use a date that is neither a weekend nor in the future:
=and(isdate(G9), weekday(G9,2)<6, G9<=today())
IT ONLY WORKS BUT ONLY IF THE SCRIPT IS RUN ANYTIME AFTER 8:00am ! If I try using it any earlier the cell validation will reject the input!
The script looks like this (curRow is the number of the row that's been added):
// Adds today's date without using =today()
var myrangename = "G"+curRow;
var dateCell = sheet.getRange(myrangename);
var d = new Date();
var dateArr = [];
dateArr[0]=d.getFullYear();
dateArr[1]=d.getMonth() + 1; //Months are zero based
dateArr[2]=d.getDate();
dateCell.setValue(dateArr.join('/'));
(n.b.: I cannot use the script to simply put =today() in the cell because all the entries would change every day. )
WHY DOES IT ONLY WORK AFTER 8:00AM? Is Google somehow running on a different time zone than my computer?? I'm based in the UK, so using BST, but that shouldn't be a problem, shouldn't it...?
Try
var d = new Date();
var d = Utilities.formatDate(d, "GMT+1", "yyyy-MM-dd HH:mm:ss");
I am not sure if google would recognise BST as a time zone, but you could also try
var d = Utilities.formatDate(d, "BST", "yyyy-MM-dd HH:mm:ss");
Thank you for your suggestion, Aprillion. Turns out that a Google Sheets file has its own internal time-zone setting! which in my case was set to American Pacific time (so 8hrs behind)
(You'd think it would pick up the date and time info automatically from Windows, like other applications do!)
To set the sheet's time-zone to the correct one, you need to go to the main menu, click 'File', then 'Spreadsheet settings...', and adjust as necessary.
The script and validation now all work fine.
Thank you all for your help.

What does this d3 time format function mean?

I know there is a specified d3 time formatting. But when I check the example shown here d.Year = new Date(d.Year,0,1); the year's format is "1996"
Does it one of the other ways to format year as a string here, i don't quite understand it.
Also, if my time format is like"01/01/96", what is the right format to get the string here?
This isn't a d3 date formatting function. Its one of the basic forms of the Javascript Date constructor.
Specifically, it is using the multi-parameter form of the constructor:
new Date(year, month, day, hour, minute, second, millisecond);
except that all the time parameters are left as their default (0/midnight) values. If the original value for d.Year is the string "1996", the line
d.Year = new Date(d.Year,0,1);
creates a new Date object with year 1996 (the conversion from string to number will be automatic), month value zero (January), day value 1, and time midnight.

How can I compare 2 dates in a Where statement while using NoRM for MongoDB on C#?

I have a table in Mongo. One of the fields is a DateTime. I would like to be able to get all of the records that are only for a single day (i.e. 9/3/2011).
If I do something like this:
var list = (from c in col
where c.PublishDate == DateTime.Now
select c).ToList();
Then it doesn't work because it is using the time in the comparison. Normally I would just compare the ToShortDateString() but NoRM does not allow me to use this.
Thoughts?
David
The best way to handle this is normally to calculate the start datetime and end datetime for the date in question and then query for values in that range.
var start = DateTime.Now.Date;
var end = start.AddDays(1);
...
But you'd also be well advised to switch to the official C# driver now. You should also use UTC datetimes in your database (but that gets more complicated).

Get the the most recent and the one before the most recent item

I have a table with many anniversaries : Date + Name.
I want to display the next anniversary and the one after with Linq.
How can i build the query ?
I use EF
Thanks
John
Just order by date and then use the .Take(n) functionality
Example with a list of some objects assuming you want to order by Date then Name:
List<Anniversaries> annivDates = GetAnnivDates();
List<Anniversaries> recentAnniv = annivDates.OrderBy(d => d.Date).ThenBy(d => d.Name).Take(2).ToList();
If the anniversaries are stored in regular DateTime structs, they may have the 'wrong' year set (i.e. wedding or birth year). I suggest writing a function which calculates the next date for an anniversary (based on the current day) like:
static DateTime CalcNext(DateTime anniversary) {
DateTime newDate = new DateTime(DateTime.Now.Year, anniversary.Month, anniversary.Day);
if (newDate < DateTime.Now.Date)
newDate = newDate.AddYear(1);
return newDate;
}
Then you proceed with sorting the dates and taking the first two values like described in the other postings:
(from e in anniversaries orderby CalcNext(e.Date) select e).Take(2)

Resources