How do I format a concatenated date in excel? - excel

I have a formula to convert a date in an excel column
=CONCATENATE(YEAR([#[Admission Date]]),"-",MONTH([#[Admission Date]]))
It produces a date like this "2014-1." For sorting purposes I need the date to look like this "2014-02" Is there a way to write that into the formula?
Thanks

Try,
=DATE(YEAR([#[Admission Date]]), MONTH([#[Admission Date]]), 1)
' as strictly text (not recommended)
=TEXT([#[Admission Date]], "yyyy-mm")
Format the column as yyyy-mm. Always work with real numbers and real dates whenever you can. There is more than enough variations available with Number Format Code to make it look like you want while rtaining an underlying numerical value.

This should be simple enough if you add an "if" statement so that it will add a "0" before the number if it's < 10
=CONCATENATE(YEAR(A1),"-",IF(MONTH(B1)>10,MONTH(B1),CONCATENATE("0",MONTH(B1))))
The logic behind it is: if "b1 > 10" then (leave it as is) else concatenate("0",month(b1))

Related

Categorical column in to a dummy array in Excel

I'm trying to translate a machine-learning model into excel, so that data analysts could play with it interactively.
I'd like to transform a categorical variable into dummy representation:
WeekDay
Monday
Thursday
to
WeekDay
{1,0,0,0,0,0,0}
{0,0,0,1,0,0,0}
Using excel arrays.
I tried this:
={INT(A1="Monday"),INT(A1="Tuesday"),INT(A1="Wednesday"), ...}
However, for some reason, excel doesn't accept forumlas in array expressions.
This approach does work, but it is problematic - since it does not allow combinig multiple arrays into one
=IF(A1="Monday", {1,0,0,0,0,0,0}, IF(A1="Tuesday", {0,1,0,0,0,0,0}, ....))
Also, it's super ugly
Any ideas ?
To get your array you can use INDEX like this:
INDEX(IF(TEXT(ROW($2:$8),"dddd")=A1,1,0),0)
This returns a vertical array.
to return a horizontal array use:
INDEX(IF(TEXT(COLUMN($B:$H),"dddd")=A2,1,0),0)
I have spilled the results of the array in the photo below:
If one has the Dynamic Array Formula SEQUENCE the ROW and COLUMN can be replaced with:
SEQUENCE(7,,2)
and
SEQUENCE(,7,2)
Respectively
I am not sure what the end goal is, but I would suggest instead of an array (again, assuming you are not utilizing VBA, and even if you are, this is very possible with Case functions):
Use the formula =Weekday(Cell,different return type for which day should = 1)
If you were to use the actual date (e.g. 5/1/2020), display it through custom formatting with only the day of the week (typically data analysis will already have the full date),
Cell Value A20 = "5/1/2020", format display Long Date - "Friday, May 1, 2020"
cell referencing with formula '=WEEKDAY(A20,1)' = 6
cell referencing with formula '=WEEKDAY(A20,2)' = 5 (is this similar to {0,0,0,0,1,0,0} enough for you to use?)
if using VBA, you could define a range with logic to turn this into {0,0,0,0,1,0,0}
Hope this helps.

Handling numbers as text in sumifs formula

I have a sumifs formula with a filter range on a column with a year. Sometime this year is formatted as a number, other times it is formatted as text.
How can I make my sumifs formula flexible enough to handle both years formatted as numbers and as text?
For example, this formula returns incorrect results when passed a year formatted as text:
=SUMIFS(C3:C6,B3:B6,"<>2016")
"Text that looks like a number" (whether in the form '2016 or ="2016") can easily be turned into a "number" by adding 0 to it.
Try this, which should yield the correct result regardless of whether the B3:B6 range is a number or text that looks like a number:
= SUMIFS(C3:C6,(B3:B6)+0,"<>2016")
EDIT
You may have to use SUMPRODUCT here instead, e.g.
= SUMPRODUCT(C3:C6,(((B3:B6)+0)<>2016)+0)
Assuming you are not using the ' prefix, mentioned by Jeeped, one way to do it is:
=SUM(C3:C7)-SUMIFS(C3:C7,B3:B7,"2016",B3:B7,2016)

Excel Formula get numbers from cell using mid and convert to date?

I have a 12 digit number in column A like so:
Column A
041120121601
Within this number there is a date 201216 or 20/12/16.
I am trying to get the date from the number using mid:
=MID(B8,5,6)
I now get this as a result: 201216.
I am trying to format this as a date like this:
20/12/2016
Here's what i've tried:
=TEXT(D8, "00-00-00")
This gives me this:
20-12-16
This is close, but i need it to be 20/12/16
OK, if it starts with a 0, Excel doesn't think this is a number. It thinks it's text. This is important, because one day someone will copy and paste the input data and Excel will automagically convert them to numbers and your formulas all
will break because the leading 0 is missing now.
So, let's first convert the cell into a number, then back into text with "000000000000" format:
=Text(Value(A1),"000000000000")
Then apply the DateValue formula with three Mid functions as Tim describes.
(Or your input data are numbers in "000000000000" format, but that's a little unusual.)
You can your split MID and force a date separator betwixt each number and wrap it in DATEVALUE, then just format as a date.
=DATEVALUE(MID(B8,5,2)&"/"&MID(B8,7,2)&"/"&MID(B8,9,2))
If it's always the same amount of characters then =MID(TRIM(A1),4,2)& "/" & MID(TRIM(A1),6,2) & "/" & MID(TRIM(A1),8,2)

Excel convert date to text retaining date layout

I have rows containing every Monday and Sunday in an entire year, (with variations on when a month starts and ends) like so,
11/05/2015 18/05/2015 25/05/2015 01/06/2015 08/06/2015
17/05/2015 24/05/2015 31/05/2015 07/06/2015 14/06/2015
However these are in the date format, and I need them in a text format, but so they still read in the dd/mm/yyyy format, not like 42125.
Further up my document, each column header should read dd/mm/yyyy-dd/mm/yyyy using each of the dates shown in my first example, and I was hoping to achieve this using the formula =A30&"-"&A31 and so on. So the desired outcome should read,
11/05/2015-17/05/2015 18/05/2015-24/05/2015
11/05/2015 18/05/2015
17/05/2015 24/05/2015
However using the =cell&cell formula im left with
42135-42141 42142-42148
11/05/2015 18/05/2015
17/05/2015 24/05/2015
I have to create these headings for 2 years worth of dates, and trying to avoid typing every heading out manually, is there a way to achieve this?
Use the TEXT function which comes with Excel.
Assuming cell A1 has "11/05/2015", use the following formula in cell B1:
B1=TEXT(A1,"dd/mm/yyyy")
This takes care of the first part of your question. For the second part, you can use the CONCATENATE function. Assuming that B1 and B2 contain dates as text, you can join them together using the following formula:
=CONCATENATE(B1, "-", B2)
Try converting those values to text before concatenating:
=TEXT(A1,"dd/mm/yyyy")&"-"&TEXT(A2,"dd/mm/yyyy")
You need to break them down like so:
=DAY(A3)&"/"&MONTH(A3)&"/"&YEAR(A3)&"-"&DAY(A4)&"/"&MONTH(A4)&"/"&YEAR(A4)
I am assuming here your data start from cell A3
Assuming headings in row 1 and dates in rows 3 and 4 this will work:
=TEXT(A3,"dd/mm/yyyy")&TEXT(A4," - dd/mm/yyyy")
without having to concatenate " - "

How to convert a text string into hours

I have data in the format as d.hh.mm.ss and hh.mm.ss and I need to conver both into a time value of HH.MM.SS
In A1 the text string is 16.21:38:27
in A2 the text string is 04:08:45
My resulting cell should show B1 405:38:27
B2 04:08:45
I have tried using =LEFT(A1,SEARCH(":",A1)-1)+TIMEVALUE(MID(A1,SEARCH(":",A1)+1,255))
but the results are wrong
A1 is shown as 403:29:24
A2 is shown as 104:45:00
I would like one formula for both strings
As 4:08:45 is a valid time format and 16.21:38:27 is not this formula should work for you in both cases:
=IF(ISNUMBER(A1+0),A1+0,RIGHT(A1,8)+LEFT(A1,FIND(".",A1)-1))
format result cells as [h]:mm:ss
Here it is:
= IF(ISERROR(FIND(".",A1)),
TIMEVALUE(A1),
VALUE(LEFT(A1,FIND(".",A1)-1)) + TIMEVALUE(MID(A1, FIND(".", A1)+1, 8)))
If a "." doesn't exist in the string, it simply uses TIMEVALUE() to parse the time. Otherwise, it parses the portion before and after the "." separately. The number of days forms the integer portion of the result, and the time is computed as a fractional day. This is then formatted using the standard formatting dialog into [h]:mm:ss format.
If you want B1 to store a string of the converted hours/minutes/seconds rather than a formatted number, wrap the whole of the above in TEXT(formula above, "[h]:mm:ss").
#TimWilliams hit the nail on the head. You have a typo. Change 16.21:38:27 to 16:21:38:27 and it will work fine. You can additionally wrap your formula to check the length. If it is more than 8 Chars then it means a date is added to it. See this example
Try this (You can use this for both)
=IF(LEN(A1)>8,LEFT(A1,SEARCH(":",A1)-1)+TIMEVALUE(MID(A1,SEARCH(":",A1)+1,255)),TIMEVALUE(A1))
SNAPSHOT
EDIT
I just noticed that you have hardcoded 255 in your formula. You don't need to do that. This will also work.
=IF(LEN(A1)>8,LEFT(A1,SEARCH(":",A1)-1)+TIMEVALUE(MID(A1,SEARCH(":",A1)+1,LEN(A1)-SEARCH(":",A1)+1)),TIMEVALUE(A1))
BTW, to make it foolproof you make also add the TRIM function to the above formula as well...

Resources