Skip to main content
Woohoo! Qlik Community has won “Best in Class Community” in the 2024 Khoros Kudos awards!
Announcements
Nov. 20th, Qlik Insider - Lakehouses: Driving the Future of Data & AI - PICK A SESSION
cancel
Showing results for 
Search instead for 
Did you mean: 
marygrace
Contributor III
Contributor III

Is possible to use if statement into variable calculation?

Hi,

I am trying to use if condition into variable calculation. I need to identify if day of the week is Monday to go back 2 days until Saturday.

the expresions works well, I valideted both,  however the if condition is not working well.

if( num(weekday(Date)) = 1,

Count(

     {$

          <Date={"$(=Date(max(Date),'DD/MM/YYYY'))"}, _Id_Flag={0},Estatus_Cobranza= {'EN SURTIDO','FACTURADO','ENTREGADO','CONSOLIDADO'}, Liberado={1}, TipoDeEntrega -= {'SOLO FACTURA'},Year={$(vAñoActual)},Month=,Week=>

          +

          <Date={"$(=Date(max(Date)-2,'DD/MM/YYYY'))"}, _Id_Flag={0},Estatus_Cobranza= {'EN SURTIDO','FACTURADO','ENTREGADO','CONSOLIDADO'}, Liberado={0}, TipoDeEntrega -= {'SOLO FACTURA'},Year={$(vAñoActual)},Month=,Week=>

     }

     distinct Numero_OrdenVenta),

    

Count(

     {$

          <Date={"$(=Date(max(Date),'DD/MM/YYYY'))"}, _Id_Flag={0},Estatus_Cobranza= {'EN SURTIDO','FACTURADO','ENTREGADO','CONSOLIDADO'}, Liberado={1}, TipoDeEntrega -= {'SOLO FACTURA'},Year={$(vAñoActual)},Month=,Week=>

          +

          <Date={"$(=Date(max(Date)-1,'DD/MM/YYYY'))"}, _Id_Flag={0},Estatus_Cobranza= {'EN SURTIDO','FACTURADO','ENTREGADO','CONSOLIDADO'}, Liberado={0}, TipoDeEntrega -= {'SOLO FACTURA'},Year={$(vAñoActual)},Month=,Week=>

     }

     distinct Numero_OrdenVenta))

    

Somebody can help me?

thank you!

10 Replies
agigliotti
Partner - Champion
Partner - Champion

where are you using that variable ?

marygrace
Contributor III
Contributor III
Author

Into chart: table

MK9885
Master II
Master II

I guess it should work. You are just storing the expression as is into variable.

If you expression is working without adding it to variable then break it down and use 2 variable instead.

Maybe use

Variable1:

Count(

     {$

          <Date={"$(=Date(max(Date),'DD/MM/YYYY'))"}, _Id_Flag={0},Estatus_Cobranza= {'EN SURTIDO','FACTURADO','ENTREGADO','CONSOLIDADO'}, Liberado={1}, TipoDeEntrega -= {'SOLO FACTURA'},Year={$(vAñoActual)},Month=,Week=>

          +

          <Date={"$(=Date(max(Date)-2,'DD/MM/YYYY'))"}, _Id_Flag={0},Estatus_Cobranza= {'EN SURTIDO','FACTURADO','ENTREGADO','CONSOLIDADO'}, Liberado={0}, TipoDeEntrega -= {'SOLO FACTURA'},Year={$(vAñoActual)},Month=,Week=>

     }

     distinct Numero_OrdenVenta)

Variable2:

  

Count(

     {$

          <Date={"$(=Date(max(Date),'DD/MM/YYYY'))"}, _Id_Flag={0},Estatus_Cobranza= {'EN SURTIDO','FACTURADO','ENTREGADO','CONSOLIDADO'}, Liberado={1}, TipoDeEntrega -= {'SOLO FACTURA'},Year={$(vAñoActual)},Month=,Week=>

          +

          <Date={"$(=Date(max(Date)-1,'DD/MM/YYYY'))"}, _Id_Flag={0},Estatus_Cobranza= {'EN SURTIDO','FACTURADO','ENTREGADO','CONSOLIDADO'}, Liberado={0}, TipoDeEntrega -= {'SOLO FACTURA'},Year={$(vAñoActual)},Month=,Week=>

     }

     distinct Numero_OrdenVenta))

This into Variable and if( num(weekday(Date)) = 1, separate?


Ex:

if( num(weekday(Date)) = 1, $(Variable1),$(Variable2))


Not sure if it will work... but I usually break down large expressions into different sets of variable.

agigliotti
Partner - Champion
Partner - Champion

to go further you should provide a sample document with your issue.

marygrace
Contributor III
Contributor III
Author

I created the both variables, I can see the both variables return the result well. However the if sentence does not works well, looks like the condition is not valide becase does not return the first variable, just the second one.

agigliotti
Partner - Champion
Partner - Champion

it could be caused by your table dimensions.

which dimensions and measures are you using in table object?

marygrace
Contributor III
Contributor III
Author

Before insert into table I am using KPI to see the number, I think my problem is the if condition, I should SET in Main something.... like SET FirstWeekDay=6;

agigliotti
Partner - Champion
Partner - Champion

what you get with the below expression ?

=num(weekday(today()))

marygrace
Contributor III
Contributor III
Author

Well I need to use Date, because the measurement is any date. Any way I set "SETFirstWeekDay=0" ; and now IF statement Works.

Thank you for your help, I really appreciated