48
Views
10
Comments
Solved
Nested If-s don't work

I have the following query: 

If(  

  TimeDropdown = IntegerToIdentifier(3),  

  TextToDateTime(StartOfWeek) > AddMonths(CurrDate(), -1),  

      If(    TimeDropdown = IntegerToIdentifier(1),     

      TextToDateTime(StartOfWeek) >= AddMonths(CurrDate(), -3),    

          If(      TimeDropdown = IntegerToIdentifier(2),       

          TextToDateTime(StartOfWeek) >= AddMonths(CurrDate(), -6),      

              If(        TimeDropdown = IntegerToIdentifier(4),         

              TextToDateTime(StartOfWeek) >= AddMonths(CurrDate(), -12),        

              TextToDateTime(StartOfWeek) >= AddMonths(CurrDate(), -12)      )    )  ))


The query works when I execute only 2 If-s ( 

If(  

  TimeDropdown = IntegerToIdentifier(3),  

  TextToDateTime(StartOfWeek) > AddMonths(CurrDate(), -1),  

      If(    TimeDropdown = IntegerToIdentifier(1),     

      TextToDateTime(StartOfWeek) >= AddMonths(CurrDate(), -3))) 

or any combination of two) but as soon as I try to run 3 or more, I get the following error message: 


Error executing query. Case expressions may only be nested to level 10.
Incorrect syntax near 'datepart'.
Incorrect syntax near '4'.
Solution

Hi shooose,

Add Below filter to your aggregate 

If(  

  TimeDropdown = IntegerToIdentifier(3) 

Or  TimeDropdown = IntegerToIdentifier(4) 

Or  TimeDropdown = IntegerToIdentifier(1) 

Or    TimeDropdown = IntegerToIdentifier(2)

,true,

TextToDateTime(StartOfWeek) >= AddMonths(CurrDate(), -12)

)

And 

If(  

  TimeDropdown = IntegerToIdentifier(3), 

  TextToDateTime(StartOfWeek) > AddMonths(CurrDate(), -1), 

True)

And

If(  

  TimeDropdown = IntegerToIdentifier(1), 

  TextToDateTime(StartOfWeek) > AddMonths(CurrDate(), -3), 

True)

And

If(  

  TimeDropdown = IntegerToIdentifier(2), 

  TextToDateTime(StartOfWeek) > AddMonths(CurrDate(), -6), 

True)

And

If(  

  TimeDropdown = IntegerToIdentifier(4), 

  TextToDateTime(StartOfWeek) > AddMonths(CurrDate(), -12), 

True)



Thanks

Hey!


What is your goal with the conditions? Is this being used to set a custom attribute? Indeed SQL only allows up to 10 nested case statements.

Also, I don't understand your conditions because it should be If(Bool StatementResult if true, Result if false) and if we see here:

If(  

  TimeDropdown = IntegerToIdentifier(3),  

  TextToDateTime(StartOfWeek) > AddMonths(CurrDate(), -1),  

you have If + Condition(TimeDropdown = IntegerToIdentifier(3)), but then when you should provide a result you are giving another condition: TextToDateTime(StartOfWeek) > AddMonths(CurrDate(), -1), shouldn't you be providing a result here? for instance only "TextToDateTime(StartOfWeek) > AddMonths(CurrDate(), -1)"


Anyway, I think if your result is a number you can do something like this:

If(Condition1,Result1, 0) + If(Condition2, Result2, 0) + ...

Make sure your conditions are exclusive, otherwise, you may have more results than wished. And use this only if you have more than 10 nested case conditions!

Hope this helps.


Gonçalo

The goal: I want to filter an aggregate with the conditions. TimeDropdown is a local variable used in a dropdown. When I select something in the dropdown, for example 3, I want to see only the data of the last month. 

Champion

Hello

Are you using this in aggregate Filter or on screen, like can you share some sample Oml where you would like to use it.

Thanks

Hello shooouse,

Why don't you simply use the month difference as the value of your dropdown list? This way, you do not have to nest if-statements and can simply compare the date to the return value of AddMonths(CurrDate(), dropdown value).

As an alternative you can prepare the compare value in a local variable with a switch.

Hope this helps. Otherwise, could you please provide a OML so l can have a look and send you a solution?

Solution

Hi shooose,

Add Below filter to your aggregate 

If(  

  TimeDropdown = IntegerToIdentifier(3) 

Or  TimeDropdown = IntegerToIdentifier(4) 

Or  TimeDropdown = IntegerToIdentifier(1) 

Or    TimeDropdown = IntegerToIdentifier(2)

,true,

TextToDateTime(StartOfWeek) >= AddMonths(CurrDate(), -12)

)

And 

If(  

  TimeDropdown = IntegerToIdentifier(3), 

  TextToDateTime(StartOfWeek) > AddMonths(CurrDate(), -1), 

True)

And

If(  

  TimeDropdown = IntegerToIdentifier(1), 

  TextToDateTime(StartOfWeek) > AddMonths(CurrDate(), -3), 

True)

And

If(  

  TimeDropdown = IntegerToIdentifier(2), 

  TextToDateTime(StartOfWeek) > AddMonths(CurrDate(), -6), 

True)

And

If(  

  TimeDropdown = IntegerToIdentifier(4), 

  TextToDateTime(StartOfWeek) > AddMonths(CurrDate(), -12), 

True)



Thanks

Hi @shooouse ,

rather than pushing you deeper into your rabbit hole, I agree with @Sebastian Krempel , you are really making it far more complex than you need to.

I would go one step further than Sebastian : 

  • build your list for your dropdown with a date attribute you calculate during the OnInitialize (or maybe in some dataAction) doing all the AddMonths stuff in there.
  • The result is a list with a date and a text, you set the text in the dropdown as the options text (something like 'last 3 months'), and you use the date as the options value.
  • You have a local date variable, called something like 'SearchFromDate', and you set that as the attribute of the dropdown
  • in your aggregate, all you have to do is a filter entity.date >= SearchFromDate

An unrelated question : why on earth are you doing a TextToDate(StartOfWeek), is it stored as text ???? Why ?

Dorine

But to answer the question in the title (although I don't think that's a good design of your query), I had a try myself out of curiosity:

I don't see anything immediately wrong with it, I tried same thing, and can go up to 9 options + the else (=10 levels), until I get the warning about the levels.

Are you showing us the full filter you have ??  Is this If part of something bigger ?  Are there other filters in the same aggregate ?

Dorine

Hello @Dorine Boudry , answers to your questions:


1. yes, StartOfWeek is saved as text. Originally I had a date column from which I created the FirstDayOfWeek column. Then I wanted to group by FirstDayOfWeek, but for some reason I had 2 values of each FirstDayOfWeek: like 2023-01-23 and 2023-01-23 00:00:00. That was the problem I was trying to solve by saving StartOfWeek as text. 


2. the filter is not part of anything, and this is my only filter. 

please share an oml demonstrating both points, otherwise, it is going to be hard to help any further. 

I find it hard to understand that storing dates as text is the best solution for solving a group by problem, and for point 2, it works for me up to 9 nested if's, not 2, don't really see what you are doing differently.  Can you share the generated sql at least ?

Dorine

But to iterate again, those nested If's are not a good solution to your requirement !

That's a very ugly workaround, never do this! If you do not understand why something happens, just doing something else is bad!

Community GuidelinesBe kind and respectful, give credit to the original source of content, and search for duplicates before posting.