Qlik Community

New to Qlik Sense

If you’re new to Qlik Sense, start with this Discussion Board and get up-to-speed quickly.

Announcements
Tell us which business and trade publications you read most regularly: RESPOND NOW
cancel
Showing results for 
Search instead for 
Did you mean: 
qlikeers
Creator
Creator

Current month in variable

Hi!

I have such a prosaic problem. How do I create a variable that will contain the month in the form "YYYY/MM"? 

I have let xxx = peek ('abc') where abc is the month in the correct format. But a variable divides two numbers by itself instead of keeping them as text. I also tried SET instead of LET but then peek doesn't work.

1 Solution

Accepted Solutions
tresesco
MVP
MVP

Where and how are you using this variable? If you are using it in the front end - try using it without $ expansion, or with single quotes around the dollar expansion.

vCurrMonth    or  '$(vCurrMonth)'

View solution in original post

9 Replies
Joseph_Musekura
Support
Support

Hi,

I am  not sure if I understand what you want to achieve.
Do you have only month as field in your data model? If you also have a date, you could well  extract "YYYY/MM" from the date field and then create a variable to contain that new field (ex: YearMonth)

------- ex

Load *,
year(Date) & '/' & Month(Date) as YearMonth

.

.

---------

qlikeers
Creator
Creator
Author

In the data model, I have exactly what you wrote.

Now I want to save this month (exactly in the same form) in a variable. Now the variable assigns me, for example, 168.4166666666667 (So the result of the division) instead of 2021/12.

Joseph_Musekura
Support
Support

Hi @qlikeers ,
I just did a quick check using set vMonth = YearMonth; in the script and the result is as expected

the result is displayed in "text &image" object.

set variable vMonth.png

Which version of Qlik sense are you using?

Can you share your sample QVF?
Regards

Joseph_Musekura
Support
Support

Sorry!
I just noticed that you are using for example numeric 12 as month instead of text "Dec". Did you used dual() in your script?

Please share your sample qvf
/joseph

qlikeers
Creator
Creator
Author

No, I didn't use dual. How do I get this to work?

Joseph_Musekura
Support
Support

Hi @qlikeers ,
To make it easy, share your qvf (make it very sample, few lines in scripts + 1 sheet showing the value-result of used variable).
I am not sure to check it directly because I am busy today.
/joseph

qlikeers
Creator
Creator
Author

I don't really know how to do it, because I don't have access to disk space.

But here I really wrote everything 🙂

In the code:

(...)
date (num (AddYears (date (Max (% JOIN_InvoiceDate)), 0)), 'YYYY / MM') as% Curr_Month,
(...)

And then:
let vCurrMonth = peek('%Curr_Month');

In this case, the variable holds the result of dividing the year by the month (e.g. 168.416666 as the result of dividing 2021/12), instead of the string '2021/12'.

tresesco
MVP
MVP

Where and how are you using this variable? If you are using it in the front end - try using it without $ expansion, or with single quotes around the dollar expansion.

vCurrMonth    or  '$(vCurrMonth)'

qlikeers
Creator
Creator
Author

Of course!

'$(vCurrMonth)' - it works fine. Thanks @tresesco!