iMacros — Setting date to tomrrow and day after tomorrow - imacros

I have recorded an iMacro which deals with setting dates in two dropdown lists, tomorrow and day after tomorrow. But people who are using it have to edit the macro everyday to set the dates, including month if it's changing tomorrow or day after tomorrow.
I was wondering if imacro can do it automatically? Setting date 1 to tomorrow's date and date 2 to day-after-tomorrow date. And what about month? What if a new month starting tomorrow or day after tomorrow? Can imacro make some commonsense decisions by itself? :)

This code outputs tomorrow day and day after tomorrow. Need to use Javascript and iMacros command EVAL.
var today = new Date();
var tomorrow = new Date();
tomorrow.setDate(today.getDate() + 1);
var day = tomorrow.getDate().toString();
if (day.length < 2) {day = "0" + day;}
var month = (tomorrow.getMonth() + 1).toString();
if (month.length < 2) {month = "0" + month;}
var year = tomorrow.getFullYear().toString();
var tomorrowDateString = day + "/" + month + "/" + year;
var afterTomorrow = new Date();
afterTomorrow.setDate(today.getDate() + 2);
var day = afterTomorrow.getDate().toString();
if (day.length < 2) {day = "0" + day;}
var month = (afterTomorrow.getMonth() + 1).toString();
if (month.length < 2) {month = "0" + month;}
var year = afterTomorrow.getFullYear().toString();
var afterTomorrowDateString = day + "/" + month + "/" + year;
alert('tomorrow: '+tomorrowDateString+' after tomorrow: '+afterTomorrowDateString);
For iMacros this code will be look like this:
SET tomorrow EVAL("var today = new Date(); var tomorrow = new Date(); tomorrow.setDate(today.getDate() + 1); var day = tomorrow.getDate().toString(); if (day.length < 2) {day = \"0\" + day;} var month = (tomorrow.getMonth() + 1).toString(); if (month.length < 2) {month = \"0\" + month;} var year = tomorrow.getFullYear().toString(); var tomorrowDateString = day + \"/\" + month + \"/\" + year; tomorrowDateString;")
PROMPT {{tomorrow}}
SET afterTomorrow EVAL("var today = new Date(); var afterTomorrow = new Date(); afterTomorrow.setDate(today.getDate() + 2); var day = afterTomorrow.getDate().toString(); if (day.length < 2) {day = \"0\" + day;} var month = (afterTomorrow.getMonth() + 1).toString(); if (month.length < 2) {month = \"0\" + month;} var year = afterTomorrow.getFullYear().toString(); var afterTomorrowDateString = day + \"/\" + month + \"/\" + year; afterTomorrowDateString;")
PROMPT {{afterTomorrow}}

Related

How to display total correct in measure

I'm trying to compare difference between years in sales but I'm having the following issue:
I have this:
Valor Actual = -CALCULATE(SUM(Apuntes_Resultado[Total Valor]), Apuntes_Resultado[IDEscenario]=1)
Total Valor Previo =
VAR SubgrupoFilter = ISFILTERED(TCuentas[SubGrupo])
VAR TipoCuentaFilter = ISFILTERED(TTipoCuenta[IDTipoCuenta])
VAR Variablefilter = OR(SubgrupoFilter,TipoCuentaFilter)
VAR Resultado = IF(Variablefilter, [Valor Actual], [ActualAjust])
RETURN
Resultado
I use the above code to have this measure
`
Total Valor Final =
VAR IDVistaDetalle = SELECTEDVALUE(TTipoCuenta[Vista Detalle])
VAR IDDetalle = SELECTEDVALUE(TTipoCuenta[Detalle])
VAR IDDetalleVisible = ISFILTERED(TCuentas[SubGrupo])
VAR Resultado = SWITCH(TRUE(),IDDetalleVisible=TRUE() && IDDetalle = 0, BLANK(),
IDVistaDetalle = 1, [Total Valor Previo],
IDVistaDetalle = 2, [Valor Acumulado])
RETURN Resultado`
this works properly. But i'm trying to normalize this values with laboral day's between years.
To this I have created the following column in my date table:
`Laboral Day =
VAR EsFinDeSemana = Dates[Number Day] IN {7}
VAR EsFestivo =
RELATED(TablaFestivos[Fecha])
RETURN
IF (EsFestivo || EsFinDeSemana,0,1)`
It works fine. Shows properly laboral days and holidays
After that, I'm using the follow measure to calculate the values adjusted by year
`AdjustYear = var total =CALCULATE([Total Valor Final], SAMEPERIODLASTYEAR(Dates[Date]))
var LBCY= CALCULATE(SUMX(Dates,Dates[Laboral Day]))
var PYLB= CALCULATE(SUMX(Dates,Dates[Laboral Day]),SAMEPERIODLASTYEAR(Dates[Date]))
return - DIVIDE(total, PYLB)*LBCY //value from previousyear/PYlaboraldays * CYlaboraldays
`
This is a sample of the result:
as you can see, it is taking the same total for two columns but values are different in rows.....
Totals for 2022 and 2021 are ok, laboral days are okey but B 2021 ajus is taking the same total of A 2021
I'm using my date table to filter by months.
Any help?
myMeasure=
VAR myTbl =
Addcolumns(
Summarize(
Dates
,Dates[yearMonth]
)
,"total",[AdjustYear]
)
RETURN
SUMX(myTbl ,[total])
OR
myMeasure=
VAR myTbl =
Addcolumns(
VALUES(Dates[yearMonth])
,"total",[AdjustYear]
)
RETURN
SUMX(myTbl ,[total])
OR
myMeasure=
VAR myTbl =
Addcolumns(
VALUES(Dates[Month]) -- the same column that you use in the matrix
,"total",[AdjustYear]
)
RETURN
SUMX(myTbl ,[total])

Multiple Selection on Dynamic Financial Quarter by Customer using Power Bi

My coworker and I are trying to create a quarterly report that has the ability to slice by customer and reflect their financial year. For example one customers financial year starts in July so Quarter 1 for them should be July 1 - September 30. We are able to get this to work for one specific quarter already but the issue becomes when we introduce the previous quarter. We are wondering if there is a way to get the current quarter based on the customers financial year start month and the previous quarter dynamically. Here is a preview of our report and model:
Report
Model
FY Start Date =
VAR FinancialStartMonth = MIN(Customer_List[FY.Start.Month])
VAR FiscalYearSelected = SELECTEDVALUE('QuarterIDs'[Year])
VAR QuarterNum = SELECTEDVALUE(QuarterIDs[Quarter])
VAR StartDate = SWITCH(QuarterNum, 1, DATE(FiscalYearSelected, FinancialStartMonth, 1), 2,
DATE(FiscalYearSelected, FinancialStartMonth+3, 1), 3, DATE(FiscalYearSelected,
FinancialStartMonth+6, 1), 4, DATE(FiscalYearSelected, FinancialStartMonth+9, 1))
RETURN
StartDate
FY End Date =
VAR FinancialStartMonth = MIN(Customer_List[FY.Start.Month])
VAR FiscalYearSelected = SELECTEDVALUE('QuarterIDs'[Year])
VAR QuarterNum = SELECTEDVALUE(QuarterIDs[Quarter])
VAR StartDate = SWITCH(QuarterNum, 1, DATE(FiscalYearSelected, FinancialStartMonth, 1), 2,
DATE(FiscalYearSelected, FinancialStartMonth+3, 1), 3, DATE(FiscalYearSelected,
FinancialStartMonth+6, 1), 4, DATE(FiscalYearSelected, FinancialStartMonth+9, 1))
VAR EndDate = DATE(YEAR(StartDate), MONTH(StartDate)+3,1) -1
RETURN
EndDate
Total Amount = CALCULATE(SUM(Raw_Data[Amount]),
FILTER('Raw_Data', [Date]))
Amounts by FY =
VAR FinancialStartMonth = MIN(Customer_List[FY.Start.Month])
VAR FiscalYearSelected = SELECTEDVALUE(QuarterIDs[Year])
VAR QuarterNum = SELECTEDVALUE(QuarterIDs[Quarter])
VAR StartDate = SWITCH(QuarterNum, 1, DATE(FiscalYearSelected, FinancialStartMonth, 1), 2,
DATE(FiscalYearSelected, FinancialStartMonth+3, 1), 3, DATE(FiscalYearSelected,
FinancialStartMonth+6, 1), 4, DATE(FiscalYearSelected, FinancialStartMonth+9, 1))
VAR EndDate = DATE(YEAR(StartDate), MONTH(StartDate)+3,1) - 1
RETURN
IF(HASONEVALUE(Dates[Date]),
IF(AND(MIN(Dates[Date]) >= StartDate, MAX(Dates[Date]) < EndDate), [Total Amount], BLANK()),
CALCULATE([Total Amount],
FILTER(ALL(Dates[Date]), Dates[Date] >= StartDate && Dates[Date] < EndDate)))
Big thanks to #EnterpriseDNA for his multiple financial years solution. Thank you for reading this and we appreciate any information or assistance you can provide!

How to calculate Dynamic Moving Average without any date reference?

I am looking for some clarification with regards to moving average calculation. My data looks like the screenshot attached.
I checked online for suggestion but it went awry. Most of the moving averages work with date (in date format). As I am having Day...only in numeric format...I am not sure which function would help me to get Mov_Avg for 3 Day time frame on the Increment col. My desired output should be col 3_day_Avg
You can use your column Increase (created using Power Query) and create this below measure for your required output-
last_three_day_average =
VAR current_row_day = MIN(your_source_table_name[day])
VAR current_row_country = MIN(your_source_table_name[country])
VAR current_row_case_death = MIN(your_source_table_name[cases/death])
VAR current_row_count = MIN(your_source_table_name[count])
RETURN
CALCULATE(
AVERAGE(your_source_table_name[increase]),
FILTER(
ALL(your_source_table_name),
your_source_table_name[country] = current_row_country
&& your_source_table_name[cases/death] = current_row_case_death
&& your_source_table_name[day] <= current_row_day
&& your_source_table_name[day] >= current_row_day - 2
)
)
Here is the final output-
Alternatively, if you wants not to use Power Query, You can use Custom column and that case you have to convert your Measure increase_using_measure as a column. As aggregation is required, you can not refer a measure for that. Here below is the code for column-
increase_using_column =
VAR current_row_day = your_source_table_name[day]
VAR current_row_country = your_source_table_name[country]
VAR current_row_case_death = your_source_table_name[cases/death]
VAR current_row_count = your_source_table_name[count]
VAR previous_day_count =
LOOKUPVALUE(
your_source_table_name[count],
your_source_table_name[day],current_row_day-1,
your_source_table_name[country],current_row_country,
your_source_table_name[cases/death],current_row_case_death
)
RETURN if(previous_day_count = BLANK(),current_row_count, current_row_count - previous_day_count)
Now you can use the above column created using DAX in your Last 3 days average calculation as below-
last_three_day_average =
VAR current_row_day = MIN(your_source_table_name[day])
VAR current_row_country = MIN(your_source_table_name[country])
VAR current_row_case_death = MIN(your_source_table_name[cases/death])
VAR current_row_count = MIN(your_source_table_name[count])
RETURN
CALCULATE(
AVERAGE(your_source_table_name[increase_using_column]),
FILTER(
ALL(your_source_table_name),
your_source_table_name[country] = current_row_country
&& your_source_table_name[cases/death] = current_row_case_death
&& your_source_table_name[day] <= current_row_day
&& your_source_table_name[day] >= current_row_day - 2
)
)
Output will be same as shown above.

How to format a date like "5 min", "1 hour,", "Yesterday", "1 Week ago"?

I am trying to get something similar to facebook post date/time. Like 1 min, 1 hour, Yesterday, Week ago etc.
This is the is function I am using to get this format: (Oct-22 17:28 PM)
func convertTime(serverTime: Double) -> String {
let serverTime = serverTime
let date = Date(timeIntervalSince1970: serverTime)
let dateFomatter = DateFormatter()
dateFomatter.dateFormat = "MMM-dd HH:mm a"
let strDate = dateFomatter.string(from: date)
print("This is the post's date: \(strDate)")
return strDate
}
try this code
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "yyyy-MM-dd HH:mm:ss"
dateFormatter.locale = Locale.init(identifier: "en_GB")
let before = dateFormatter.date(from: "2017-10-23 10:28:17")!
//getting the current time
let now = Date()
let formatter = DateComponentsFormatter()
formatter.unitsStyle = .full
formatter.zeroFormattingBehavior = .dropAll
formatter.maximumUnitCount = 1
formatter.allowedUnits = [.year, .month, .weekOfMonth, .day, .hour, .minute]
formatter.includesApproximationPhrase = true
let formatString = NSLocalizedString("%# ago", comment: "e.g. '2 hours ago'")
let timeString = formatter.string(from: before, to: now)

How to set the first day of current week to Tuesday in Swift?

How to set the first day of current week to Tuesday in Swift 3? The schedule will update every Tuesday so the first day have to start from Tuesday.
and then display on label :
thisWeekDate.text! = "\(first day of current week) - \(last day of current week)"
Full code.
let today = NSDate()
let nextTue = Calendar.current.date(byAdding: .day, value: 6, to: today as Date)
let formatter = DateFormatter()
formatter.dateFormat = "yyyy-MM-dd"
let todayString = formatter.string(from: today as Date)
let nextString = formatter.string(from: nextTue!)
formatter.dateFormat = "dd-MMM-yyyy"
let calendar = NSCalendar(calendarIdentifier: NSCalendar.Identifier.gregorian)
let components = calendar!.components([.weekday], from: today as Date)
//Label code
thisWeekDate.text! = "\(first day of current week) - \(last day of current week)"
//If i can change the first day of current week this weekly update code can be deleted
if components.weekday == 3 {
print("Hello Tuesday")
thisWeekDate.text! = "\(todayString) - \(nextString)"
UserDefaults.standard.set(todayString, forKey: "todayStringKey")
_ = UserDefaults.standard.object(forKey: "todayStringKey") as? String
UserDefaults.standard.set(nextString, forKey: "nextStringKey")
_ = UserDefaults.standard.object(forKey: "nextStringKey") as? String
} else {
print("It's not Tuesday")
let RetrivedDate_1 = UserDefaults.standard.object(forKey: "todayStringKey") as? String
let RetrivedDate_2 = UserDefaults.standard.object(forKey: "nextStringKey") as? String
if let date_1 = RetrivedDate_1, let date_2 = RetrivedDate_2 {
thisWeekDate.text! = "\(date_1) - \(date_2)"
}
}
App Design Screenshot
Create a custom Gregorian calendar and set the first weekday to Tuesday
var customCalendar = Calendar(identifier: .gregorian)
customCalendar.firstWeekday = 3
Then you can get the week interval with
var startDate = Date()
var interval = TimeInterval()
customCalendar.dateInterval(of: .weekOfMonth, start: &startDate, interval: &interval, for: Date())
let endDate = startDate.addingTimeInterval(interval - 1)
print(startDate, endDate)