[Solved] WEEKNUM + text

Discuss the spreadsheet application
Post Reply
amDEwolff
Posts: 4
Joined: Sun Apr 20, 2008 9:53 pm

[Solved] WEEKNUM + text

Post by amDEwolff »

Hello,

Spreadsheet (version 4.1.7) contains 52 tabs, one per week.

Function WEEKNUM needs to return year (4 digits), <week> as a written word, week number (2 digits).
I entered =YEAR(C2)&"week" &WEEKNUM(C2; 2) which gives me without spaces: 2024week1

However output should read: 2024 week 1

1) How can I alter the formula so as to add single spaces in between?
2) How to add a leading zero to week number 1 up to week number 9?
3) The formula does work, but does it look crisp and elegant as well (not sure)?

Thanks.
Last edited by Hagar Delest on Sun May 12, 2024 9:01 pm, edited 1 time in total.
User avatar
MrProgrammer
Moderator
Posts: 4948
Joined: Fri Jun 04, 2010 7:57 pm
Location: Wisconsin, USA

Re: weeknum + text

Post by MrProgrammer »

amDEwolff wrote: Sun May 12, 2024 2:55 pm Function WEEKNUM needs to return year (4 digits), <week> as a written word, week number (2 digits).
WEEKNUM only returns the week number. WEEKNUM cannot provide the year, nor can it add text. To put 2024 week 01 in a cell, you must use the YEAR function and the concatenation operator, as you have done.

amDEwolff wrote: Sun May 12, 2024 2:55 pm I entered =YEAR(C2)&"week" &WEEKNUM(C2; 2) which gives me without spaces: 2024week1.
1) How can I alter the formula so as to add single spaces in between?
2) How to add a leading zero to week number 1 up to week number 9?
=YEAR(C2)&" week "&TEXT(WEEKNUM(C2;2);"00")

amDEwolff wrote: Sun May 12, 2024 2:55 pm 3) The formula does work, but does it look crisp and elegant as well (not sure)?
Any crisp and elegant formulas would be of little value if you don't understand how they work. The formula above is what I would use. Parameter 00 tells the TEXT function to format the week number as two digits.

To learn more about the WEEKNUM function see sections E and F in [Tutorial] Calc date formulas. Perhaps you want WEEKNUM_ADD instead of WEEKNUM or you want type 1 instead of type 2. Those sections explain the differences.

If this solved your problem please go to your first post use the Edit button and add [Solved] to the start of the Subject field. Select the green checkmark icon at the same time.
Mr. Programmer
AOO 4.1.7 Build 9800, MacOS 13.6.3, iMac Intel.   The locale for any menus or Calc formulas in my posts is English (USA).
amDEwolff
Posts: 4
Joined: Sun Apr 20, 2008 9:53 pm

Re: WEEKNUM + text

Post by amDEwolff »

I had been searching for answers in excel help forums. I would not have figured out placing the bit TEXT the way you did.
Thanks a lot.
User avatar
karolus
Volunteer
Posts: 1164
Joined: Sat Jul 02, 2011 9:47 am

Re: [Solved] WEEKNUM + text

Post by karolus »

Hallo

Code: Select all

=TEXT(C2; "yyyy \w\e\e\k ww")
AOO4, Libreoffice 6.1 on Rasbian OS (on ARM)
Libreoffice 7.4 on Debian 12 (Bookworm) (on RaspberryPI4)
Libreoffice 7.6 flatpak on Debian 12 (Bookworm) (on RaspberryPI4)
User avatar
MrProgrammer
Moderator
Posts: 4948
Joined: Fri Jun 04, 2010 7:57 pm
Location: Wisconsin, USA

Re: [Solved] WEEKNUM + text

Post by MrProgrammer »

karolus wrote: Mon May 13, 2024 12:34 am =TEXT(C2; "yyyy \w\e\e\k ww")
Number format code WW gives the value of WEEKNUM(date;1) and uses 1 digit for the first nine weeks. Poster amDEwolff asked for WEEKNUM(date;2) and 2-digit numbers for those weeks.

=YEAR("2024-01-07")&" week "&TEXT(WEEKNUM("2024-01-07";2);"00") → 2024 week 01
=TEXT("2024-01-07";"YYYY \w\e\e\k WW")                          → 2024 week 2
Mr. Programmer
AOO 4.1.7 Build 9800, MacOS 13.6.3, iMac Intel.   The locale for any menus or Calc formulas in my posts is English (USA).
User avatar
karolus
Volunteer
Posts: 1164
Joined: Sat Jul 02, 2011 9:47 am

Re: [Solved] WEEKNUM + text

Post by karolus »

MrProgrammer wrote: Mon May 13, 2024 4:13 am
karolus wrote: Mon May 13, 2024 12:34 am =TEXT(C2; "yyyy \w\e\e\k ww")
Number format code WW gives the value of WEEKNUM(date;1) and uses 1 digit for the first nine weeks. Poster amDEwolff asked for WEEKNUM(date;2) and 2-digit numbers for those weeks.

=YEAR("2024-01-07")&" week "&TEXT(WEEKNUM("2024-01-07";2);"00") → 2024 week 01
=TEXT("2024-01-07";"YYYY \w\e\e\k WW")                          → 2024 week 2
I cannot see any difference between (except 1st returns Text and the 2nd returns numeric values:

Code: Select all

=TEXT(A2; "ww" )
=WEEKNUM(A2;2)
with me:

Code: Select all

Version: 7.4.7.2 / LibreOffice Community
Build ID: 40(Build:2)
CPU threads: 4; OS: Linux 6.6; UI render: default; VCL: gtk3
Locale: de-DE (de_DE.UTF-8); UI: de-DE
Debian package version: 4:7.4.7-1+deb12u1
Calc: threaded
weeknum.png
weeknum.png (73.93 KiB) Viewed 323 times
AOO4, Libreoffice 6.1 on Rasbian OS (on ARM)
Libreoffice 7.4 on Debian 12 (Bookworm) (on RaspberryPI4)
Libreoffice 7.6 flatpak on Debian 12 (Bookworm) (on RaspberryPI4)
User avatar
MrProgrammer
Moderator
Posts: 4948
Joined: Fri Jun 04, 2010 7:57 pm
Location: Wisconsin, USA

Re: [Solved] WEEKNUM + text

Post by MrProgrammer »

karolus wrote: Mon May 13, 2024 7:19 am I cannot see any difference between (except 1st returns Text and the 2nd returns numeric values)
That's interesting. They are the same for you. But they are different for me. For both of us =WEEKNUM("2024-01-07";2) is numeric 1. For you, =TEXT("2024-01-07";"WW") is text 1. But for me, =TEXT("2024-01-07";"WW") is text 2 because the result of format code WW depends on the locale! Mine is English (Canada).

amDEwolff wrote: Sun May 12, 2024 2:55 pm How to add a leading zero to week number 1 up to week number 9?
Code WW does not pad single-digit weeks with a leading zero, as other codes like MM and DD do, so WW doesn't seem like a good solution for amDEwolff.
Mr. Programmer
AOO 4.1.7 Build 9800, MacOS 13.6.3, iMac Intel.   The locale for any menus or Calc formulas in my posts is English (USA).
Post Reply