Time format in Excel using excel4node - excel

I am trying to print the number of hours in excel sheet using excel4node application. although the number of hours is getting printed in the excel, I am not able to get the sum or average while selecting that column.
Its only counting the columns not calculating the sum of the hours.
I am expecting the average while selecting the columns (Marked in RED color)
But what I am actually getting is counting of columns(Marked in RED color)
ws.cell(i+2,j+2).string(value).style(bodystyle);
value here contains the number of hours.
Ask me for more Clarification.

turn the value into a date object and use setUTCHours & setUTCMinutes
functions to set the desired time.
In the workbook settings set dateFormat attribute to hh:mm .. something like
const ws = new xl.Workbook({ dateFormat: 'hh:mm' });
Use .date() function instead of .string()
Example
const [hours, minutes] = value.split(':');
const date = new Date();
date.setUTCHours(hours);
date.setUTCMinutes(minutes);
-----
const ws = new xl.Workbook({ dateFormat: 'hh:mm' });
-----
ws.cell(i+2,j+2).date(date).style(bodystyle);
There will be a problem though ... this will not work as expected if the sum of the hours is 24 or more.

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])

Convert dates from Excel to Matlab

I have a series of dates and some corresponding values. The format of the data in Excel is "Custom" dd/mm/yyyy hh:mm.
When I try to convert this column into an array in Matlab, in order to use it as the x axis of a plot, I use:
a = datestr(xlsread('filename.xlsx',1,'A:A'), 'dd/mm/yyyy HH:MM');
But I get a Empty string: 0-by-16.
Therefore I am not able to convert it into a date array using the function datenum.
Where do I make a mistake? Edit: passing from hh:mm to HH:MM doesn't work neither. when I try only
a = xlsread('filename.xlsx',1,'A2')
I get: a = []
According to the documentation of datestr the syntax for minutes, months and hours is as follows:
HH -> Hour in two digits
MM -> Minute in two digits
mm -> Month in two digits
Therefore you have to change the syntax in the call for datestr. Because the serial date number format between Excel and Matlab differ, you have to add an offset of 693960 to the retrieved numbers from xlsread.
dateval = xlsread('test.xls',1,'A:A') + 693960;
datestring = datestr(dateval, 'dd/mm/yyyy HH:MM');
This will read the first column (A) of the first sheet (1) in the Excel-file. For better performance you can specify the range explicitly (for example 'A1:A20').
The code converts...
... to:
datestring =
22/06/2015 16:00
Edit: The following code should work for your provided Excel-file:
% read from file
tbl = readtable('data.xls','ReadVariableNames',false);
dateval = tbl.(1);
dateval = dateval + 693960;
datestring = datestr(dateval)
% plot with dateticks as x-axis
plot(dateval,tbl.(2))
datetick('x','mmm/yy')
%datetick('x','dd/mmm/yy') % this is maybe better than only the months
Minutes need to be called with a capital M to distinguish them from months.
Use a=datestr(xlsread('filename.xlsx',1,'A:A'),'dd/mm/yyyy HH:MM')
Edit: Corrected my original answer, where I had mixed up the cases needed.
I tried with this. It works but it is slow and I am not able to plot the dates at the end. Anyway:
table= readtable ('filename.xlsx');
dates = table(:,1);
dates = table2array (dates);
dates = datenum(dates);
dates = datestr (dates);

Lotus Notes: Displaying days of a month, following the weekdays of it

I have a not so nice question. I've been thinking about this for like a month now and read a couple of books but can seem to find an answer or how to execute this. As you all know, I'm making this application that generates date. I have one combobox it has months in it, starting january to december, two column table, first colum displays the day and the second one displays the weekdays, on selecting month combobox, it must display the days in that month on first column and weekdays on 2nd column, by row. example: I choose january, on the first column it will display the days of that month, 1st row is 1, 2nd row is 2, and so on, and same as weekdays. I'm kinda new to LN. Can you give me an idea for it? Hope you can help me out.
This is a solution based on Notes #Formula. Only a few lines of code are necessary to achieve the result.
First we need the form
The formula for Days is
_days :=1:2:3:4:5:6:7:8:9:10:11:12:13:14:15:16:17:18:19:20:21:22:23:24:25:26:27:28:29:30:31;
_numberDays := #Day(#Adjust(#Adjust(#Date(#ToNumber(Year); #ToNumber(Month); 1); 0; 1; 0; 0; 0; 0); 0; 0; -1; 0; 0; 0));
#Subset(_days; _numberDays)
and the formula for Weekday is
_weekdays := #Transform( #ToNumber(Days); "day";
#Weekday(#Date(#ToNumber(Year); #ToNumber(Month); day)));
#Replace(#Text(_weekdays);
"1":"2":"3":"4":"5":"6":"7";
"Sunday":"Monday":"Tuesday":"Wednesday":"Thursday":"Friday":"Saturday")
That's it.
The fields Month and Year have to have the property "Refresh fields on keyword change".
The fields Days and Weekday need "Computed for display", "Allow multiple values" and "New Line" as separate values.
The result will look like this
Truly dynamic tables are difficult. In this case it's definitely possible because you have a defined number of rows, but it's still somewhat messy.
I'm not aware of anything built in that will easily tell you how many days there are in each month. I'd probably just create a config doc with year, month, and numberOfDays fields, and hidden view that you can use for lookups. You're going to need this in many places, and you don't want to do the lookup each time, so do it in a hidden computed field that comes after your dropdown but before your table. (Be sure to set properties so the field is recomputed after the value in the dropdown is changed.) Call the field something like daysInSelectedMonth.
Obviously the first column is easy: just create your table and enter the numbers 1 through 31, and apply a hide-when formula to the cells for rows 29 through 31 so that they only show up if daysInSelectedMonth is the right number of days. You don't need the hide when in the other rows.
For the second column, you will need to use computed for display fields. I would strongly suggest naming them something like weekday_1, weekday_2,... weekday_31 so that you can use #ThisName and some simple string manipulation to extract the number from the field name. That will tell you what row the formula is in, and it is your day number. The benefit of doing it this way is that your formula can be exactly the same in every one of the fields -- just a cut-and-paste into the other fields after you get it right once.
I would suggest starting to work on the formula in the weekday_31 field, and when you get it right (showing the correct weekday in a month that does have 31 days, and blank in a month that does not), then you can copy the formula to the rest of the fields. You will need to use an #If to detect whether the month has the correct number of days -- this is easy, except for leap year. I'm going to leave that part up to you. Just make it return "" if the month does not have the right number of days, and then have the final clause of the #f use #Date to build the value for the date that you are working on and then use the #Weekday function to display the value.
It all depends on a few things:
Web application or Notes client application?
What is the end result of the exercise, i.e. what is the table intended to be used for? Display purposes only?
Making some assumptions (Notes client application, and table used only for display), I see two ways to do this.
An easy way to do this is to create the table with 31 rows and 2 columns.
In the cells you create fields: Day1, Weekday1, Day2, Weekday2, etc.
You also need a year field, unless it is always current year.
Set a hide-when formula on rows 29-31, to hide if the Day field for that row is blank.
On the advanced properties tab for the combobox where you select month, set "Run Exiting/OnChange events after value change".
In the Exiting event for the combobox, write some Lotusscript that populate the fields with days and weekdays, based on selected year and month. Something like this (untested:
Sub Exiting(Source As Field)
Dim session As New NotesSession
Dim ws As New NotesUIWorkspace
Dim uidoc As NotesUIDocument
Dim monthName As String
Dim YYYY As String
Dim firstDate As NotesDateTime
Dim lastDate As NotesDateTime
Dim n As Double
Dim i As Integer
Dim dayname(1 To 7) As String
dayname(1) = "Sunday"
dayname(2) = "Monday"
dayname(3) = "Tuesday"
dayname(4) = "Wednesday"
dayname(5) = "Thursday"
dayname(6) = "Friday"
dayname(7) = "Saturday"
Set uidoc = ws.CurrentDocument
YYYY = uidoc.FieldGetText("Year")
monthName = uidoc.FieldGetText("Month")
Set firstDate = New NotesDateTime("1 " & monthName & ", " & YYYY)
Set lastDate = New NotesDateTime(firstDate.DateOnly)
Call lastDate.AdjustMonth(1)
Call lastDate.AdjustDay(-1)
i = 0
For n = Cdbl(firstDate.LSLocalTime) To Cdbl(lastDate.LSLocalTime)
i = i + 1
Call uidoc.FieldSetText("Day" & i, Cstr(i))
Call uidoc.FieldSetText("Weekday" & i, dayname(Weekday(Cdat(n))))
Next
Call uidoc.Refresh()
End Sub
Another way to create a truly dynamic table, is the method I blogged about here:
http://blog.texasswede.com/dynamic-tables-in-classic-notes/
The benefit is that it is more flexible, and you can create a nicer layout without needing to create a large number of fields.

Using POI to write date (without time)

When i write out a date using POI, I also see the time in Excel. (the time shown is the time on my machine when the cell was written).
This is the code that i have used
XSSFCellStyle dateStyle = wb.createCellStyle();
CreationHelper createHelper = wb.getCreationHelper();
dateStyle.setDataFormat(createHelper.createDataFormat().getFormat("dd-mmm-yyyy"));
Calendar cal = Calendar.getInstance();
cal.set(xx.getYear(), xx.getMonthOfYear(), xx.getDayOfMonth());
cell.setCellStyle(dateStyle);
cell.setCellValue(cal);
the date in the cell is correct i.e 12-Dec-2013 but for that cell, IN THE FORMULA bar, the time also shows. So cell shows 12-Dec-2013 and the formula bar show 12-Dec-2013 7:14:39AM. I checked the format of the cell in excel and it shows as custom dd-mm-yyyy, which is what i expect.
Just too be clear - the cell itself show 12-12-2012 but for that cell in the formula bar the time also shows.
I also replaced Calendar with Date - same issue.
Addl info: In excel i changed the format of the col to 'general' - for the cells that were addined in by POI, i see that the values is xxx.xxx like 41319.3769490278, while when i just enter the date by hand the value looks something like 41319. It looks like the digits after the decimal point is causing the time to show up. Not sure how to avoid this when i use POI to write it out
Ok solved. Putting this out there for others who run into the same problem.
i looked into the POI source code and realized that from the calendar, a double is computed, and the cell value is set to that. From the comments its clear that the digits after the decimal point represent the time. So all i did in my code is to truncate that double. The changed lines are commented in the code snippet below.
XSSFCellStyle dateStyle = wb.createCellStyle();
CreationHelper createHelper = wb.getCreationHelper();
dateStyle.setDataFormat(createHelper.createDataFormat().getFormat("dd-mmm-yyyy"));
Calendar cal = Calendar.getInstance();
cal.set(xx.getYear(), xx.getMonthOfYear(), xx.getDayOfMonth());
cell.setCellStyle(dateStyle);
double d = DateUtil.getExcelDate(cal, false); //get double value f
cell.setCellValue((int)d); //get int value of the double
You're not fully clearing the Calendar instance you're getting the date from, that's why the time is coming through
You need to also set the time values to zero, so your code would want to look something like:
Calendar cal = Calendar.getInstance();
cal.set(xx.getYear(), xx.getMonthOfYear(), xx.getDayOfMonth());
calendar.set(Calendar.HOUR, 0);
calendar.set(Calendar.MINUTE, 0);
calendar.set(Calendar.SECOND, 0);
calendar.set(Calendar.MILLISECOND, 0);

Resources