Subtract 1 month of the Calendar Function End Date - dax

I am using a calendar table in an slicer in my Power BI report. In order to avoid the user to select the current month, I would like this calendar to go up to the last day of the previous month. i.e., If we are in February, it would only display the dates until Jan 31.
How can I define the end date of my Calendar DAX.
CALENDAR (
DATE(2019,1,1),
TODAY()
)
I have tried a couple of things included ENDOFMONTH() and EOMONTH(), but none of them worked.

If you want to exclude, then you could calculate the end date of the previous month using EOMONTH function and then pass this date to CALENDAR, e.g. like this:
var CalendarTable =
var LastMonthEnd = EOMONTH(TODAY(),-1)
var DatesRange = CALENDAR (
DATE(2019,1,1),
LastMonthEnd
)
return DatesRange

I like Andrey's solution, but want to flagg that you can do it like this as well:
Calendar =
CALENDAR(
DATE(2019,1,1),
TODAY()-DAY(TODAY())
)
As always, in DAX there are several ways of doing the same thing.

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

Can't get monthy CarbonPeriod to behave as desired

In the project I'm working on I have a daily command that basically checks the date of the last record in the database and tries to fetch data from an API from the day after and then each month after that (the data is published monthly).
Basically, the last record's date is 2019-08-30. I'm mocking as if I were running the task on 2019-09-01 with
$test = Carbon::create(2019,9,1,4);
Carbon::setTestNow($test);
I then create a monthly period between the next day of the last record's date and the last day of the current month like so:
$period = CarbonPeriod::create($last_record_date->addDay(), '1 month', $last_day_of_current_month);
Successfully generating a period with start_date = 2019-08-31 and end_date = 2019-09-30. Which I use in a simple foreach.
What I expected to happen is that it runs twice, once for August and once for September, but it's running only once for the start date. It's probably adding a month and going past the end date, but I don't know how to force the behaviour I'm looking for.
TL;DR:
$period = CarbonPeriod::create('2019-08-31', '1 month', '2019-09-30');
foreach ($period as $dt) {
echo $dt->format("Y-m") . "<br>\n";
}
This will print just 2019-08, while I expect 2019-08 and 2019-09. What's the best way to achieve that?
Solution :-
You can store actual date in $actual_day and current date for occurring monthly in $current_day. Put a check on comparing both dates, if not matched then make it on the same day it will skip 30,31 case in case of February month.
$current_date = $current_date->addMonths(1);
if($current_date->day != $actual_day){
$date = Carbon::parse($date->year."-".$date->month."-".$actual_day);
}
Your start date is 2019-08-31. Adding a month takes you to 2019-09-31. 2019-09-31 doesn't exist so instead you get 2019-10-01, which is after your end date. To avoid this I'd suggest you use a more regular interval such as 30 days.
Otherwise you're going to have to rigorously define what you mean by "a month later". If the start date is 31st Jan is the next date 28th February? Is the month after 28th or 31st March? How do leap years affect things?

How do I get the aggr of two aggrs in QlikView?

If I want to find the maximum value of a column from two states aggregated by a member's ID, should this work?
=Aggr(
MaxString(
Aggr(NODISTINCT MinString({[State1]}DATE_STRING),MBR_ID)
+
Aggr(NODISTINCT MinString({[State2]}DATE_STRING),MBR_ID)
) , MBR_ID)
So if I had this data:
MBR ID DATE_STRING
1 20120101
1 20120102
1 20120103
And State1 had 20120101 selected and State2 has 20120103 selected, my expression would return 20120103 for member 1.
Thanks!
Edit: In SQL, this would look like:
WITH MinInfo (DATE_STRING, MBR_ID)
AS (SELECT MIN(DATE_STRING), MBR_ID FROM Table WHERE TYPE IN ('State1', 'State2') GROUP BY MBR_ID, TYPE)
SELECT MAX(DATE_STRING) DATE_STRING, MBR_ID FROM MinInfo GROUP BY MBR_ID
It would be easier to accomplish your goal if you convert your that to an actual date field
Assuming that you are using a chart where MBR_ID is the Dimension, if you want the maximum date (latest date) you can do the following:
=nummax(Max({[State1]}DATE_STRING),Max({[State2]}DATE_STRING))
To convert to a date, you can use this function:
date#(DATE_STRING,'[text format of the date]')
(The date format looks like YYYYMMDD to me, but if its day then month, you would use YYYYDDMM)
I'd suggest you format it in the script, so that you wont have to worry about it every time you need to use that date.

Determine the amount of clicks required to reach a specific date using a JQuery calendar?

I have a jquery calendar for the start date of a project.
Using Watir (automated browser driver, a gem for ruby), I have a set date that I would like to enter in.
The calendar start date is always today's date, whatever that may be for the day it is used. I was wondering if there was a way that ruby can process what today's date is, and use the specified date provided by the user, to calculate the difference of months between them.
Here is an example of the Calendar plugin: http://jqueryui.com/datepicker/
example:
today's date is 30/10/2012, if there was a project that were to start on the 20/12/2012, that would be 2 months from now, so 2 clicks on the next month button.
Is there a way I could do this?
Here is how I approached a similar situation with JSdatepicker:
$today = Time.now.strftime("%e").gsub(" ", "") #one digit day of month without leading space
#browser.text_field(:id => /dateAvailable/).click
Watir::Wait.until(60) {#browser.div(:id => /dateAvailable_popup_cal/).td(:text => $today).exists?}
#browser.div(:id => /dateAvailable_popup_cal/).td(:text => $today).click
Set or grab the date.
Click the text_field that fires the JSDatePicker object
Wait until the calendar actually pops up
The current month is shown, so choose today's date number.
In your case, you also need to set the month. Whether prompting the user for this, or choosing "today", the theory is the same:
$month = Date::MONTHNAMES[Date.today.month] #etc
Pseudo-code making lots of assumptions (only future dates, month name shown on calendar as text, etc):
while !#jquerytablewindow.text.include?($month)
next_month_button.click
end
I don't see a specific advantage to my method versus counting each month, unless of course we add a month to the calendar one day and you still want your code to work!
You could do:
#End date converted to date object
specified_date = '20/12/2012'
end_date = Date.parse(specified_date)
#Start date (today - 30/10/2012)
today = Date.today
#Determine difference in months
number_of_months_up_to_today = (today.month + today.year * 12)
number_of_months_up_to_end = (end_date.month + end_date.year * 12)
clicks_required = number_of_months_up_to_end - number_of_months_up_to_today
#=> 2
Basically it is counting the number of months since the year 0 and then finding the difference.

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