Calculated Field with IF statements

slamba
New Member

I am trying to create a calculated filed based on an if statement to categorize and add the sales in Looker. I have tried using multiple if statements but I am not able to figure out. I am not even sure if its possible in calculated field. Below is the data:

52ced3c3-a7c2-4699-8681-7095bf41c63f.png

I need to create a calculated field to categorize the above styles and sub styles in a new column. My output should look like below:

f5e3daa4-4cb6-4972-9041-03bf7fe91e99.png
Solved Solved
0 4 43.4K
1 ACCEPTED SOLUTION

Hi @slamba,

This is possible to do just with table calculations, but it would involve quite a few steps. I wrote out a similar example on my instance. First, I used an if statement to create a new column for the total of each cohort we want to show. In my example, I wanted total by brand. 

** The overall goal of this was to aggregate count of users by a limited number of brands. 

 

77b23dea-dbf8-45d8-87a1-912ece4ddf80.png

I did have to create a new table calculation for each brand I wanted to show. 

Then, I created a column that took the value of the new aggregated column and combined them based on the original brand name in the dimension field. 

 

0e5cdf63-a8d6-4e45-b8c5-b9b70f0f4f12.png

Then, I wanted to show only one line item per brand, so I sorted the data tab by the brand name and used the offset function with an if statement to determined if the line item above had the same brand name. If it did I returned no, else I returned yes. Then, I clicked hide no’s from visualization.

 

5505a7c6-fb12-400f-865c-daba748ba186.png
41c58902-4fbe-4542-8e60-bfcdc954b304.png

This was the explore I started with below:

 

b8999f40-1874-4621-9cf5-affb8693df43.png

This was my end result below!

 

19f98f69-74fe-4526-81e2-1e4bb937eba0.png

We can try to combine a few of these steps, but I wanted to lay them all out just to make the logic as clear as possible. The alternative would be to remove the style and sub categories if those are dimension and then create a measure type sum for $$. Then, we get sum of sales grouped by New Style. We could create new style in the LookML layer with a case when statement. 

Please let me know if you have any questions.

Thanks,

Eric

View solution in original post

4 REPLIES 4

Hi @slamba,

This is possible to do just with table calculations, but it would involve quite a few steps. I wrote out a similar example on my instance. First, I used an if statement to create a new column for the total of each cohort we want to show. In my example, I wanted total by brand. 

** The overall goal of this was to aggregate count of users by a limited number of brands. 

 

77b23dea-dbf8-45d8-87a1-912ece4ddf80.png

I did have to create a new table calculation for each brand I wanted to show. 

Then, I created a column that took the value of the new aggregated column and combined them based on the original brand name in the dimension field. 

 

0e5cdf63-a8d6-4e45-b8c5-b9b70f0f4f12.png

Then, I wanted to show only one line item per brand, so I sorted the data tab by the brand name and used the offset function with an if statement to determined if the line item above had the same brand name. If it did I returned no, else I returned yes. Then, I clicked hide no’s from visualization.

 

5505a7c6-fb12-400f-865c-daba748ba186.png
41c58902-4fbe-4542-8e60-bfcdc954b304.png

This was the explore I started with below:

 

b8999f40-1874-4621-9cf5-affb8693df43.png

This was my end result below!

 

19f98f69-74fe-4526-81e2-1e4bb937eba0.png

We can try to combine a few of these steps, but I wanted to lay them all out just to make the logic as clear as possible. The alternative would be to remove the style and sub categories if those are dimension and then create a measure type sum for $$. Then, we get sum of sales grouped by New Style. We could create new style in the LookML layer with a case when statement. 

Please let me know if you have any questions.

Thanks,

Eric

slamba
New Member

@Eric_Lyons Thanks for this. My If statement should be based on both the style and sub style dimension. I think in your example it is only based on one dimension.

slamba
New Member

I did some workaround and I finally got it !! thanks for all your help 🙂

Hi @Eric_Lyons  this is great, thank you. 

A question I have is how can I use this method to create a new field based on an existing numeric field being either odd or even?

e.g. IF field ID is odd then the new field gets a value of 1 , even then the new field gets a value of 2?

Many thanks

Top Labels in this Space
Top Solution Authors