Qlik Community

New to Qlik Sense

Discussion board where members can get started with Qlik Sense.

marygrace
New Contributor II

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
Honored Contributor II

Re: Is possible to use if statement into variable calculation?

where are you using that variable ?

marygrace
New Contributor II

Re: Is possible to use if statement into variable calculation?

Into chart: table

arvind654
Honored Contributor II

Re: Is possible to use if statement into variable calculation?

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
Honored Contributor II

Re: Is possible to use if statement into variable calculation?

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

marygrace
New Contributor II

Re: Is possible to use if statement into variable calculation?

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
Honored Contributor II

Re: Is possible to use if statement into variable calculation?

it could be caused by your table dimensions.

which dimensions and measures are you using in table object?

marygrace
New Contributor II

Re: Is possible to use if statement into variable calculation?

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
Honored Contributor II

Re: Is possible to use if statement into variable calculation?

what you get with the below expression ?

=num(weekday(today()))

marygrace
New Contributor II

Re: Is possible to use if statement into variable calculation?

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