Skip to main content
Announcements
Qlik Connect 2024! Seize endless possibilities! LEARN MORE
cancel
Showing results for 
Search instead for 
Did you mean: 
DanCasSan
Contributor III
Contributor III

Sort months chronologically

Hello community!

 

Can you help me with the ordering of the months for my pivot table and line chart chronologically (Jan, Feb, Mar...)?

DanCasSan_1-1655663065234.png

 

The dimension for the months is calculated as follows:

=If(GetSelectedCount([Tiempo.Año])=0 or GetFieldSelections([Tiempo.Año])=Year(Now()),
    If([Tiempo.Mes numérico]<=Num(Month(today())),
        [Tiempo.Mes descripción]&' '&vAñoActual,
        [Tiempo.Mes descripción]&' '&(vAñoActual-1)
    ),
    If(GetFieldSelections([Tiempo.Año])<>Year(Now()) and GetSelectedCount([Tiempo.Año])=1,
        If([Tiempo.Mes numérico]<=Num(Month(today())),
            [Tiempo.Mes descripción]&' '&vMaxAño,
            [Tiempo.Mes descripción]&' '&vMaxAñoAnterior
        )
))

 

When I do a month filter, the table and the graph get out of order, I have even configured them in the sort by expression section, numerically and alphabetically, without achieving the chronological order.

 

Note: for the calculation of my measures, in the set analysis I am indicating that the month filter is not applied to the table, this is the expression that I am using:

=If(GetSelectedCount([Tiempo.Año])=0 or GetFieldSelections([Tiempo.Año])=Year(Now()),
    If([Tiempo.Mes numérico]<=Num(Month(today())),
        Sum({<[Puesto]={"12"},[Tiempo.Año]={"$(vAñoActual)"},[Tiempo.Mes numérico]=>} [Precipitación]),
        Sum({<[Puesto]={"12"},[Tiempo.Año]={"$(vAñoAnterior)"},[Tiempo.Mes numérico]=>} [Precipitación])
    ),
    If(GetFieldSelections([Tiempo.Año])<>Year(Now()) and GetSelectedCount([Tiempo.Año])=1,
        If([Tiempo.Mes numérico]<=Num(Month(today())),
            Sum({<[Puesto]={"12"},[Tiempo.Año]={"$(vMaxAño)"},[Tiempo.Mes numérico]=>} [Precipitación]),
            Sum({<[Puesto]={"12"},[Tiempo.Año]={"$(vMaxAñoAnterior)"},[Tiempo.Mes numérico]=>} [Precipitación])
        )
))

 

Regards,

DanCas

Labels (6)
7 Replies
MarcoWedel

Without completely changing your expressions maybe it is sufficient to calculate the dimensions as proper dates, i.e. not just as concatenated strings, so their underlying numerical values can take care of the sort order.

Can you please try replacing the 

 

[Tiempo.Mes descripción]&' '&vAñoActual,
[Tiempo.Mes descripción]&' '&(vAñoActual-1)

 

bit with something like

 

Date(MakeDate(vAñoActual,[Tiempo.Mes numérico]),'MMMM YYYY')
Date(MakeDate(vAñoActual-1,[Tiempo.Mes numérico]),'MMMM YYYY')

 

 

and 

 

Date(MakeDate(vMaxAño,[Tiempo.Mes numérico]),'MMMM YYYY')
Date(MakeDate(vMaxAñoAnterior,[Tiempo.Mes numérico]),'MMMM YYYY')

 

instead of

 

[Tiempo.Mes descripción]&' '&vMaxAño,
[Tiempo.Mes descripción]&' '&vMaxAñoAnterior

 

respectively.


Hope this helps

Marco

 

 

luiferva
Contributor III
Contributor III

Hola  DanCasSan  puedes usar la funcion  Macth(FieldName,'Enero','February') etc.

DanCasSan
Contributor III
Contributor III
Author

Hola @luiferva, he utilizado la función indicada, pero, me salen los números de los meses y no sus nombres:

=If(GetSelectedCount([Tiempo.Año])=0 or GetFieldSelections([Tiempo.Año])=Year(Now()),
    If([Tiempo.Mes numérico]<=Num(Month(today())),
        Match([Tiempo.Mes descripción],'ENERO','FEBRERO','MARZO','ABRIL','MAYO','JUNIO','JULIO','AGOSTO','SETIEMBRE','OCTUBRE','NOVIEMBRE','DICIEMBRE')&' '&vAñoActual,
        Match([Tiempo.Mes descripción],'ENERO','FEBRERO','MARZO','ABRIL','MAYO','JUNIO','JULIO','AGOSTO','SETIEMBRE','OCTUBRE','NOVIEMBRE','DICIEMBRE')&' '&vAñoActual-1
    ),
    If(GetFieldSelections([Tiempo.Año])<>Year(Now()) and GetSelectedCount([Tiempo.Año])=1,
        If([Tiempo.Mes numérico]<=Num(Month(today())),     
        	Match([Tiempo.Mes descripción],'ENERO','FEBRERO','MARZO','ABRIL','MAYO','JUNIO','JULIO','AGOSTO','SETIEMBRE','OCTUBRE','NOVIEMBRE','DICIEMBRE')&' '&vMaxAño,
        	Match([Tiempo.Mes descripción],'ENERO','FEBRERO','MARZO','ABRIL','MAYO','JUNIO','JULIO','AGOSTO','SETIEMBRE','OCTUBRE','NOVIEMBRE','DICIEMBRE')&' '&vMaxAñoAnterior            
        )
	)
)

DanCasSan_0-1655702408613.png

¿Qué me estaría faltando?

Saludos,

@DanCasSan 

DanCasSan
Contributor III
Contributor III
Author

Hi @MarcoWedel, it is not returning the expected result and the order always leaves the selected month first, as shown in the image:

DanCasSan_1-1655703428370.png

Regards,

DanCas

 

MarcoWedel

The order of months seems to be chronological in your screenshot, therefore I don't follow.

DanCasSan
Contributor III
Contributor III
Author

Hi @MarcoWedel , thanks for your response. What I need to appear in the table is the following structure:

January 2022
February 2022
March 2022
April 2022
May 2022
June 2022
July 2021
August 2021
September 2021
October 2021
November 2021
December 2021

The order must be with respect to the months and descending to the years

Regards,

DanCas

luiferva
Contributor III
Contributor III

Hola DanCasSan

Y si lo intentas ordenar por expresión?

pegas el codigo que saca las fechas, y lo ordenas ascendente.