Excel, find date based on day name in cell - excel

I have table:
I'm trying to find dates of specific day (here: Monday) in specific month in 5 cells below DATE cell.
For example, Mondays in March - 6.03.2017, 13.03.2017, 20.03.2017, 27.03.2017,
I'm "playing" with it for about 2h, trying week.day, mod, choose and anything iC can find in Google but no luck.
Any ideas?

This kind of task can be done more easily by dividing steps. I made an example sheet. When you input some values in yellow cells, output(calculated by formulae) will be displayed in green cells. What you really need is weekday function, plus I used date, join, day, etc.
Of course, this can be done via VBA script or Apps Script, which can be undoubtedly much more elegant. I added the script to the above sheet. The red cells are filled by this custom function. Its logic is almost the same as the flow of the sheet.
function getDaysWithNames(year, month, name) {
name = name.slice(0,3).toUpperCase();
var t;
//get the first day
var day;
for(day=1; day<8; day++) {
t = new Date(year, month-1, day).toDateString().slice(0,3).toUpperCase();
if(t == name) break;
}
//first three days are always valid because they are <= 21
var validDays = [day, day+7, day+14];
//test the fourth day
day += 21;
t = new Date(year, month-1, day).getDate();
if(t == day) validDays.push(day); else return validDays;
//test the fifth day
day += 7;
t = new Date(year, month-1, day).getDate();
if(t == day) validDays.push(day);
return validDays;
}

Related

Power BI first IF-Statement then the DAX-Formula

I am new to Power BI and have the following issue:
I tried to build a formula for a frequency counter. I got some examples from the web and I was able to build this working formula. The basic idea behind is to categorize an item with the values: daily, weekly or first time.
I tried to add an IF-Statement to the formula, that is checking a calculated column "Time frame", which shows the duration of an item in minutes.
Basically it should run this formula only if the Column "Time frame" is equal or bigger 1.
Now the formula gives to items with a Time frame of 0, the value first time. But they should be ignored or blanked.
Calculated column =
Var freqcount =
COUNTAX(FILTER(ALL('Count'),
AND([Date]>=DATEADD('Count'[Date],-6,DAY)&&[Date]<=EARLIER([Date]),[ID]=EARLIER('Count'[ID]))),ID])
return
if(freqcount>=4,"Daily",if(freqcount>=2,"Weekly",if(freqcount>=1,"First time","Inactive")))
I would be thankful, if someone could support me with this issue.
Edit: an ID can occur multiple times in my table but with different dates. But only once with the same date. For example:
ID 1, Date 01.01.2020
ID 1, Date 02.01.2020
ID 1, Date 03.01.2020
it is easier to use calculate:
Calculated column =
var rDate = yourTable[Date]
var rID = yourTable[ID]
var freqCount = CALCULATE(yourTable('Count'), FILTER(yourTable, rDate >= DATEADD(yourTable[Date], -6 , DAY) && rID = yourTable[ID] && yourTable['Time frame'] > 0))
return if(freqcount>=4,"Daily",if(freqcount>=2,"Weekly",if(freqcount>=1,"First time","Inactive")))
you see how I simply added the Time frame to the expression. Also I removed the use of earlier by using var's so it is better readable.

PowerBI: How to calculate/convert Time into Percentage using DAX/Measure

I managed to get in Excel desired % of time difference from column E, easy job just changed the Data Type to Percentage. What are we calculating is % of these TimeDifferences, one per one (other columns inconsiderable).
The same thing isn't in PowerBI, where I am not able to calculate it properly, always getting "1" before comma and then the result - you can compare it in both tables/columns what I am talking about.
I am looking for the way/DAX/measure how to properly calculate it, no matter in decimals or directly to percentage, as long as the % is the same as in Excel column. Any ideas?
P.S Left is Excel and right is PowerBI!
Seems Excel is basing the percentage on 24 hours, this I used in the calculation (24 hours = 24 * 3600 seconds).
I started combining in power query the date and the time, this has to do with the fact that you go over the day and your calculation still needs to be correct.
Go to query editor. select both columns, combine them. Next change the type to Date/Time, result:
Save and close editor.
In Power Bi, add a column:
NextDate = LOOKUPVALUE(Explog[Date];Explog[Index];Explog[Index] + 1)
This is picking up the next Date based on Index + 1
Add another column TimeDiffSec, calculating the datediff in seconds:
TimeDiffSec = DATEDIFF(Explog[Date];Explog[NextDate];SECOND)
Last step is adding a column for percentage:
% of time difference =
var perc = Explog[TimeDiffSec]/ (24*3600)
return if(perc >= 1; perc - 1; perc)
End result:
Note: If you have a situation you do not want to mix the System (STYRAX - scrubber) you can use the following for the NextDate:
NextDate =
var nextIndex =
CALCULATE(MIN(Explog[Index]);
FILTER(Explog;Explog[Index] > EARLIER(Explog[Index]) && Explog[System] = EARLIER(Explog[System])))
return
LOOKUPVALUE(Explog[Date];Explog[Index];nextIndex; Explog[System];Explog[System])

Determine number of days (not including weekend) based on hours given

I have two important details that are inputted into an excel table, Job_Start_Date and Job_Hours (meaning the hours required to complete the job). Given certain working hours (eg 7:00am-3:30pm) I need to calculate what day and what time they will finish. I already have that basic bit working, but I cannot for the life of me figure out how to skip weekends in that calculation (Note that there is a boolean for Sat/Sun that defines whether that day should be skipped).
Here is an example of the data
and an example of the data visualization: (The DIV errors are because employee count equals 0, pay this no mind.)
.
This is the formula used in the visualizer (a massive index match)
=IFNA(INDEX(INDIRECT(Allocation!$A$1), MATCH(1,($A3 = INDIRECT(Allocation!$K$1))
* (C$1 >= INDIRECT(Allocation!$C$1)) * (C$1 <= INDIRECT(Allocation!$D$1))
* IF(C$1 = INDIRECT(Allocation!$C$1), ($B3 >= INDIRECT(Allocation!$E$1)),
($B3 >= INDIRECT(Allocation!$I$1))) * IF(C$1 = INDIRECT(Allocation!$D$1),
($B3 < INDIRECT(Allocation!$F$1)), ($B3 < INDIRECT(Allocation!$J$1))), 0)),"")
As you can see in the image, Saturdays need to be skipped (being FALSE), but it is still shown on the visualizer. However, if I include a statement that matches the Saturday condition (so it only shows up on a Saturday if TRUE), it will not alter the end date and thus will not push the final day to Monday.
Essentially the question is: How can I skip days but preserve the 'working hours'. This must be done in excel formulas in the same Job table (first image).
Thanks.
Here is how I got it working using WORKDAY.INTL.
First is a nested IF to determine which weekend type to use for the workday.intl function
Weekend=IF(OR([#[JOB SATURDAY]], [#[JOB SUNDAY]]), IF(AND([#[JOB SATURDAY]], [#[JOB SUNDAY]]), -1, IF([#[JOB SATURDAY]], 11, 17)), 1)
Then a second if statement that references that value and then spits out the correct date
=IF([#[Weekend]] = -1, [#[JOB START DATE]]+[#[Working Days]], WORKDAY.INTL([#[JOB START DATE]], [#[Working Days]], [#[Weekend]]))

Making Google sheets for entries made today, this week, and this month

I have a Google sheet that pulls information from a form. The form inputs a timestamp and IDnumber into a spreadsheet. I need to make different sheets to show which of these entries were made Today, This week, and This month.
Example
Timestamp ID
12/1/2012 12345
12/1/2012 55555
12/4/2012 98765
12/15/2012 74823
I need to make a sheet that puts ID 12345 and 55555 into one "DAILY" Sheet, IDs 12345, 55555, and 98765 into a "WEEKLY" sheet, and one that has all of these in a "MONTHLY" sheet.
Here's what I would do...
Add today's date somewhere in the sheet (let's say cell D2) by inputting '=Today()' in D2.
In column C, next to your first two columns, add formulas to calculate the number of days between the date in column A and today's date. In row 2, the formula would look like this...
=DATEDIF(A2,D2,"D")
Then, using the script editor in the spreadsheet, I would write a script that looks at column C and writes values from column A and B depending on that value. I haven't tested the script below, but I think it's what you would need. Essentially, it loops through column C of your results sheet. Depending on the value (days from today), that row is pushed to an array for each range. Then I cleared the sheet for that range before setting the values of the array. I hope this helps!
function daysBetween(){
var spreadsheet = SpreadsheetApp.getActiveSpreadsheet(),
formResultsSheet = spreadsheet.getSheetByName("Form Results"),
daySheet = spreadsheet.getSheetByName("Today"),
weekSheet = spreadsheet.getSheetByName("This Week"),
monthSheet = spreadsheet.getSheetByName("This Month"),
allData = formResultsSheet.getDataRange().getValues(),
dayResults = [],
weekResults = [],
monthResults = [];
for (var i = 0; i < formResultsSheet.getLastRow(); i++) {
if (allData[i][2] === 0){
dayResults.push(allData[i])
} else if (allData[i][2] > 0 && allData[i][2] <= 7) {
weekResults.push(allData[i])
} else if (allData[i][2] > 7 && allData[i][2] <= 30) {
monthResults.push(allData[i])
}
}
daySheet.getDataRange().clear()
daySheet.getRange(1,1,dayResults.length,dayResults[0].length).setValues(dayResults)
weekSheet.getDataRange().clear()
weekSheet.getRange(1,1,weekResults.length,weekResults[0].length).setValues(weekResults)
monthSheet.getDataRange().clear()
monthSheet.getRange(1,1,monthResults.length,monthResults[0].length).setValues(monthResults)
}

date time conversion

I have 40241 as a date value. Which format is this in?
I think it is in seconds past midnight.
But I need a formula so that I can work out manually and verify!!
Thanks
If it is an Excel datestamp, then it's the number of days since 31st December 1899 (with 1900 treated as a leap year); which puts it as 4th March 2010... unless Excel was configured to use the Mac 1904 Calendar, in which case it's the number of days since 1st January 1904.
How to convert it depends on your preferred scripting language; or whether you can simply use Excel itself, and just set the format mask for that cell to one of the date formats
If it is "seconds past midnight", you can simply do this:
divide by 3600 (seconds in an hour); you get the number of hours;
take the remainder of that division, and divide it by 60; you get the minutes;
the remainder is the seconds.
Example:
40241/3600=11 (641)
641/60=10 (41)
So it is 11:10:41.
By the way, I suppose that it's a time value; if it was a datetime value it would probably be much bigger (like UNIX timestamps) or it would have a decimal part (like, IIRC, OLE dates).
It turns out that it's an Excel date; then, have a look at this KB article, it's all explained in detail; but if you just want to display it correctly, go on the properties of the cell (Ctrl+1) and set its data type to "Date" or "Date/Time" (or whatever it was, I don't have Excel at hand at the moment).
Excel stores dates in an interesting way. I've had this crop up on me too but I never had to move outside Excel so I could just use the format function in Excel.
You can read more here:
http://www.ozgrid.com/Excel/ExcelDateandTimes.htm
http://www.cpearson.com/excel/datetime.htm
Excel saves the date as an integer for the number of days since Jan 1st, 1900
Note: there is a bug in excel so you do the conversion and subtract one. If you see a decimal after it is the time.
Here is some java code to convert it if you want to verify it:
public static Date ExcelDateParse(int ExcelDate){
Date result = null;
try{
GregorianCalendar gc = new GregorianCalendar(1900, Calendar.JANUARY, 1);
gc.add(Calendar.DATE, ExcelDate - 1);
result = gc.getTime();
} catch(RuntimeException e1) {}
return result;
}
I condensed the apache solution for the date without time ( https://svn.apache.org/repos/asf/poi/trunk/src/java/org/apache/poi/ss/usermodel/DateUtil.java )
public static Date parseExcelDate(double date) {
int wholeDays = (int) Math.floor(date);
Calendar calendar = new GregorianCalendar();
int startYear = 1900;
int dayAdjust = wholeDays < 61 ? 0 : -1;
calendar.set(startYear, 0, wholeDays + dayAdjust, 0, 0, 0);
return calendar.getTime();
}
Other thread: Program in Java to convert a date to serial number as done in Excel

Resources